[ Index ]

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

title

Body

[close]

/search/Zend/Search/Lucene/Search/Query/ -> 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_Search_Query */
  24  require_once $CFG->dirroot.'/search/Zend/Search/Lucene/Search/Query.php';
  25  
  26  /** Zend_Search_Lucene_Search_Weight_Term */
  27  require_once $CFG->dirroot.'/search/Zend/Search/Lucene/Search/Weight/Term.php';
  28  
  29  
  30  /**
  31   * @category   Zend
  32   * @package    Zend_Search_Lucene
  33   * @subpackage Search
  34   * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  35   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  36   */
  37  class Zend_Search_Lucene_Search_Query_Term extends Zend_Search_Lucene_Search_Query
  38  {
  39      /**
  40       * Term to find.
  41       *
  42       * @var Zend_Search_Lucene_Index_Term
  43       */
  44      private $_term;
  45  
  46      /**
  47       * Documents vector.
  48       *
  49       * @var array
  50       */
  51      private $_docVector = null;
  52  
  53      /**
  54       * Term freqs vector.
  55       * array(docId => freq, ...)
  56       *
  57       * @var array
  58       */
  59      private $_termFreqs;
  60  
  61  
  62      /**
  63       * Zend_Search_Lucene_Search_Query_Term constructor
  64       *
  65       * @param Zend_Search_Lucene_Index_Term $term
  66       * @param boolean $sign
  67       */
  68      public function __construct($term)
  69      {
  70          $this->_term = $term;
  71      }
  72  
  73      /**
  74       * Re-write query into primitive queries in the context of specified index
  75       *
  76       * @param Zend_Search_Lucene_Interface $index
  77       * @return Zend_Search_Lucene_Search_Query
  78       */
  79      public function rewrite(Zend_Search_Lucene_Interface $index)
  80      {
  81          if ($this->_term->field != null) {
  82              return $this;
  83          } else {
  84              $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
  85              $query->setBoost($this->getBoost());
  86  
  87              foreach ($index->getFieldNames(true) as $fieldName) {
  88                  $term = new Zend_Search_Lucene_Index_Term($this->_term->text, $fieldName);
  89  
  90                  $query->addTerm($term);
  91              }
  92  
  93              return $query->rewrite($index);
  94          }
  95      }
  96  
  97      /**
  98       * Optimize query in the context of specified index
  99       *
 100       * @param Zend_Search_Lucene_Interface $index
 101       * @return Zend_Search_Lucene_Search_Query
 102       */
 103      public function optimize(Zend_Search_Lucene_Interface $index)
 104      {
 105          // Check, that index contains specified term
 106          if (!$index->hasTerm($this->_term)) {
 107              return new Zend_Search_Lucene_Search_Query_Empty();
 108          }
 109  
 110          return $this;
 111      }
 112  
 113  
 114      /**
 115       * Constructs an appropriate Weight implementation for this query.
 116       *
 117       * @param Zend_Search_Lucene_Interface $reader
 118       * @return Zend_Search_Lucene_Search_Weight
 119       */
 120      public function createWeight(Zend_Search_Lucene_Interface $reader)
 121      {
 122          $this->_weight = new Zend_Search_Lucene_Search_Weight_Term($this->_term, $this, $reader);
 123          return $this->_weight;
 124      }
 125  
 126      /**
 127       * Execute query in context of index reader
 128       * It also initializes necessary internal structures
 129       *
 130       * @param Zend_Search_Lucene_Interface $reader
 131       */
 132      public function execute(Zend_Search_Lucene_Interface $reader)
 133      {
 134          $this->_docVector = array_flip($reader->termDocs($this->_term));
 135          $this->_termFreqs = $reader->termFreqs($this->_term);
 136  
 137          // Initialize weight if it's not done yet
 138          $this->_initWeight($reader);
 139      }
 140  
 141      /**
 142       * Get document ids likely matching the query
 143       *
 144       * It's an array with document ids as keys (performance considerations)
 145       *
 146       * @return array
 147       */
 148      public function matchedDocs()
 149      {
 150          return $this->_docVector;
 151      }
 152  
 153      /**
 154       * Score specified document
 155       *
 156       * @param integer $docId
 157       * @param Zend_Search_Lucene_Interface $reader
 158       * @return float
 159       */
 160      public function score($docId, Zend_Search_Lucene_Interface $reader)
 161      {
 162          if (isset($this->_docVector[$docId])) {
 163              return $reader->getSimilarity()->tf($this->_termFreqs[$docId]) *
 164                     $this->_weight->getValue() *
 165                     $reader->norm($docId, $this->_term->field) *
 166                     $this->getBoost();
 167          } else {
 168              return 0;
 169          }
 170      }
 171  
 172      /**
 173       * Return query terms
 174       *
 175       * @return array
 176       */
 177      public function getQueryTerms()
 178      {
 179          return array($this->_term);
 180      }
 181  
 182      /**
 183       * Return query term
 184       *
 185       * @return Zend_Search_Lucene_Index_Term
 186       */
 187      public function getTerm()
 188      {
 189          return $this->_term;
 190      }
 191  
 192      /**
 193       * Returns query term
 194       *
 195       * @return array
 196       */
 197      public function getTerms()
 198      {
 199          return $this->_terms;
 200      }
 201  
 202      /**
 203       * Highlight query terms
 204       *
 205       * @param integer &$colorIndex
 206       * @param Zend_Search_Lucene_Document_Html $doc
 207       */
 208      public function highlightMatchesDOM(Zend_Search_Lucene_Document_Html $doc, &$colorIndex)
 209      {
 210          $doc->highlight($this->_term->text, $this->_getHighlightColor($colorIndex));
 211      }
 212  
 213      /**
 214       * Print a query
 215       *
 216       * @return string
 217       */
 218      public function __toString()
 219      {
 220          // It's used only for query visualisation, so we don't care about characters escaping
 221          return (($this->_term->field === null)? '':$this->_term->field . ':') . $this->_term->text;
 222      }
 223  }
 224  


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