[ Index ]

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

title

Body

[close]

/search/Zend/Search/Lucene/Search/QueryEntry/ -> Phrase.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_Phrase extends Zend_Search_Lucene_Search_QueryEntry
  48  {
  49      /**
  50       * Phrase value
  51       *
  52       * @var string
  53       */
  54      private $_phrase;
  55  
  56      /**
  57       * Field
  58       *
  59       * @var string|null
  60       */
  61      private $_field;
  62  
  63  
  64      /**
  65       * Proximity phrase query
  66       *
  67       * @var boolean
  68       */
  69      private $_proximityQuery = false;
  70  
  71      /**
  72       * Words distance, used for proximiti queries
  73       *
  74       * @var integer
  75       */
  76      private $_wordsDistance = 0;
  77  
  78  
  79      /**
  80       * Object constractor
  81       *
  82       * @param string $phrase
  83       * @param string $field
  84       */
  85      public function __construct($phrase, $field)
  86      {
  87          $this->_phrase = $phrase;
  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->_proximityQuery = true;
  99  
 100          if ($parameter !== null) {
 101              $this->_wordsDistance = $parameter;
 102          }
 103      }
 104  
 105      /**
 106       * Transform entry to a subquery
 107       *
 108       * @param string $encoding
 109       * @return Zend_Search_Lucene_Search_Query
 110       * @throws Zend_Search_Lucene_Search_QueryParserException
 111       */
 112      public function getQuery($encoding)
 113      {
 114          if (strpos($this->_phrase, '?') !== false || strpos($this->_phrase, '*') !== false) {
 115              throw new Zend_Search_Lucene_Search_QueryParserException('Wildcards are only allowed in a single terms.');
 116          }
 117  
 118          $tokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($this->_phrase, $encoding);
 119  
 120          if (count($tokens) == 0) {
 121              return new Zend_Search_Lucene_Search_Query_Empty();
 122          }
 123  
 124          if (count($tokens) == 1) {
 125              $term  = new Zend_Search_Lucene_Index_Term($tokens[0]->getTermText(), $this->_field);
 126              $query = new Zend_Search_Lucene_Search_Query_Term($term);
 127              $query->setBoost($this->_boost);
 128  
 129              return $query;
 130          }
 131  
 132          //It's not empty or one term query
 133          $query = new Zend_Search_Lucene_Search_Query_Phrase();
 134          foreach ($tokens as $token) {
 135              $term = new Zend_Search_Lucene_Index_Term($token->getTermText(), $this->_field);
 136              $query->addTerm($term);
 137          }
 138  
 139          if ($this->_proximityQuery) {
 140              $query->setSlop($this->_wordsDistance);
 141          }
 142  
 143          $query->setBoost($this->_boost);
 144  
 145          return $query;
 146      }
 147  }


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