예제 #1
0
 /**
  * Removes search entry of current entity from the index.
  * 
  * @return Mage_Lucene_Model_Index_Document_Abstract
  **/
 protected function delete()
 {
     $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
     $query->addTerm(new Zend_Search_Lucene_Index_Term($this->_id, 'entity_id'), true);
     $query->addTerm(new Zend_Search_Lucene_Index_Term($this->getDoctype(), 'doctype'), true);
     $query->addTerm(new Zend_Search_Lucene_Index_Term($this->getStore()->getId(), self::STORE_ATTRIBUTE_CODE), true);
     $this->deleteByQuery($query);
     return $this;
 }
예제 #2
0
 /**
  * Re-write query into primitive queries in the context of specified index
  *
  * @param Zend_Search_Lucene_Interface $index
  * @return Zend_Search_Lucene_Search_Query
  */
 public function rewrite(Zend_Search_Lucene_Interface $index)
 {
     if ($this->_term->field != null) {
         return $this;
     } else {
         $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
         $query->setBoost($this->getBoost());
         foreach ($index->getFieldNames(true) as $fieldName) {
             $term = new Zend_Search_Lucene_Index_Term($this->_term->text, $fieldName);
             $query->addTerm($term);
         }
         return $query->rewrite($index);
     }
 }
예제 #3
0
파일: Term.php 프로젝트: kotow/work
 /**
  * Re-write query into primitive queries in the context of specified index
  *
  * @param Zend_Search_Lucene_Interface $index
  * @return Zend_Search_Lucene_Search_Query
  */
 public function rewrite(Zend_Search_Lucene_Interface $index)
 {
     if ($this->_term->field != null) {
         return $this;
     } else {
         require_once sfConfig::get('sf_lib_dir') . '/modules/search/lib/Lucene/Search/Query/MultiTerm.php';
         $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
         $query->setBoost($this->getBoost());
         require_once sfConfig::get('sf_lib_dir') . '/modules/search/lib/Lucene/Index/Term.php';
         foreach ($index->getFieldNames(true) as $fieldName) {
             $term = new Zend_Search_Lucene_Index_Term($this->_term->text, $fieldName);
             $query->addTerm($term);
         }
         return $query->rewrite($index);
     }
 }
예제 #4
0
파일: Term.php 프로젝트: netixx/Stock
 /**
  * Re-write query into primitive queries in the context of specified index
  *
  * @param Zend_Search_Lucene_Interface $index
  * @return Zend_Search_Lucene_Search_Query
  */
 public function rewrite(Zend_Search_Lucene_Interface $index)
 {
     if ($this->_term->field != null) {
         return $this;
     } else {
         require_once PHP_LIBRARY_PATH . 'Zend/Search/Lucene/Search/Query/MultiTerm.php';
         $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
         $query->setBoost($this->getBoost());
         require_once PHP_LIBRARY_PATH . 'Zend/Search/Lucene/Index/Term.php';
         foreach ($index->getFieldNames(true) as $fieldName) {
             $term = new Zend_Search_Lucene_Index_Term($this->_term->text, $fieldName);
             $query->addTerm($term);
         }
         return $query->rewrite($index);
     }
 }
 /**
  * Assigns the query normalization factor to this.
  *
  * @param float $queryNorm
  */
 public function normalize($queryNorm)
 {
     // incorporate boost
     $queryNorm *= $this->_query->getBoost();
     foreach ($this->_weights as $weight) {
         $weight->normalize($queryNorm);
     }
 }
예제 #6
0
 /**
  * The main workshop page.  It has the list of all the workshops that are 
  * available in the system.
  *
  */
 public function indexAction()
 {
     $this->view->acl = array('workshopList' => $this->_helper->hasAccess('workshop-list'));
     $get = Zend_Registry::get('getFilter');
     $form = new Zend_Form();
     $form->setAttrib('id', 'workshopForm')->setMethod(Zend_Form::METHOD_GET)->setDecorators(array('FormElements', array('HtmlTag', array('tag' => 'div', 'class' => 'filterForm')), 'Form'));
     $searchField = $form->createElement('text', 'search', array('label' => 'workshop-index-index:searchWorkshops'));
     $searchField->setRequired(false)->addFilter('StringTrim')->addFilter('StripTags')->setValue(isset($get->search) ? $get->search : '');
     $category = new Category();
     $categoryList = $category->fetchAll(null, 'name');
     $categories = $form->createElement('select', 'categoryId');
     $categories->addMultiOption('', '-- Search By Category -- ');
     foreach ($categoryList as $c) {
         $categories->addMultiOption($c['categoryId'], $c['name']);
     }
     $categories->setValue(isset($get->categoryId) ? $get->categoryId : '');
     $submit = $form->createElement('submit', 'submitButton', array('label' => 'workshop-index-index:search'));
     $submit->setDecorators(array(array('ViewHelper', array('helper' => 'formSubmit'))));
     $form->addElements(array($searchField, $categories));
     $form->setElementDecorators(array('ViewHelper', 'Errors', array('HtmlTag', array('tag' => 'div', 'class' => 'elm')), array('Label', array('tag' => 'span'))))->addElements(array($submit));
     $this->view->form = $form;
     $searchTerm = new Search_Term();
     $workshops = array();
     if ($get->search != '' || $get->categoryId != 0) {
         $workshop = new Workshop();
         $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
         if ($get->search != '') {
             $query->addTerm(new Zend_Search_Lucene_Index_Term($get->search), true);
         }
         if ($get->categoryId != 0) {
             $query->addTerm(new Zend_Search_Lucene_Index_Term($get->categoryId, 'categoryId'), true);
         }
         $workshops = $workshop->search($query);
         $searchTerm->increment($get->search);
         $this->view->searchTerm = $get->search;
     }
     $this->view->workshops = $workshops;
     $this->view->topTerms = $searchTerm->getTopSearchTerms(10);
     $this->view->layout()->setLayout('search');
     $this->view->layout()->rightContent = $this->view->render('index/top-terms.phtml');
     $this->view->headScript()->appendFile($this->view->baseUrl() . '/scripts/jquery.autocomplete.js');
     $this->view->headLink()->appendStylesheet($this->view->baseUrl() . '/css/jquery.autocomplete.css');
     $this->_helper->pageTitle("workshop-index-index:title");
 }
예제 #7
0
 protected function prepareLuceneQuery($keyword)
 {
     $keyword = strtolower($keyword);
     $query = new Zend_Search_Lucene_Search_Query_Boolean();
     # multiterm query
     $subquery1 = new Zend_Search_Lucene_Search_Query_MultiTerm();
     foreach (explode(' ', $keyword) as $key) {
         if (!trim($key)) {
             continue;
         }
         $subquery1->addTerm(new Zend_Search_Lucene_Index_Term($key));
     }
     # wildcard query
     Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(1);
     $tokens = preg_split('/ /', $keyword, -1, PREG_SPLIT_NO_EMPTY);
     $lastWord = trim(array_pop($tokens)) . "*";
     $pattern = new Zend_Search_Lucene_Index_Term($lastWord);
     $subquery2 = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
     $query->addSubquery($subquery1);
     $query->addSubquery($subquery2);
     return $query;
 }
예제 #8
0
파일: Term.php 프로젝트: ookwudili/chisimba
 /**
  * Transform entry to a subquery
  *
  * @param string $encoding
  * @return Zend_Search_Lucene_Search_Query
  * @throws Zend_Search_Lucene_Search_QueryParserException
  */
 public function getQuery($encoding)
 {
     if ($this->_fuzzyQuery) {
         throw new Zend_Search_Lucene_Search_QueryParserException('Fuzzy search is not supported yet.');
     }
     if (strpos($this->_term, '?') !== false || strpos($this->_term, '*') !== false) {
         throw new Zend_Search_Lucene_Search_QueryParserException('Wildcard queries are not supported yet.');
     }
     $tokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($this->_term, $encoding);
     if (count($tokens) == 0) {
         return new Zend_Search_Lucene_Search_Query_Insignificant();
     }
     if (count($tokens) == 1) {
         $term = new Zend_Search_Lucene_Index_Term($tokens[0]->getTermText(), $this->_field);
         $query = new Zend_Search_Lucene_Search_Query_Term($term);
         $query->setBoost($this->_boost);
         return $query;
     }
     //It's not empty or one term query
     $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
     /**
      * @todo Process $token->getPositionIncrement() to support stemming, synonyms and other
      * analizer design features
      */
     foreach ($tokens as $token) {
         $term = new Zend_Search_Lucene_Index_Term($token->getTermText(), $this->_field);
         $query->addTerm($term, true);
         // all subterms are required
     }
     $query->setBoost($this->_boost);
     return $query;
 }
예제 #9
0
 /**
  * Re-write query into primitive queries in the context of specified index
  *
  * @param Zend_Search_Lucene_Interface $index
  * @return Zend_Search_Lucene_Search_Query
  */
 public function rewrite(Zend_Search_Lucene_Interface $index)
 {
     $this->_matches = array();
     if ($this->_pattern->field === null) {
         // Search through all fields
         $fields = $index->getFieldNames(true);
     } else {
         $fields = array($this->_pattern->field);
     }
     $prefix = self::_getPrefix($this->_pattern->text);
     $prefixLength = strlen($prefix);
     $matchExpression = '/^' . str_replace(array('\\?', '\\*'), array('.', '.*'), preg_quote($this->_pattern->text, '/')) . '$/';
     /** @todo check for PCRE unicode support may be performed through Zend_Environment in some future */
     if (@preg_match('/\\pL/u', 'a') == 1) {
         // PCRE unicode support is turned on
         // add Unicode modifier to the match expression
         $matchExpression .= 'u';
     }
     foreach ($fields as $field) {
         $index->resetTermsStream();
         if ($prefix != '') {
             $index->skipTo(new Zend_Search_Lucene_Index_Term($prefix, $field));
             while ($index->currentTerm() !== null && $index->currentTerm()->field == $field && substr($index->currentTerm()->text, 0, $prefixLength) == $prefix) {
                 if (preg_match($matchExpression, $index->currentTerm()->text) === 1) {
                     $this->_matches[] = $index->currentTerm();
                 }
                 $index->nextTerm();
             }
         } else {
             $index->skipTo(new Zend_Search_Lucene_Index_Term('', $field));
             while ($index->currentTerm() !== null && $index->currentTerm()->field == $field) {
                 if (preg_match($matchExpression, $index->currentTerm()->text) === 1) {
                     $this->_matches[] = $index->currentTerm();
                 }
                 $index->nextTerm();
             }
         }
         $index->closeTermsStream();
     }
     if (count($this->_matches) == 0) {
         return new Zend_Search_Lucene_Search_Query_Empty();
     } else {
         if (count($this->_matches) == 1) {
             return new Zend_Search_Lucene_Search_Query_Term(reset($this->_matches));
         } else {
             $rewrittenQuery = new Zend_Search_Lucene_Search_Query_MultiTerm();
             foreach ($this->_matches as $matchedTerm) {
                 $rewrittenQuery->addTerm($matchedTerm);
             }
             return $rewrittenQuery;
         }
     }
 }
예제 #10
0
 /**
  * Return a list of posts that are similar to the current post.
  * This is not a very good implementation, so do not expect 
  * amazing results - the term vector is not available for a doc
  * in ZSL, which limits how far you can go!
  *
  * @return array ids 
  */
 public function get_similar_posts($post, $max_recommended = 5)
 {
     Zend_Search_Lucene::setResultSetLimit($max_recommended + 1);
     $title = $post->title;
     $tags = $post->tags;
     $tagstring = '';
     foreach ($tags as $tag) {
         $tagstring .= $tag . ' ';
     }
     $analyser = Zend_Search_Lucene_Analysis_Analyzer::getDefault();
     $tokens = $analyser->tokenize(strtolower($tagstring) . ' ' . strtolower($title));
     $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
     foreach ($tokens as $token) {
         $query->addTerm(new Zend_Search_Lucene_Index_Term($token->getTermText()));
     }
     $hits = $this->_index->find($query);
     $ids = array();
     $counter = 0;
     foreach ($hits as $hit) {
         if ($hit->postid != $post->id) {
             $ids[] = $hit->postid;
             $counter++;
         }
         if ($counter == $max_recommended) {
             break;
         }
     }
     return $ids;
 }
 /**
  * Delete an existing document from the index
  *
  * @param integer $id object identifier
  * @param string $language ISO-639-1 code
  * @return void
  */
 public static function deleteByIdLanguage($id, $language)
 {
     // have to use another search object to perform the querying
     $querier = new QubitSearch();
     $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
     $query->addTerm(new Zend_Search_Lucene_Index_Term($id, 'id'), true);
     $query->addTerm(new Zend_Search_Lucene_Index_Term($language, 'culture'), true);
     foreach ($querier->getEngine()->getIndex()->find($query) as $hit) {
         self::getInstance()->getEngine()->getIndex()->delete($hit->id);
     }
 }
예제 #12
0
 /**
  * Create a lucene search from search terms
  * 
  * @param string $searchTerm									Search terms
  * @return \Zend_Search_Lucene_Search_Query						Lucene search query
  */
 public function query($searchTerm)
 {
     // If there are meaningful search terms
     if (strlen(trim($searchTerm))) {
         $query = new \Zend_Search_Lucene_Search_Query_Boolean();
         $termQuery = \Zend_Search_Lucene_Search_QueryParser::parse($searchTerm);
         // Include term based restriction
         $query->addSubquery($termQuery, true);
         // If applicable: Apply language based restriction
         if (\Tollwerk\TwLucenesearch\Utility\Indexer::indexConfig($GLOBALS['TSFE'], 'search.restrictByLanguage')) {
             require_once 'Zend/Search/Lucene/Search/Query/Term.php';
             require_once 'Zend/Search/Lucene/Index/Term.php';
             $query->addSubquery(new \Zend_Search_Lucene_Search_Query_Term(new \Zend_Search_Lucene_Index_Term($GLOBALS['TSFE']->lang, 'language')), true);
         }
         // If applicable: Apply rootline based restriction
         $rootlinePids = \Tollwerk\TwLucenesearch\Utility\Indexer::indexConfig($GLOBALS['TSFE'], 'search.restrictByRootlinePids');
         if (is_array($rootlinePids) && count($rootlinePids)) {
             require_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php';
             require_once 'Zend/Search/Lucene/Index/Term.php';
             $rootlineQuery = new \Zend_Search_Lucene_Search_Query_MultiTerm();
             foreach ($rootlinePids as $rootlinePid) {
                 $rootlineQuery->addTerm(new \Zend_Search_Lucene_Index_Term($rootlinePid, 'rootline'), null);
             }
             $query->addSubquery($rootlineQuery, true);
         }
         return $query;
     }
     return null;
 }
예제 #13
0
 /**
  * Optimize query in the context of specified index
  *
  * @param Zend_Search_Lucene_Interface $index
  * @return Zend_Search_Lucene_Search_Query
  */
 public function optimize(Zend_Search_Lucene_Interface $index)
 {
     $subqueries = array();
     $signs = array();
     // Optimize all subqueries
     foreach ($this->_subqueries as $id => $subquery) {
         $subqueries[] = $subquery->optimize($index);
         $signs[] = $this->_signs === null ? true : $this->_signs[$id];
     }
     // Remove insignificant subqueries
     foreach ($subqueries as $id => $subquery) {
         if ($subquery instanceof Zend_Search_Lucene_Search_Query_Insignificant) {
             // Insignificant subquery has to be removed anyway
             unset($subqueries[$id]);
             unset($signs[$id]);
         }
     }
     if (count($subqueries) == 0) {
         // Boolean query doesn't has non-insignificant subqueries
         return new Zend_Search_Lucene_Search_Query_Insignificant();
     }
     // Check if all non-insignificant subqueries are prohibited
     $allProhibited = true;
     foreach ($signs as $sign) {
         if ($sign !== false) {
             $allProhibited = false;
             break;
         }
     }
     if ($allProhibited) {
         return new Zend_Search_Lucene_Search_Query_Insignificant();
     }
     // Check for empty subqueries
     foreach ($subqueries as $id => $subquery) {
         if ($subquery instanceof Zend_Search_Lucene_Search_Query_Empty) {
             if ($signs[$id] === true) {
                 // Matching is required, but is actually empty
                 return new Zend_Search_Lucene_Search_Query_Empty();
             } else {
                 // Matching is optional or prohibited, but is empty
                 // Remove it from subqueries and signs list
                 unset($subqueries[$id]);
                 unset($signs[$id]);
             }
         }
     }
     // Check, if reduced subqueries list is empty
     if (count($subqueries) == 0) {
         return new Zend_Search_Lucene_Search_Query_Empty();
     }
     // Check if all non-empty subqueries are prohibited
     $allProhibited = true;
     foreach ($signs as $sign) {
         if ($sign !== false) {
             $allProhibited = false;
             break;
         }
     }
     if ($allProhibited) {
         return new Zend_Search_Lucene_Search_Query_Empty();
     }
     // Check, if reduced subqueries list has only one entry
     if (count($subqueries) == 1) {
         // It's a query with only one required or optional clause
         // (it's already checked, that it's not a prohibited clause)
         if ($this->getBoost() == 1) {
             return reset($subqueries);
         }
         $optimizedQuery = clone reset($subqueries);
         $optimizedQuery->setBoost($optimizedQuery->getBoost() * $this->getBoost());
         return $optimizedQuery;
     }
     // Prepare first candidate for optimized query
     $optimizedQuery = new Zend_Search_Lucene_Search_Query_Boolean($subqueries, $signs);
     $optimizedQuery->setBoost($this->getBoost());
     $terms = array();
     $tsigns = array();
     $boostFactors = array();
     // Try to decompose term and multi-term subqueries
     foreach ($subqueries as $id => $subquery) {
         if ($subquery instanceof Zend_Search_Lucene_Search_Query_Term) {
             $terms[] = $subquery->getTerm();
             $tsigns[] = $signs[$id];
             $boostFactors[] = $subquery->getBoost();
             // remove subquery from a subqueries list
             unset($subqueries[$id]);
             unset($signs[$id]);
         } else {
             if ($subquery instanceof Zend_Search_Lucene_Search_Query_MultiTerm) {
                 $subTerms = $subquery->getTerms();
                 $subSigns = $subquery->getSigns();
                 if ($signs[$id] === true) {
                     // It's a required multi-term subquery.
                     // Something like '... +(+term1 -term2 term3 ...) ...'
                     // Multi-term required subquery can be decomposed only if it contains
                     // required terms and doesn't contain prohibited terms:
                     // ... +(+term1 term2 ...) ... => ... +term1 term2 ...
                     //
                     // Check this
                     $hasRequired = false;
                     $hasProhibited = false;
                     if ($subSigns === null) {
                         // All subterms are required
                         $hasRequired = true;
                     } else {
                         foreach ($subSigns as $sign) {
                             if ($sign === true) {
                                 $hasRequired = true;
                             } else {
                                 if ($sign === false) {
                                     $hasProhibited = true;
                                     break;
                                 }
                             }
                         }
                     }
                     // Continue if subquery has prohibited terms or doesn't have required terms
                     if ($hasProhibited || !$hasRequired) {
                         continue;
                     }
                     foreach ($subTerms as $termId => $term) {
                         $terms[] = $term;
                         $tsigns[] = $subSigns === null ? true : $subSigns[$termId];
                         $boostFactors[] = $subquery->getBoost();
                     }
                     // remove subquery from a subqueries list
                     unset($subqueries[$id]);
                     unset($signs[$id]);
                 } else {
                     // $signs[$id] === null  ||  $signs[$id] === false
                     // It's an optional or prohibited multi-term subquery.
                     // Something like '... (+term1 -term2 term3 ...) ...'
                     // or
                     // something like '... -(+term1 -term2 term3 ...) ...'
                     // Multi-term optional and required subqueries can be decomposed
                     // only if all terms are optional.
                     //
                     // Check if all terms are optional.
                     $onlyOptional = true;
                     if ($subSigns === null) {
                         // All subterms are required
                         $onlyOptional = false;
                     } else {
                         foreach ($subSigns as $sign) {
                             if ($sign !== null) {
                                 $onlyOptional = false;
                                 break;
                             }
                         }
                     }
                     // Continue if non-optional terms are presented in this multi-term subquery
                     if (!$onlyOptional) {
                         continue;
                     }
                     foreach ($subTerms as $termId => $term) {
                         $terms[] = $term;
                         $tsigns[] = $signs[$id] === null ? null : false;
                         $boostFactors[] = $subquery->getBoost();
                     }
                     // remove subquery from a subqueries list
                     unset($subqueries[$id]);
                     unset($signs[$id]);
                 }
             }
         }
     }
     // Check, if there are no decomposed subqueries
     if (count($terms) == 0) {
         // return prepared candidate
         return $optimizedQuery;
     }
     // Check, if all subqueries have been decomposed and all terms has the same boost factor
     if (count($subqueries) == 0 && count(array_unique($boostFactors)) == 1) {
         $optimizedQuery = new Zend_Search_Lucene_Search_Query_MultiTerm($terms, $tsigns);
         $optimizedQuery->setBoost(reset($boostFactors) * $this->getBoost());
         return $optimizedQuery;
     }
     // This boolean query can't be transformed to Term/MultiTerm query and still contains
     // several subqueries
     // Separate prohibited terms
     $prohibitedTerms = array();
     foreach ($terms as $id => $term) {
         if ($tsigns[$id] === false) {
             $prohibitedTerms[] = $term;
             unset($terms[$id]);
             unset($tsigns[$id]);
             unset($boostFactors[$id]);
         }
     }
     if (count($terms) == 1) {
         $clause = new Zend_Search_Lucene_Search_Query_Term(reset($terms));
         $clause->setBoost(reset($boostFactors));
         $subqueries[] = $clause;
         $signs[] = reset($tsigns);
         // Clear terms list
         $terms = array();
     } else {
         if (count($terms) > 1 && count(array_unique($boostFactors)) == 1) {
             $clause = new Zend_Search_Lucene_Search_Query_MultiTerm($terms, $tsigns);
             $clause->setBoost(reset($boostFactors));
             $subqueries[] = $clause;
             // Clause sign is 'required' if clause contains required terms. 'Optional' otherwise.
             $signs[] = in_array(true, $tsigns) ? true : null;
             // Clear terms list
             $terms = array();
         }
     }
     if (count($prohibitedTerms) == 1) {
         // (boost factors are not significant for prohibited clauses)
         $subqueries[] = new Zend_Search_Lucene_Search_Query_Term(reset($prohibitedTerms));
         $signs[] = false;
         // Clear prohibited terms list
         $prohibitedTerms = array();
     } else {
         if (count($prohibitedTerms) > 1) {
             // prepare signs array
             $prohibitedSigns = array();
             foreach ($prohibitedTerms as $id => $term) {
                 // all prohibited term are grouped as optional into multi-term query
                 $prohibitedSigns[$id] = null;
             }
             // (boost factors are not significant for prohibited clauses)
             $subqueries[] = new Zend_Search_Lucene_Search_Query_MultiTerm($prohibitedTerms, $prohibitedSigns);
             // Clause sign is 'prohibited'
             $signs[] = false;
             // Clear terms list
             $prohibitedTerms = array();
         }
     }
     /** @todo Group terms with the same boost factors together */
     // Check, that all terms are processed
     // Replace candidate for optimized query
     if (count($terms) == 0 && count($prohibitedTerms) == 0) {
         $optimizedQuery = new Zend_Search_Lucene_Search_Query_Boolean($subqueries, $signs);
         $optimizedQuery->setBoost($this->getBoost());
     }
     return $optimizedQuery;
 }
예제 #14
0
 /**
  * Re-write query into primitive queries in the context of specified index
  *
  * @param Zend_Search_Lucene_Interface $index
  * @return Zend_Search_Lucene_Search_Query
  */
 public function rewrite(Zend_Search_Lucene_Interface $index)
 {
     $this->_matches = array();
     if ($this->_field === null) {
         // Search through all fields
         $fields = $index->getFieldNames(true);
     } else {
         $fields = array($this->_field);
     }
     // require_once 'Zend/Search/Lucene.php';
     $maxTerms = Zend_Search_Lucene::getTermsPerQueryLimit();
     foreach ($fields as $field) {
         $index->resetTermsStream();
         // require_once 'Zend/Search/Lucene/Index/Term.php';
         if ($this->_lowerTerm !== null) {
             $lowerTerm = new Zend_Search_Lucene_Index_Term($this->_lowerTerm->text, $field);
             $index->skipTo($lowerTerm);
             if (!$this->_inclusive && $index->currentTerm() == $lowerTerm) {
                 // Skip lower term
                 $index->nextTerm();
             }
         } else {
             $index->skipTo(new Zend_Search_Lucene_Index_Term('', $field));
         }
         if ($this->_upperTerm !== null) {
             // Walk up to the upper term
             $upperTerm = new Zend_Search_Lucene_Index_Term($this->_upperTerm->text, $field);
             while ($index->currentTerm() !== null && $index->currentTerm()->field == $field && strcmp($index->currentTerm()->text, $upperTerm->text) < 0) {
                 $this->_matches[] = $index->currentTerm();
                 if ($maxTerms != 0 && count($this->_matches) > $maxTerms) {
                     // require_once 'Zend/Search/Lucene/Exception.php';
                     throw new Zend_Search_Lucene_Exception('Terms per query limit is reached.');
                 }
                 $index->nextTerm();
             }
             if ($this->_inclusive && $index->currentTerm() == $upperTerm) {
                 // Include upper term into result
                 $this->_matches[] = $upperTerm;
             }
         } else {
             // Walk up to the end of field data
             while ($index->currentTerm() !== null && $index->currentTerm()->field == $field) {
                 $this->_matches[] = $index->currentTerm();
                 if ($maxTerms != 0 && count($this->_matches) > $maxTerms) {
                     // require_once 'Zend/Search/Lucene/Exception.php';
                     throw new Zend_Search_Lucene_Exception('Terms per query limit is reached.');
                 }
                 $index->nextTerm();
             }
         }
         $index->closeTermsStream();
     }
     if (count($this->_matches) == 0) {
         // require_once 'Zend/Search/Lucene/Search/Query/Empty.php';
         return new Zend_Search_Lucene_Search_Query_Empty();
     } else {
         if (count($this->_matches) == 1) {
             // require_once 'Zend/Search/Lucene/Search/Query/Term.php';
             return new Zend_Search_Lucene_Search_Query_Term(reset($this->_matches));
         } else {
             // require_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php';
             $rewrittenQuery = new Zend_Search_Lucene_Search_Query_MultiTerm();
             foreach ($this->_matches as $matchedTerm) {
                 $rewrittenQuery->addTerm($matchedTerm);
             }
             return $rewrittenQuery;
         }
     }
 }
예제 #15
0
 /**
  * add_to_index function
  *
  * deletes any exising index entries, and adds a document to the index
  *
  * @TODO Is this working properly
  * @param mixed $Model
  * @param mixed $id
  * @access public
  * @return void
  */
 function add_to_index(&$Model, $id)
 {
     if (!empty($this->settings[$Model->alias])) {
         if (!$this->open_index($Model)) {
             return false;
         }
         try {
             $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
             $query->addTerm(new Zend_Search_Lucene_Index_Term($id, 'cake_id'), true);
             $query->addTerm(new Zend_Search_Lucene_Index_Term($Model->alias, 'cake_model'), true);
             $Hits = $this->Index->find($query);
             // TODO there are never any hits - why
             if (count($Hits)) {
                 $this->delete_from_index($Model, $id);
             }
             if (method_exists($Model, 'find_index')) {
                 $result = $Model->find_index('first', array('conditions' => array($Model->alias . '.id' => $id)));
             } else {
                 $result = $Model->find('first', array('conditions' => array($Model->alias . '.id' => $id)));
             }
             if (!empty($result)) {
                 $doc = new Zend_Search_Lucene_Document();
                 // add the model field
                 $doc->addField(Zend_Search_Lucene_Field::Keyword('cake_model', $Model->alias, 'utf-8'));
                 foreach ($this->settings[$Model->alias]['fields'] as $field_name => $options) {
                     if (!empty($options['prepare']) && function_exists($options['prepare'])) {
                         $result[$Model->alias][$field_name] = call_user_func($options['prepare'], $result[$Model->alias][$field_name]);
                     }
                     $alias = !empty($options['alias']) ? $options['alias'] : $field_name;
                     $doc->addField(Zend_Search_Lucene_Field::$options['type']($alias, $result[$Model->alias][$field_name], 'utf-8'));
                 }
                 $this->Index->addDocument($doc);
                 $this->Index->commit();
                 if (Configure::read()) {
                     $Hits = $this->Index->find($query);
                     if (!count($Hits)) {
                         // TODO why isn't it possible to find what was just added
                         $this->log('Tried to add to the search index, no errors but couldnt find what was just added!', 'searchable');
                     }
                     $this->log('added to index id:' . $id, 'searchable');
                 }
             }
         } catch (Zend_Search_Lucene_Exception $e) {
             $this->log('Lucene exception:' . $e->getMessage(), 'searchable');
         }
     }
 }
 private function addLanguageQuery($query)
 {
     if (!empty($this->searchLanguage)) {
         $languageQuery = new \Zend_Search_Lucene_Search_Query_MultiTerm();
         $languageQuery->addTerm(new Zend_Search_Lucene_Index_Term('all', 'lang'));
         if (is_object($this->searchLanguage)) {
             $lang = $this->searchLanguage->toString();
         } else {
             $lang = $this->searchLanguage;
         }
         $lang = str_replace(array('_', '-'), '', $lang);
         $languageQuery->addTerm(new Zend_Search_Lucene_Index_Term($lang, 'lang'));
         $query->addSubquery($languageQuery, TRUE);
     }
     return $query;
 }
try {
    $criteria->addField(new Foo());
    $t->fail('->addField() rejects invalid values');
} catch (Exception $e) {
    $t->pass('->addField() rejects invalid values');
}
$t->diag('testing addMultiTerm()');
$s = inst()->addMultiTerm(range(1, 10), 'foo')->getQuery()->getSubqueries();
$q = new Zend_Search_Lucene_Search_Query_MultiTerm();
foreach (range(1, 10) as $value) {
    $q->addTerm(new Zend_Search_Lucene_Index_Term($value, 'foo'));
}
$t->ok($s[0] == $q, '->addMultiTerm() registers the correct query');
try {
    $s = inst()->addMultiTerm('bar', 'foo')->getQuery()->getSubqueries();
    $q = new Zend_Search_Lucene_Search_Query_MultiTerm();
    $q->addTerm(new Zend_Search_Lucene_Index_Term('bar', 'foo'));
    $t->ok($s[0] == $q, '->addMultiTerm() registers and accepts a string value');
} catch (Exception $e) {
    $t->fail('->addMultiTerm() registers and accepts a string value');
}
$t->diag('testing addWildcard()');
$s = inst()->addWildcard('foo*', 'bar')->getQuery()->getSubqueries();
$q = new Zend_Search_Lucene_Search_Query_Wildcard(new Zend_Search_Lucene_Index_Term('foo*', 'bar'));
$t->ok($s[0] == $q, '->addWildcard() registers the correct query with mutlitple character wildcards');
$s = inst()->addWildcard('f?o', 'bar')->getQuery()->getSubqueries();
$q = new Zend_Search_Lucene_Search_Query_Wildcard(new Zend_Search_Lucene_Index_Term('f?o', 'bar'));
$t->ok($s[0] == $q, '->addWildcard() registers the correct query with single character wildcards');
$s = inst()->addWildcard('foo* baz?', 'bar')->getQuery()->getSubqueries();
$q = new Zend_Search_Lucene_Search_Query_Wildcard(new Zend_Search_Lucene_Index_Term('foo* baz?', 'bar'));
$t->ok($s[0] == $q, '->addWildcard() registers the correct query with mixing character wildcards');
 /**
  * Adds a multiterm query.
  * 
  * @return sfLuceneCriteria
  */
 public function addMultiTerm($values, $field = null, $matchType = null, $type = true)
 {
     if (!is_array($values)) {
         $values = array($values);
     }
     $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
     foreach ($values as $value) {
         $query->addTerm(new Zend_Search_Lucene_Index_Term($value, $field), $matchType);
     }
     return $this->add($query, $type);
 }
예제 #19
0
 /**
  * Transform entry to a subquery
  *
  * @param string $encoding
  * @return Zend_Search_Lucene_Search_Query
  * @throws Zend_Search_Lucene_Search_QueryParserException
  */
 public function getQuery($encoding)
 {
     if (strpos($this->_term, '?') !== false || strpos($this->_term, '*') !== false) {
         if ($this->_fuzzyQuery) {
             // require_once 'Zend/Search/Lucene/Search/QueryParserException.php';
             throw new Zend_Search_Lucene_Search_QueryParserException('Fuzzy search is not supported for terms with wildcards.');
         }
         $pattern = '';
         $subPatterns = explode('*', $this->_term);
         $astericFirstPass = true;
         foreach ($subPatterns as $subPattern) {
             if (!$astericFirstPass) {
                 $pattern .= '*';
             } else {
                 $astericFirstPass = false;
             }
             $subPatternsL2 = explode('?', $subPattern);
             $qMarkFirstPass = true;
             foreach ($subPatternsL2 as $subPatternL2) {
                 if (!$qMarkFirstPass) {
                     $pattern .= '?';
                 } else {
                     $qMarkFirstPass = false;
                 }
                 $tokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($subPatternL2, $encoding);
                 if (count($tokens) > 1) {
                     // require_once 'Zend/Search/Lucene/Search/QueryParserException.php';
                     throw new Zend_Search_Lucene_Search_QueryParserException('Wildcard search is supported only for non-multiple word terms');
                 }
                 foreach ($tokens as $token) {
                     $pattern .= $token->getTermText();
                 }
             }
         }
         $term = new Zend_Search_Lucene_Index_Term($pattern, $this->_field);
         $query = new Zend_Search_Lucene_Search_Query_Wildcard($term);
         $query->setBoost($this->_boost);
         return $query;
     }
     $tokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($this->_term, $encoding);
     if (count($tokens) == 0) {
         return new Zend_Search_Lucene_Search_Query_Insignificant();
     }
     if (count($tokens) == 1 && !$this->_fuzzyQuery) {
         $term = new Zend_Search_Lucene_Index_Term($tokens[0]->getTermText(), $this->_field);
         $query = new Zend_Search_Lucene_Search_Query_Term($term);
         $query->setBoost($this->_boost);
         return $query;
     }
     if (count($tokens) == 1 && $this->_fuzzyQuery) {
         $term = new Zend_Search_Lucene_Index_Term($tokens[0]->getTermText(), $this->_field);
         $query = new Zend_Search_Lucene_Search_Query_Fuzzy($term, $this->_similarity);
         $query->setBoost($this->_boost);
         return $query;
     }
     if ($this->_fuzzyQuery) {
         // require_once 'Zend/Search/Lucene/Search/QueryParserException.php';
         throw new Zend_Search_Lucene_Search_QueryParserException('Fuzzy search is supported only for non-multiple word terms');
     }
     //It's not empty or one term query
     $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
     /**
      * @todo Process $token->getPositionIncrement() to support stemming, synonyms and other
      * analizer design features
      */
     foreach ($tokens as $token) {
         $term = new Zend_Search_Lucene_Index_Term($token->getTermText(), $this->_field);
         $query->addTerm($term, true);
         // all subterms are required
     }
     $query->setBoost($this->_boost);
     return $query;
 }
예제 #20
0
 /**
  * Re-write query into primitive queries in the context of specified index
  *
  * @param Zend_Search_Lucene_Interface $index
  * @return Zend_Search_Lucene_Search_Query
  */
 public function rewrite(Zend_Search_Lucene_Interface $index)
 {
     $this->_matches = array();
     if ($this->_field === null) {
         // Search through all fields
         $fields = $index->getFieldNames(true);
     } else {
         $fields = array($this->_field);
     }
     foreach ($fields as $field) {
         $index->resetTermsStream();
         if ($this->_lowerTerm !== null) {
             $lowerTerm = new Zend_Search_Lucene_Index_Term($this->_lowerTerm->text, $field);
             $index->skipTo($lowerTerm);
             if (!$this->_inclusive && $index->currentTerm() == $lowerTerm) {
                 // Skip lower term
                 $index->nextTerm();
             }
         } else {
             $index->skipTo(new Zend_Search_Lucene_Index_Term('', $field));
         }
         if ($this->_upperTerm !== null) {
             // Walk up to the upper term
             $upperTerm = new Zend_Search_Lucene_Index_Term($this->_upperTerm->text, $field);
             while ($index->currentTerm() !== null && $index->currentTerm()->field == $field && $index->currentTerm()->text < $upperTerm->text) {
                 $this->_matches[] = $index->currentTerm();
                 $index->nextTerm();
             }
             if ($this->_inclusive && $index->currentTerm() == $upperTerm) {
                 // Include upper term into result
                 $this->_matches[] = $upperTerm;
             }
         } else {
             // Walk up to the end of field data
             while ($index->currentTerm() !== null && $index->currentTerm()->field == $field) {
                 $this->_matches[] = $index->currentTerm();
                 $index->nextTerm();
             }
         }
         $index->closeTermsStream();
     }
     if (count($this->_matches) == 0) {
         return new Zend_Search_Lucene_Search_Query_Empty();
     } else {
         if (count($this->_matches) == 1) {
             return new Zend_Search_Lucene_Search_Query_Term(reset($this->_matches));
         } else {
             $rewrittenQuery = new Zend_Search_Lucene_Search_Query_MultiTerm();
             foreach ($this->_matches as $matchedTerm) {
                 $rewrittenQuery->addTerm($matchedTerm);
             }
             return $rewrittenQuery;
         }
     }
 }
 /**
  * Parses a query string
  *
  * @param string $strQuery
  * @param string $encoding
  * @return Zend_Search_Lucene_Search_Query
  * @throws Zend_Search_Lucene_Search_QueryParserException
  */
 public static function parse($strQuery, $encoding = null)
 {
     self::_getInstance();
     // Reset FSM if previous parse operation didn't return it into a correct state
     self::$_instance->reset();
     require_once 'Zend/Search/Lucene/Search/QueryParserException.php';
     try {
         require_once 'Zend/Search/Lucene/Search/QueryParserContext.php';
         self::$_instance->_encoding = $encoding !== null ? $encoding : self::$_instance->_defaultEncoding;
         self::$_instance->_lastToken = null;
         self::$_instance->_context = new Zend_Search_Lucene_Search_QueryParserContext(self::$_instance->_encoding);
         self::$_instance->_contextStack = array();
         self::$_instance->_tokens = self::$_instance->_lexer->tokenize($strQuery, self::$_instance->_encoding);
         // Empty query
         if (count(self::$_instance->_tokens) == 0) {
             require_once 'Zend/Search/Lucene/Search/Query/Insignificant.php';
             return new Zend_Search_Lucene_Search_Query_Insignificant();
         }
         foreach (self::$_instance->_tokens as $token) {
             try {
                 self::$_instance->_currentToken = $token;
                 self::$_instance->process($token->type);
                 self::$_instance->_lastToken = $token;
             } catch (Exception $e) {
                 if (strpos($e->getMessage(), 'There is no any rule for') !== false) {
                     throw new Zend_Search_Lucene_Search_QueryParserException('Syntax error at char position ' . $token->position . '.', 0, $e);
                 }
                 require_once 'Zend/Search/Lucene/Exception.php';
                 throw new Zend_Search_Lucene_Exception($e->getMessage(), $e->getCode(), $e);
             }
         }
         if (count(self::$_instance->_contextStack) != 0) {
             throw new Zend_Search_Lucene_Search_QueryParserException('Syntax Error: mismatched parentheses, every opening must have closing.');
         }
         return self::$_instance->_context->getQuery();
     } catch (Zend_Search_Lucene_Search_QueryParserException $e) {
         if (self::$_instance->_suppressQueryParsingExceptions) {
             $queryTokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($strQuery, self::$_instance->_encoding);
             require_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php';
             $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
             $termsSign = self::$_instance->_defaultOperator == self::B_AND ? true : null;
             require_once 'Zend/Search/Lucene/Index/Term.php';
             foreach ($queryTokens as $token) {
                 $query->addTerm(new Zend_Search_Lucene_Index_Term($token->getTermText()), $termsSign);
             }
             return $query;
         } else {
             require_once 'Zend/Search/Lucene/Exception.php';
             throw new Zend_Search_Lucene_Exception($e->getMessage(), $e->getCode(), $e);
         }
     }
 }
예제 #22
0
 /**
  * Optimize query in the context of specified index
  *
  * @param Zend_Search_Lucene_Interface $index
  * @return Zend_Search_Lucene_Search_Query
  */
 public function optimize(Zend_Search_Lucene_Interface $index)
 {
     $terms = $this->_terms;
     $signs = $this->_signs;
     foreach ($terms as $id => $term) {
         if (!$index->hasTerm($term)) {
             if ($signs === null || $signs[$id] === true) {
                 // Term is required
                 return new Zend_Search_Lucene_Search_Query_Empty();
             } else {
                 // Term is optional or prohibited
                 // Remove it from terms and signs list
                 unset($terms[$id]);
                 unset($signs[$id]);
             }
         }
     }
     // Check if all presented terms are prohibited
     $allProhibited = true;
     if ($signs === null) {
         $allProhibited = false;
     } else {
         foreach ($signs as $sign) {
             if ($sign !== false) {
                 $allProhibited = false;
                 break;
             }
         }
     }
     if ($allProhibited) {
         return new Zend_Search_Lucene_Search_Query_Empty();
     }
     /**
      * @todo make an optimization for repeated terms
      * (they may have different signs)
      */
     if (count($terms) == 1) {
         // It's already checked, that it's not a prohibited term
         // It's one term query with one required or optional element
         $optimizedQuery = new Zend_Search_Lucene_Search_Query_Term(reset($terms));
         $optimizedQuery->setBoost($this->getBoost());
         return $optimizedQuery;
     }
     if (count($terms) == 0) {
         return new Zend_Search_Lucene_Search_Query_Empty();
     }
     $optimizedQuery = new Zend_Search_Lucene_Search_Query_MultiTerm($terms, $signs);
     $optimizedQuery->setBoost($this->getBoost());
     return $optimizedQuery;
 }
예제 #23
0
파일: Term.php 프로젝트: be-dmitry/zf1
 /**
  * Re-write query into primitive queries in the context of specified index
  *
  * @param Zend_Search_Lucene_Interface $index
  * @return Zend_Search_Lucene_Search_Query
  */
 public function rewrite(Zend_Search_Lucene_Interface $index)
 {
     if ($this->_field === null) {
         require_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php';
         $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
         $query->setBoost($this->getBoost());
         $hasInsignificantSubqueries = false;
         require_once 'Zend/Search/Lucene.php';
         if (Zend_Search_Lucene::getDefaultSearchField() === null) {
             $searchFields = $index->getFieldNames(true);
         } else {
             $searchFields = array(Zend_Search_Lucene::getDefaultSearchField());
         }
         require_once 'Zend/Search/Lucene/Search/Query/Preprocessing/Term.php';
         foreach ($searchFields as $fieldName) {
             $subquery = new Zend_Search_Lucene_Search_Query_Preprocessing_Term($this->_word, $this->_encoding, $fieldName);
             $rewrittenSubquery = $subquery->rewrite($index);
             foreach ($rewrittenSubquery->getQueryTerms() as $term) {
                 $query->addTerm($term);
             }
             if ($rewrittenSubquery instanceof Zend_Search_Lucene_Search_Query_Insignificant) {
                 $hasInsignificantSubqueries = true;
             }
         }
         if (count($query->getTerms()) == 0) {
             $this->_matches = array();
             if ($hasInsignificantSubqueries) {
                 require_once 'Zend/Search/Lucene/Search/Query/Insignificant.php';
                 return new Zend_Search_Lucene_Search_Query_Insignificant();
             } else {
                 require_once 'Zend/Search/Lucene/Search/Query/Empty.php';
                 return new Zend_Search_Lucene_Search_Query_Empty();
             }
         }
         $this->_matches = $query->getQueryTerms();
         return $query;
     }
     // -------------------------------------
     // Recognize exact term matching (it corresponds to Keyword fields stored in the index)
     // encoding is not used since we expect binary matching
     require_once 'Zend/Search/Lucene/Index/Term.php';
     $term = new Zend_Search_Lucene_Index_Term($this->_word, $this->_field);
     if ($index->hasTerm($term)) {
         require_once 'Zend/Search/Lucene/Search/Query/Term.php';
         $query = new Zend_Search_Lucene_Search_Query_Term($term);
         $query->setBoost($this->getBoost());
         $this->_matches = $query->getQueryTerms();
         return $query;
     }
     // -------------------------------------
     // Recognize wildcard queries
     /** @todo check for PCRE unicode support may be performed through Zend_Environment in some future */
     if (@preg_match('/\\pL/u', 'a') == 1) {
         $word = iconv($this->_encoding, 'UTF-8', $this->_word);
         $wildcardsPattern = '/[*?]/u';
         $subPatternsEncoding = 'UTF-8';
     } else {
         $word = $this->_word;
         $wildcardsPattern = '/[*?]/';
         $subPatternsEncoding = $this->_encoding;
     }
     $subPatterns = preg_split($wildcardsPattern, $word, -1, PREG_SPLIT_OFFSET_CAPTURE);
     if (count($subPatterns) > 1) {
         // Wildcard query is recognized
         $pattern = '';
         require_once 'Zend/Search/Lucene/Analysis/Analyzer.php';
         foreach ($subPatterns as $id => $subPattern) {
             // Append corresponding wildcard character to the pattern before each sub-pattern (except first)
             if ($id != 0) {
                 $pattern .= $word[$subPattern[1] - 1];
             }
             // Check if each subputtern is a single word in terms of current analyzer
             $tokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($subPattern[0], $subPatternsEncoding);
             if (count($tokens) > 1) {
                 require_once 'Zend/Search/Lucene/Search/QueryParserException.php';
                 throw new Zend_Search_Lucene_Search_QueryParserException('Wildcard search is supported only for non-multiple word terms');
             }
             foreach ($tokens as $token) {
                 $pattern .= $token->getTermText();
             }
         }
         require_once 'Zend/Search/Lucene/Index/Term.php';
         $term = new Zend_Search_Lucene_Index_Term($pattern, $this->_field);
         require_once 'Zend/Search/Lucene/Search/Query/Wildcard.php';
         $query = new Zend_Search_Lucene_Search_Query_Wildcard($term);
         $query->setBoost($this->getBoost());
         // Get rewritten query. Important! It also fills terms matching container.
         $rewrittenQuery = $query->rewrite($index);
         $this->_matches = $query->getQueryTerms();
         return $rewrittenQuery;
     }
     // -------------------------------------
     // Recognize one-term multi-term and "insignificant" queries
     require_once 'Zend/Search/Lucene/Analysis/Analyzer.php';
     $tokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($this->_word, $this->_encoding);
     if (count($tokens) == 0) {
         $this->_matches = array();
         require_once 'Zend/Search/Lucene/Search/Query/Insignificant.php';
         return new Zend_Search_Lucene_Search_Query_Insignificant();
     }
     if (count($tokens) == 1) {
         require_once 'Zend/Search/Lucene/Index/Term.php';
         $term = new Zend_Search_Lucene_Index_Term($tokens[0]->getTermText(), $this->_field);
         require_once 'Zend/Search/Lucene/Search/Query/Term.php';
         $query = new Zend_Search_Lucene_Search_Query_Term($term);
         $query->setBoost($this->getBoost());
         $this->_matches = $query->getQueryTerms();
         return $query;
     }
     //It's not insignificant or one term query
     require_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php';
     $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
     /**
      * @todo Process $token->getPositionIncrement() to support stemming, synonyms and other
      * analizer design features
      */
     require_once 'Zend/Search/Lucene/Index/Term.php';
     foreach ($tokens as $token) {
         $term = new Zend_Search_Lucene_Index_Term($token->getTermText(), $this->_field);
         $query->addTerm($term, true);
         // all subterms are required
     }
     $query->setBoost($this->getBoost());
     $this->_matches = $query->getQueryTerms();
     return $query;
 }
예제 #24
0
 /**
  * Creates a Lucene query object from a list of values.
  *
  * @param string $name Translated name of the variable or column
  * @param integer $type Type constant
  * @return Zend_Search_Lucene_Search_Query Lucene query object
  */
 protected function _createListTerm($name, $type)
 {
     $sign = null;
     switch ($this->getOperator()) {
         case '!=':
             $sign = false;
         case '==':
             $multiterm = new Zend_Search_Lucene_Search_Query_MultiTerm();
             foreach ($this->getValue() as $value) {
                 $escaped = $this->_escape($this->getOperator(), SORT_STRING, $value);
                 $term = new Zend_Search_Lucene_Index_Term($this->_empty($escaped), $name);
                 $multiterm->addTerm($term, $sign);
             }
             return $multiterm;
         case '~=':
             $query = new Zend_Search_Lucene_Search_Query_Boolean();
             foreach ($this->getValue() as $value) {
                 $escaped = $this->_escape($this->getOperator(), SORT_STRING, $value);
                 $term = new Zend_Search_Lucene_Index_Term(strtolower($this->_empty($escaped)) . '*', $name);
                 $query->addSubquery(new Zend_Search_Lucene_Search_Query_Wildcard($term));
             }
             return $query;
     }
 }
예제 #25
0
 /**
  * Transform entry to a subquery
  *
  * @param string $encoding
  * @return Zend_Search_Lucene_Search_Query
  * @throws Zend_Search_Lucene_Search_QueryParserException
  */
 public function getQuery($encoding)
 {
     if (strpos($this->_term, '?') !== false || strpos($this->_term, '*') !== false) {
         if ($this->_fuzzyQuery) {
             require_once __CA_LIB_DIR__ . '/core/Zend/Search/Lucene/Search/QueryParserException.php';
             throw new Zend_Search_Lucene_Search_QueryParserException('Fuzzy search is not supported for terms with wildcards.');
         }
         $pattern = '';
         $subPatterns = explode('*', $this->_term);
         $astericFirstPass = true;
         foreach ($subPatterns as $subPattern) {
             if (!$astericFirstPass) {
                 $pattern .= '*';
             } else {
                 $astericFirstPass = false;
             }
             $subPatternsL2 = explode('?', $subPattern);
             $qMarkFirstPass = true;
             foreach ($subPatternsL2 as $subPatternL2) {
                 if (!$qMarkFirstPass) {
                     $pattern .= '?';
                 } else {
                     $qMarkFirstPass = false;
                 }
                 $pattern .= $subPatternL2;
             }
         }
         $term = new Zend_Search_Lucene_Index_Term(strtolower($pattern), $this->_field);
         $query = new Zend_Search_Lucene_Search_Query_Wildcard($term);
         $query->setBoost($this->_boost);
         return $query;
     }
     $tokens = explode(" ", $this->_term);
     if (count($tokens) == 0) {
         return new Zend_Search_Lucene_Search_Query_Insignificant();
     }
     if (count($tokens) == 1 && !$this->_fuzzyQuery) {
         $term = new Zend_Search_Lucene_Index_Term(strtolower($tokens[0]), $this->_field);
         $query = new Zend_Search_Lucene_Search_Query_Term($term);
         $query->setBoost($this->_boost);
         return $query;
     }
     if (count($tokens) == 1 && $this->_fuzzyQuery) {
         $term = new Zend_Search_Lucene_Index_Term(strtolower($tokens[0]), $this->_field);
         $query = new Zend_Search_Lucene_Search_Query_Fuzzy($term, $this->_similarity);
         $query->setBoost($this->_boost);
         return $query;
     }
     if ($this->_fuzzyQuery) {
         require_once __CA_LIB_DIR__ . '/core/Zend/Search/Lucene/Search/QueryParserException.php';
         throw new Zend_Search_Lucene_Search_QueryParserException('Fuzzy search is supported only for non-multiple word terms');
     }
     //It's not empty or one term query
     $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
     foreach ($tokens as $token) {
         $term = new Zend_Search_Lucene_Index_Term(strtolower($token), $this->_field);
         $query->addTerm($term, true);
     }
     $query->setBoost($this->_boost);
     return $query;
 }
예제 #26
0
파일: index.php 프로젝트: Tony133/zf-web
<?php

require_once '/var/www/library/Zend/Search/Lucene.php';
Zend_Search_Lucene::create('/tmp/index');
// Doesn't work when including a number:
$searchTerm = '12stones';
$index = Zend_Search_Lucene::open('/tmp/index');
$document = new Zend_Search_Lucene_Document();
$document->addField(Zend_Search_Lucene_Field::Text('test', $searchTerm));
$index->addDocument($document);
$index->commit();
$query = new Zend_Search_Lucene_Search_Query_MultiTerm();
$query->addTerm(new Zend_Search_Lucene_Index_Term($searchTerm, 'test'), true);
$hits = $index->find($query);
echo 'Hits:';
// For my use case, this should return one hit, however, it currently returns none:
foreach ($hits as $hit) {
    var_dump($hit->test);
}
예제 #27
0
파일: Wildcard.php 프로젝트: netvlies/zf
 /**
  * Re-write query into primitive queries in the context of specified index
  *
  * @param Zend_Search_Lucene_Interface $index
  * @return Zend_Search_Lucene_Search_Query
  * @throws Zend_Search_Lucene_Exception
  */
 public function rewrite(Zend_Search_Lucene_Interface $index)
 {
     $this->_matches = array();
     if ($this->_pattern->field === null) {
         // Search through all fields
         $fields = $index->getFieldNames(true);
     } else {
         $fields = array($this->_pattern->field);
     }
     $prefix = self::_getPrefix($this->_pattern->text);
     $prefixLength = strlen($prefix);
     $matchExpression = '/^' . str_replace(array('\\?', '\\*'), array('.', '.*'), preg_quote($this->_pattern->text, '/')) . '$/';
     if ($prefixLength < self::$_minPrefixLength) {
         // require_once 'Zend/Search/Lucene/Exception.php';
         throw new Zend_Search_Lucene_Exception('At least ' . self::$_minPrefixLength . ' non-wildcard characters are required at the beginning of pattern.');
     }
     /** @todo check for PCRE unicode support may be performed through Zend_Environment in some future */
     if (@preg_match('/\\pL/u', 'a') == 1) {
         // PCRE unicode support is turned on
         // add Unicode modifier to the match expression
         $matchExpression .= 'u';
     }
     $maxTerms = Zend_Search_Lucene::getTermsPerQueryLimit();
     foreach ($fields as $field) {
         $index->resetTermsStream();
         // require_once 'Zend/Search/Lucene/Index/Term.php';
         if ($prefix != '') {
             $index->skipTo(new Zend_Search_Lucene_Index_Term($prefix, $field));
             while ($index->currentTerm() !== null && $index->currentTerm()->field == $field && substr($index->currentTerm()->text, 0, $prefixLength) == $prefix) {
                 if (preg_match($matchExpression, $index->currentTerm()->text) === 1) {
                     $this->_matches[] = $index->currentTerm();
                     if ($maxTerms != 0 && count($this->_matches) > $maxTerms) {
                         // require_once 'Zend/Search/Lucene/Exception.php';
                         throw new Zend_Search_Lucene_Exception('Terms per query limit is reached.');
                     }
                 }
                 $index->nextTerm();
             }
         } else {
             $index->skipTo(new Zend_Search_Lucene_Index_Term('', $field));
             while ($index->currentTerm() !== null && $index->currentTerm()->field == $field) {
                 if (preg_match($matchExpression, $index->currentTerm()->text) === 1) {
                     $this->_matches[] = $index->currentTerm();
                     if ($maxTerms != 0 && count($this->_matches) > $maxTerms) {
                         // require_once 'Zend/Search/Lucene/Exception.php';
                         throw new Zend_Search_Lucene_Exception('Terms per query limit is reached.');
                     }
                 }
                 $index->nextTerm();
             }
         }
         $index->closeTermsStream();
     }
     if (count($this->_matches) == 0) {
         // require_once 'Zend/Search/Lucene/Search/Query/Empty.php';
         return new Zend_Search_Lucene_Search_Query_Empty();
     } else {
         if (count($this->_matches) == 1) {
             // require_once 'Zend/Search/Lucene/Search/Query/Term.php';
             return new Zend_Search_Lucene_Search_Query_Term(reset($this->_matches));
         } else {
             // require_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php';
             $rewrittenQuery = new Zend_Search_Lucene_Search_Query_MultiTerm();
             foreach ($this->_matches as $matchedTerm) {
                 $rewrittenQuery->addTerm($matchedTerm);
             }
             return $rewrittenQuery;
         }
     }
 }
 /**
  * Perform a search query on the index
  */
 public function performSearch()
 {
     try {
         $index = Zend_Search_Lucene::open(self::get_index_location());
         Zend_Search_Lucene::setResultSetLimit(100);
         $query = new Zend_Search_Lucene_Search_Query_Boolean();
         $term = Zend_Search_Lucene_Search_QueryParser::parse($this->getQuery());
         $query->addSubquery($term, true);
         if ($this->modules) {
             $moduleQuery = new Zend_Search_Lucene_Search_Query_MultiTerm();
             foreach ($this->modules as $module) {
                 $moduleQuery->addTerm(new Zend_Search_Lucene_Index_Term($module, 'Entity'));
             }
             $query->addSubquery($moduleQuery, true);
         }
         if ($this->versions) {
             $versionQuery = new Zend_Search_Lucene_Search_Query_MultiTerm();
             foreach ($this->versions as $version) {
                 $versionQuery->addTerm(new Zend_Search_Lucene_Index_Term($version, 'Version'));
             }
             $query->addSubquery($versionQuery, true);
         }
         $er = error_reporting();
         error_reporting('E_ALL ^ E_NOTICE');
         $this->results = $index->find($query);
         error_reporting($er);
         $this->totalResults = $index->numDocs();
     } catch (Zend_Search_Lucene_Exception $e) {
         user_error($e . '. Ensure you have run the rebuld task (/dev/tasks/RebuildLuceneDocsIndex)', E_USER_ERROR);
     }
 }