[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/search/Zend/Search/Lucene/Search/QueryEntry/ -> Term.php (source)

   1  <?php
   2  /**
   3   * Zend Framework
   4   *
   5   * LICENSE
   6   *
   7   * This source file is subject to the new BSD license that is bundled
   8   * with this package in the file LICENSE.txt.
   9   * It is also available through the world-wide-web at this URL:
  10   * http://framework.zend.com/license/new-bsd
  11   * If you did not receive a copy of the license and are unable to
  12   * obtain it through the world-wide-web, please send an email
  13   * to license@zend.com so we can send you a copy immediately.
  14   *
  15   * @category   Zend
  16   * @package    Zend_Search_Lucene
  17   * @subpackage Search
  18   * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  19   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  20   */
  21  
  22  
  23  /** Zend_Search_Lucene_Index_Term */
  24  require_once $CFG->dirroot.'/search/Zend/Search/Lucene/Index/Term.php';
  25  
  26  /** Zend_Search_Lucene_Exception */
  27  require_once $CFG->dirroot.'/search/Zend/Search/Lucene/Exception.php';
  28  
  29  /** Zend_Search_Lucene_Search_QueryEntry */
  30  require_once $CFG->dirroot.'/search/Zend/Search/Lucene/Search/QueryEntry.php';
  31  
  32  /** Zend_Search_Lucene_Search_QueryParserException */
  33  require_once $CFG->dirroot.'/search/Zend/Search/Lucene/Search/QueryParserException.php';
  34  
  35  /** Zend_Search_Lucene_Analysis_Analyzer */
  36  require_once $CFG->dirroot.'/search/Zend/Search/Lucene/Analysis/Analyzer.php';
  37  
  38  
  39  
  40  /**
  41   * @category   Zend
  42   * @package    Zend_Search_Lucene
  43   * @subpackage Search
  44   * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  45   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  46   */
  47  class Zend_Search_Lucene_Search_QueryEntry_Term extends Zend_Search_Lucene_Search_QueryEntry
  48  {
  49      /**
  50       * Term value
  51       *
  52       * @var string
  53       */
  54      private $_term;
  55  
  56      /**
  57       * Field
  58       *
  59       * @var string|null
  60       */
  61      private $_field;
  62  
  63  
  64      /**
  65       * Fuzzy search query
  66       *
  67       * @var boolean
  68       */
  69      private $_fuzzyQuery = false;
  70  
  71      /**
  72       * Similarity
  73       *
  74       * @var float
  75       */
  76      private $_similarity = 1.;
  77  
  78  
  79      /**
  80       * Object constractor
  81       *
  82       * @param string $term
  83       * @param string $field
  84       */
  85      public function __construct($term, $field)
  86      {
  87          $this->_term  = $term;
  88          $this->_field = $field;
  89      }
  90  
  91      /**
  92       * Process modifier ('~')
  93       *
  94       * @param mixed $parameter
  95       */
  96      public function processFuzzyProximityModifier($parameter = null)
  97      {
  98          $this->_fuzzyQuery = true;
  99  
 100          if ($parameter !== null) {
 101              $this->_similarity = $parameter;
 102          } else {
 103              $this->_similarity = 0.5;
 104          }
 105      }
 106  
 107      /**
 108       * Transform entry to a subquery
 109       *
 110       * @param string $encoding
 111       * @return Zend_Search_Lucene_Search_Query
 112       * @throws Zend_Search_Lucene_Search_QueryParserException
 113       */
 114      public function getQuery($encoding)
 115      {
 116          if ($this->_fuzzyQuery) {
 117              throw new Zend_Search_Lucene_Search_QueryParserException('Fuzzy search is not supported yet.');
 118          }
 119  
 120          if (strpos($this->_term, '?') !== false || strpos($this->_term, '*') !== false) {
 121              throw new Zend_Search_Lucene_Search_QueryParserException('Wildcard queries are not supported yet.');
 122          }
 123  
 124          $tokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($this->_term, $encoding);
 125  
 126          if (count($tokens) == 0) {
 127              return new Zend_Search_Lucene_Search_Query_Empty();
 128          }
 129  
 130          if (count($tokens) == 1) {
 131              $term  = new Zend_Search_Lucene_Index_Term($tokens[0]->getTermText(), $this->_field);
 132              $query = new Zend_Search_Lucene_Search_Query_Term($term);
 133              $query->setBoost($this->_boost);
 134  
 135              return $query;
 136          }
 137  
 138          //It's not empty or one term query
 139          $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
 140  
 141          /**
 142           * @todo Process $token->getPositionIncrement() to support stemming, synonyms and other
 143           * analizer design features
 144           */
 145          foreach ($tokens as $token) {
 146              $term = new Zend_Search_Lucene_Index_Term($token->getTermText(), $this->_field);
 147              $query->addTerm($term, true); // all subterms are required
 148          }
 149  
 150          $query->setBoost($this->_boost);
 151  
 152          return $query;
 153      }
 154  }


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7