Development

Changeset 10529

You must first sign up to be able to contribute.

Changeset 10529

Show
Ignore:
Timestamp:
07/31/08 07:12:36 (2 years ago)
Author:
Carl.Vondrick
Message:

sfSearch: added support for range queries and wildcards to Lucene lexer and general cleanup

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfSearchPlugin/trunk/lib/lexer/lucene/xfLexemeLucene.class.php

    r10409 r10529  
    99 
    1010/** 
    11  * A Lexeme that follows Lucene rules. 
     11 * A Lexeme with Lucene types. 
    1212 * 
    1313 * @package sfSearch 
     
    1515 * @author Carl Vondrick 
    1616 */ 
    17 class xfLexemeLucene extends xfLexeme 
     17final class xfLexemeLucene extends xfLexeme 
    1818{ 
     19  const WORD = 1; 
     20  const SYNTAX = 2; 
    1921  const PHRASE = 3; 
    2022  const NUMBER = 4; 
    2123  const FIELD = 5; 
     24  const WILDCARD = 6; 
     25  const RANGE_START_INCLUSIVE = 100; 
     26  const RANGE_START_EXCLUSIVE = 101; 
     27  const RANGE_END_INCLUSIVE   = 102; 
     28  const RANGE_END_EXCLUSIVE   = 103; 
     29  const RANGE_SEPARATOR       = 104; 
    2230 
    2331  /** 
     
    2634  public function setType($type) 
    2735  { 
     36    $lexeme = strtolower($this->getLexeme()); 
     37 
     38    if ($type == self::SYNTAX) 
     39    { 
     40      if ($lexeme == '[') 
     41      { 
     42        $type = self::RANGE_START_INCLUSIVE; 
     43      } 
     44      elseif ($lexeme == '{') 
     45      { 
     46        $type = self::RANGE_START_EXCLUSIVE; 
     47      } 
     48      elseif ($lexeme == ']') 
     49      { 
     50        $type = self::RANGE_END_INCLUSIVE; 
     51      } 
     52      elseif ($lexeme == '}') 
     53      { 
     54        $type = self::RANGE_END_EXCLUSIVE; 
     55      } 
     56    } 
     57    elseif ($type == self::WORD) 
     58    { 
     59      if ($lexeme == 'to') 
     60      { 
     61        $type = self::RANGE_SEPARATOR; 
     62      } 
     63      elseif ($lexeme == 'and') 
     64      { 
     65        $type = self::SYNTAX; 
     66      } 
     67      elseif ($lexeme == 'or') 
     68      { 
     69        $type = self::SYNTAX; 
     70      } 
     71      elseif ($lexeme == 'not') 
     72      { 
     73        $type = self::SYNTAX; 
     74      } 
     75      elseif (false !== strpos($lexeme, '*') || false !== strpos($lexeme, '?')) 
     76      { 
     77        $type = self::WILDCARD; 
     78      } 
     79    } 
     80 
    2881    parent::setType($type); 
    2982  } 
  • plugins/sfSearchPlugin/trunk/lib/lexer/xfLexeme.class.php

    r10409 r10529  
    1717class xfLexeme 
    1818{ 
    19   const WORD = 1; 
    20   const SYNTAX = 2; 
    21  
    2219  /** 
    2320   * The type 
  • plugins/sfSearchPlugin/trunk/lib/lexer/xfLexemeBuilder.class.php

    r10409 r10529  
    9797    $this->position++; 
    9898 
    99     if (isset($this->characters[$this->position])) 
     99    if (array_key_exists($this->position, $this->characters)) 
    100100    { 
    101101      return $this->characters[$this->position]; 
    102102    } 
    103      
     103 
    104104    return false; 
    105105  } 
  • plugins/sfSearchPlugin/trunk/test/unit/lexer/lucene/xfLexerLuceneTest.php

    r10409 r10529  
    2424require 'parser/xfParserException.class.php'; 
    2525 
    26 $t = new xfLexerTester(new lime_test(162, new lime_output_auto), new xfLexerLucene); 
     26$t = new xfLexerTester(new lime_test(182, new lime_output_auto), new xfLexerLucene); 
    2727 
    2828$t->pass('', array()); 
     
    8585$t->pass('\\+boo', array(array('+boo', xfLexemeLucene::WORD))); 
    8686 
     87 
    8788$t->pass('\\&', array(array('&', xfLexemeLucene::WORD))); 
    8889 
     
    9899 
    99100$t->fail('a | b', 'A | must follow a | character.'); 
     101 
     102$t->pass('[1 to 30]', array(array('[', xfLexemeLucene::RANGE_START_INCLUSIVE), 
     103                            array('1', xfLexemeLucene::WORD), 
     104                            array('to', xfLexemeLucene::RANGE_SEPARATOR), 
     105                            array('30', xfLexemeLucene::WORD), 
     106                            array(']', xfLexemeLucene::RANGE_END_INCLUSIVE))); 
     107 
     108$t->pass('m*n', array(array('m*n', xfLexemeLucene::WILDCARD))); 
     109 
     110$t->pass('m?n', array(array('m?n', xfLexemeLucene::WILDCARD))); 
     111 
     112$t->pass('t*s?', array(array('t*s?', xfLexemeLucene::WILDCARD))); 
    100113 
    101114$t->pass('(+a || \\b) && f: c && ("foo bar"~2 || (y && +x))^2.5', array(array('(', xfLexemeLucene::SYNTAX), 
  • plugins/sfSearchPlugin/trunk/test/unit/lexer/xfLexemeBuilderTest.php

    r10409 r10529  
    1313require 'util/xfException.class.php'; 
    1414 
    15 $t = new lime_test(17, new lime_output_color); 
     15$t = new lime_test(20, new lime_output_color); 
    1616 
    1717$b = new xfLexemeBuilder('symfony pròject'); 
     
    7474$b->commit(); 
    7575$t->is($b->count(), 3, '->commit() does not create a new lexeme if the text is empty'); 
     76 
     77$b = new xfLexemeBuilder('102'); 
     78$t->is($b->next(), 1, '->next() works on numbers'); 
     79$t->is($b->next(), 0, '->next() works on numbers'); 
     80$t->is($b->next(), 2, '->next() works on numbers'); 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting.
Sensio Labs also supports several large Open-Source projects.