[ Index ]

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

title

Body

[close]

/search/Zend/Search/Lucene/Search/ -> Query.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  /** Zend_Search_Lucene_Document_Html */
  23  require_once $CFG->dirroot.'/search/Zend/Search/Lucene/Document/Html.php';
  24  
  25  
  26  /**
  27   * @category   Zend
  28   * @package    Zend_Search_Lucene
  29   * @subpackage Search
  30   * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  31   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  32   */
  33  abstract class Zend_Search_Lucene_Search_Query
  34  {
  35  
  36      /**
  37       * query boost factor
  38       *
  39       * @var float
  40       */
  41      private $_boost = 1;
  42  
  43      /**
  44       * Query weight
  45       *
  46       * @var Zend_Search_Lucene_Search_Weight
  47       */
  48      protected $_weight = null;
  49  
  50      /**
  51       * Current highlight color
  52       *
  53       * @var integer
  54       */
  55      private $_currentColorIndex = 0;
  56  
  57      /**
  58       * List of colors for text highlighting
  59       *
  60       * @var array
  61       */
  62      private $_highlightColors = array('#66ffff', '#ff66ff', '#ffff66',
  63                                        '#ff8888', '#88ff88', '#8888ff',
  64                                        '#88dddd', '#dd88dd', '#dddd88',
  65                                        '#aaddff', '#aaffdd', '#ddaaff', '#ddffaa', '#ffaadd', '#ffddaa');
  66  
  67  
  68      /**
  69       * Gets the boost for this clause.  Documents matching
  70       * this clause will (in addition to the normal weightings) have their score
  71       * multiplied by boost.   The boost is 1.0 by default.
  72       *
  73       * @return float
  74       */
  75      public function getBoost()
  76      {
  77          return $this->_boost;
  78      }
  79  
  80      /**
  81       * Sets the boost for this query clause to $boost.
  82       *
  83       * @param float $boost
  84       */
  85      public function setBoost($boost)
  86      {
  87          $this->_boost = $boost;
  88      }
  89  
  90      /**
  91       * Score specified document
  92       *
  93       * @param integer $docId
  94       * @param Zend_Search_Lucene_Interface $reader
  95       * @return float
  96       */
  97      abstract public function score($docId, Zend_Search_Lucene_Interface $reader);
  98  
  99      /**
 100       * Get document ids likely matching the query
 101       *
 102       * It's an array with document ids as keys (performance considerations)
 103       *
 104       * @return array
 105       */
 106      abstract public function matchedDocs();
 107  
 108      /**
 109       * Execute query in context of index reader
 110       * It also initializes necessary internal structures
 111       *
 112       * Query specific implementation
 113       *
 114       * @param Zend_Search_Lucene_Interface $reader
 115       */
 116      abstract public function execute(Zend_Search_Lucene_Interface $reader);
 117  
 118      /**
 119       * Constructs an appropriate Weight implementation for this query.
 120       *
 121       * @param Zend_Search_Lucene_Interface $reader
 122       * @return Zend_Search_Lucene_Search_Weight
 123       */
 124      abstract public function createWeight(Zend_Search_Lucene_Interface $reader);
 125  
 126      /**
 127       * Constructs an initializes a Weight for a _top-level_query_.
 128       *
 129       * @param Zend_Search_Lucene_Interface $reader
 130       */
 131      protected function _initWeight(Zend_Search_Lucene_Interface $reader)
 132      {
 133          // Check, that it's a top-level query and query weight is not initialized yet.
 134          if ($this->_weight !== null) {
 135              return $this->_weight;
 136          }
 137  
 138          $this->createWeight($reader);
 139          $sum = $this->_weight->sumOfSquaredWeights();
 140          $queryNorm = $reader->getSimilarity()->queryNorm($sum);
 141          $this->_weight->normalize($queryNorm);
 142      }
 143  
 144      /**
 145       * Re-write query into primitive queries in the context of specified index
 146       *
 147       * @param Zend_Search_Lucene_Interface $index
 148       * @return Zend_Search_Lucene_Search_Query
 149       */
 150      abstract public function rewrite(Zend_Search_Lucene_Interface $index);
 151  
 152      /**
 153       * Optimize query in the context of specified index
 154       *
 155       * @param Zend_Search_Lucene_Interface $index
 156       * @return Zend_Search_Lucene_Search_Query
 157       */
 158      abstract public function optimize(Zend_Search_Lucene_Interface $index);
 159  
 160      /**
 161       * Reset query, so it can be reused within other queries or
 162       * with other indeces
 163       */
 164      public function reset()
 165      {
 166          $this->_weight = null;
 167      }
 168  
 169  
 170      /**
 171       * Print a query
 172       *
 173       * @return string
 174       */
 175      abstract public function __toString();
 176  
 177      /**
 178       * Return query terms
 179       *
 180       * @return array
 181       */
 182      abstract public function getQueryTerms();
 183  
 184      /**
 185       * Get highlight color and shift to next
 186       *
 187       * @param integer &$colorIndex
 188       * @return string
 189       */
 190      protected function _getHighlightColor(&$colorIndex)
 191      {
 192          $color = $this->_highlightColors[$colorIndex++];
 193  
 194          $colorIndex %= count($this->_highlightColors);
 195  
 196          return $color;
 197      }
 198  
 199      /**
 200       * Highlight query terms
 201       *
 202       * @param integer &$colorIndex
 203       * @param Zend_Search_Lucene_Document_Html $doc
 204       */
 205      abstract public function highlightMatchesDOM(Zend_Search_Lucene_Document_Html $doc, &$colorIndex);
 206  
 207      /**
 208       * Highlight matches in $inputHTML
 209       *
 210       * @param string $inputHTML
 211       * @return string
 212       */
 213      public function highlightMatches($inputHTML)
 214      {
 215          $doc = Zend_Search_Lucene_Document_Html::loadHTML($inputHTML);
 216  
 217          $colorIndex = 0;
 218          $this->highlightMatchesDOM($doc, $colorIndex);
 219  
 220          return $doc->getHTML();
 221      }
 222  }
 223  


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