final move of files
[web.mtrack] / Zend / Search / Lucene / Search / Query / Preprocessing / Phrase.php
diff --git a/Zend/Search/Lucene/Search/Query/Preprocessing/Phrase.php b/Zend/Search/Lucene/Search/Query/Preprocessing/Phrase.php
new file mode 100644 (file)
index 0000000..6ec236c
--- /dev/null
@@ -0,0 +1,274 @@
+<?php\r
+/**\r
+ * Zend Framework\r
+ *\r
+ * LICENSE\r
+ *\r
+ * This source file is subject to the new BSD license that is bundled\r
+ * with this package in the file LICENSE.txt.\r
+ * It is also available through the world-wide-web at this URL:\r
+ * http://framework.zend.com/license/new-bsd\r
+ * If you did not receive a copy of the license and are unable to\r
+ * obtain it through the world-wide-web, please send an email\r
+ * to license@zend.com so we can send you a copy immediately.\r
+ *\r
+ * @category   Zend\r
+ * @package    Zend_Search_Lucene\r
+ * @subpackage Search\r
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)\r
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License\r
+ * @version    $Id: Phrase.php 16971 2009-07-22 18:05:45Z mikaelkael $\r
+ */\r
+\r
+\r
+/** Zend_Search_Lucene_Search_Query_Processing */\r
+require_once 'Zend/Search/Lucene/Search/Query/Preprocessing.php';\r
+\r
+/** Zend_Search_Lucene_Search_Query_Phrase */\r
+require_once 'Zend/Search/Lucene/Search/Query/Phrase.php';\r
+\r
+/** Zend_Search_Lucene_Search_Query_Insignificant */\r
+require_once 'Zend/Search/Lucene/Search/Query/Insignificant.php';\r
+\r
+/** Zend_Search_Lucene_Search_Query_Empty */\r
+require_once 'Zend/Search/Lucene/Search/Query/Empty.php';\r
+\r
+/** Zend_Search_Lucene_Search_Query_Term */\r
+require_once 'Zend/Search/Lucene/Search/Query/Term.php';\r
+\r
+/** Zend_Search_Lucene_Index_Term */\r
+require_once 'Zend/Search/Lucene/Index/Term.php';\r
+\r
+\r
+/**\r
+ * It's an internal abstract class intended to finalize ase a query processing after query parsing.\r
+ * This type of query is not actually involved into query execution.\r
+ *\r
+ * @category   Zend\r
+ * @package    Zend_Search_Lucene\r
+ * @subpackage Search\r
+ * @internal\r
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)\r
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License\r
+ */\r
+class Zend_Search_Lucene_Search_Query_Preprocessing_Phrase extends Zend_Search_Lucene_Search_Query_Preprocessing\r
+{\r
+    /**\r
+     * Phrase to find.\r
+     *\r
+     * @var string\r
+     */\r
+    private $_phrase;\r
+\r
+    /**\r
+     * Phrase encoding (field name is always provided using UTF-8 encoding since it may be retrieved from index).\r
+     *\r
+     * @var string\r
+     */\r
+    private $_phraseEncoding;\r
+\r
+\r
+    /**\r
+     * Field name.\r
+     *\r
+     * @var string\r
+     */\r
+    private $_field;\r
+\r
+    /**\r
+     * Sets the number of other words permitted between words in query phrase.\r
+     * If zero, then this is an exact phrase search.  For larger values this works\r
+     * like a WITHIN or NEAR operator.\r
+     *\r
+     * The slop is in fact an edit-distance, where the units correspond to\r
+     * moves of terms in the query phrase out of position.  For example, to switch\r
+     * the order of two words requires two moves (the first move places the words\r
+     * atop one another), so to permit re-orderings of phrases, the slop must be\r
+     * at least two.\r
+     * More exact matches are scored higher than sloppier matches, thus search\r
+     * results are sorted by exactness.\r
+     *\r
+     * The slop is zero by default, requiring exact matches.\r
+     *\r
+     * @var integer\r
+     */\r
+    private $_slop;\r
+\r
+    /**\r
+     * Class constructor.  Create a new preprocessing object for prase query.\r
+     *\r
+     * @param string $phrase          Phrase to search.\r
+     * @param string $phraseEncoding  Phrase encoding.\r
+     * @param string $fieldName       Field name.\r
+     */\r
+    public function __construct($phrase, $phraseEncoding, $fieldName)\r
+    {\r
+       $this->_phrase         = $phrase;\r
+       $this->_phraseEncoding = $phraseEncoding;\r
+       $this->_field          = $fieldName;\r
+    }\r
+\r
+    /**\r
+     * Set slop\r
+     *\r
+     * @param integer $slop\r
+     */\r
+    public function setSlop($slop)\r
+    {\r
+        $this->_slop = $slop;\r
+    }\r
+\r
+\r
+    /**\r
+     * Get slop\r
+     *\r
+     * @return integer\r
+     */\r
+    public function getSlop()\r
+    {\r
+        return $this->_slop;\r
+    }\r
+\r
+    /**\r
+     * Re-write query into primitive queries in the context of specified index\r
+     *\r
+     * @param Zend_Search_Lucene_Interface $index\r
+     * @return Zend_Search_Lucene_Search_Query\r
+     */\r
+    public function rewrite(Zend_Search_Lucene_Interface $index)\r
+    {\r
+// Allow to use wildcards within phrases\r
+// They are either removed by text analyzer or used as a part of keyword for keyword fields\r
+//\r
+//        if (strpos($this->_phrase, '?') !== false || strpos($this->_phrase, '*') !== false) {\r
+//            require_once 'Zend/Search/Lucene/Search/QueryParserException.php';\r
+//            throw new Zend_Search_Lucene_Search_QueryParserException('Wildcards are only allowed in a single terms.');\r
+//        }\r
+\r
+       // Split query into subqueries if field name is not specified\r
+       if ($this->_field === null) {\r
+               $query = new Zend_Search_Lucene_Search_Query_Boolean();\r
+            $query->setBoost($this->getBoost());\r
+\r
+            if (Zend_Search_Lucene::getDefaultSearchField() === null) {\r
+                $searchFields = $index->getFieldNames(true);\r
+            } else {\r
+                $searchFields = array(Zend_Search_Lucene::getDefaultSearchField());\r
+            }\r
+\r
+            foreach ($searchFields as $fieldName) {\r
+                $subquery = new Zend_Search_Lucene_Search_Query_Preprocessing_Phrase($this->_phrase,\r
+                                                                                     $this->_phraseEncoding,\r
+                                                                                     $fieldName);\r
+                $subquery->setSlop($this->getSlop());\r
+\r
+                $query->addSubquery($subquery->rewrite($index));\r
+            }\r
+\r
+            $this->_matches = $query->getQueryTerms();\r
+            return $query;\r
+       }\r
+\r
+       // Recognize exact term matching (it corresponds to Keyword fields stored in the index)\r
+       // encoding is not used since we expect binary matching\r
+       $term = new Zend_Search_Lucene_Index_Term($this->_phrase, $this->_field);\r
+       if ($index->hasTerm($term)) {\r
+            $query = new Zend_Search_Lucene_Search_Query_Term($term);\r
+               $query->setBoost($this->getBoost());\r
+\r
+               $this->_matches = $query->getQueryTerms();\r
+               return $query;\r
+       }\r
+\r
+\r
+       // tokenize phrase using current analyzer and process it as a phrase query\r
+        $tokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($this->_phrase, $this->_phraseEncoding);\r
+\r
+        if (count($tokens) == 0) {\r
+               $this->_matches = array();\r
+            return new Zend_Search_Lucene_Search_Query_Insignificant();\r
+        }\r
+\r
+        if (count($tokens) == 1) {\r
+            $term  = new Zend_Search_Lucene_Index_Term($tokens[0]->getTermText(), $this->_field);\r
+            $query = new Zend_Search_Lucene_Search_Query_Term($term);\r
+            $query->setBoost($this->getBoost());\r
+\r
+            $this->_matches = $query->getQueryTerms();\r
+            return $query;\r
+        }\r
+\r
+        //It's non-trivial phrase query\r
+        $position = -1;\r
+        $query = new Zend_Search_Lucene_Search_Query_Phrase();\r
+        foreach ($tokens as $token) {\r
+            $position += $token->getPositionIncrement();\r
+            $term = new Zend_Search_Lucene_Index_Term($token->getTermText(), $this->_field);\r
+            $query->addTerm($term, $position);\r
+            $query->setSlop($this->getSlop());\r
+        }\r
+        $this->_matches = $query->getQueryTerms();\r
+        return $query;\r
+    }\r
+\r
+    /**\r
+     * Query specific matches highlighting\r
+     *\r
+     * @param Zend_Search_Lucene_Search_Highlighter_Interface $highlighter  Highlighter object (also contains doc for highlighting)\r
+     */\r
+    protected function _highlightMatches(Zend_Search_Lucene_Search_Highlighter_Interface $highlighter)\r
+    {\r
+       /** Skip fields detection. We don't need it, since we expect all fields presented in the HTML body and don't differentiate them */\r
+\r
+        /** Skip exact term matching recognition, keyword fields highlighting is not supported */\r
+\r
+        /** Skip wildcard queries recognition. Supported wildcards are removed by text analyzer */\r
+\r
+        // tokenize phrase using current analyzer and process it as a phrase query\r
+        $tokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($this->_phrase, $this->_phraseEncoding);\r
+\r
+        if (count($tokens) == 0) {\r
+            // Do nothing\r
+            return;\r
+        }\r
+\r
+        if (count($tokens) == 1) {\r
+            $highlighter->highlight($tokens[0]->getTermText());\r
+            return;\r
+        }\r
+\r
+        //It's non-trivial phrase query\r
+        $words = array();\r
+        foreach ($tokens as $token) {\r
+            $words[] = $token->getTermText();\r
+        }\r
+        $highlighter->highlight($words);\r
+    }\r
+\r
+    /**\r
+     * Print a query\r
+     *\r
+     * @return string\r
+     */\r
+    public function __toString()\r
+    {\r
+        // It's used only for query visualisation, so we don't care about characters escaping\r
+        if ($this->_field !== null) {\r
+            $query = $this->_field . ':';\r
+        } else {\r
+               $query = '';\r
+        }\r
+\r
+        $query .= '"' . $this->_phrase . '"';\r
+\r
+        if ($this->_slop != 0) {\r
+            $query .= '~' . $this->_slop;\r
+        }\r
+\r
+        if ($this->getBoost() != 1) {\r
+            $query .= '^' . round($this->getBoost(), 4);\r
+        }\r
+\r
+        return $query;\r
+    }\r
+}\r