/** * Constructor * * @param \Zend_Search_Lucene_Interface Lucene index instance * @param \Zend_Search_Lucene_Search_Query $query Lucene query * @param array $terms Original search terms * @param boolean $highlight Highlight search terms in results * @return void */ public function __construct(\Zend_Search_Lucene_Interface $index, \Zend_Search_Lucene_Search_Query $query, array $terms = array(), $highlight = false) { $this->_index = $index; $this->_query = $query; $this->_terms = $terms; $this->_index->addReference(); // Run the Lucene search and register the results foreach ($this->_index->find($this->_query) as $hit) { $this->_hits[] = \Tollwerk\TwLucenesearch\Domain\Model\QueryHit::cast($hit); } // If search terms should be highlighted ... if (count($this->_hits) && $highlight) { $this->_highlight = array(); foreach ($this->_query->rewrite($this->_index)->getQueryTerms() as $term) { if (!array_key_exists($term->field, $this->_highlight)) { $this->_highlight[$term->field] = array($term->text); } else { $this->_highlight[$term->field][] = $term->text; } } } }
/** * * @since 5-24-11 */ protected function find(Zend_Search_Lucene_Proxy $lucene, array $where_criteria) { $ret_list = array(); Zend_Search_Lucene::setResultSetLimit($where_criteria['limit'][0]); if (empty($where_criteria['sort'])) { $ret_list = $lucene->find($where_criteria['query']); } else { // http://framework.zend.com/manual/en/zend.search.lucene.searching.html#zend.search.lucene.searching.sorting $args = $where_criteria['sort']; array_unshift($args, $where_criteria['query']); $ret_list = call_user_func_array(array($lucene, 'find'), $args); } //if/else return $lucene->find($where_criteria['query']); }
/** * Search all pages that match the query. * * <code> * //$query = '(pi AND groupe AND partner*) OR pi-groupe'; * $query = " travers projet ference coin"; * $options = array( * 'searchBool' => true, * 'searchBoolType' => 'AND', * 'searchByMotif' => true, * 'setMinPrefixLength'=> 0, * 'getResultSetLimit' => 0, * 'searchFields' => array( * 0=> array('sortField'=>'Contents', 'sortType'=> SORT_STRING, 'sortOrder' => SORT_ASC), * 1=> array('sortField'=>'Key', 'sortType'=> SORT_NUMERIC, 'sortOrder' => SORT_DESC) * ), * ); * $result = $this->container->get('pi_app_admin.manager.search_lucene')->searchPagesByQuery($query, $options); * </code> * * @link http://framework.zend.com/manual/fr/zend.search.lucene.searching.html * @link http://framework.zend.com/manual/fr/learning.lucene.queries.html * @link http://framework.zend.com/manual/1.12/fr/zend.search.lucene.query-api.html * @param string $query The search query index file * @param array $options Options of the search query of the index file * @return array All Etags from pages that match the query. * @access public * * @author Etienne de Longeaux <*****@*****.**> * @since 2012-06-11 */ public function searchPagesByQuery($query = "Key:*", $options = null, $locale = '') { try { if (isset($options) && is_array($options) && count($options) >= 1) { $options_values = array_map(function ($key, $value) { if (in_array($value, array("true"))) { return 1; } elseif (in_array($value, array("false"))) { return 0; } elseif (!is_array($value) && preg_match_all("/[0-9]+/", $value, $nbrs, PREG_SET_ORDER)) { return intval($value); } else { return $value; } }, array_keys($options), array_values($options)); $options = array_combine(array_keys($options), $options_values); } if (empty($query)) { return null; } else { $query = $this->container->get('sfynx.tool.string_manager')->minusculesSansAccents($query); } if (empty($locale)) { $locale = $this->container->get('request')->getLocale(); } $options_default = array('searchBool' => true, 'searchBoolType' => 'OR', 'searchByMotif' => true, 'setMinPrefixLength' => 0, 'getResultSetLimit' => 0, 'searchFields' => '*', 'searchMaxResultByWord' => 5); if (is_array($options)) { $options = array_merge($options_default, $options); } else { $options = $options_default; } if ($options['searchBool']) { $q_string = $this->container->get('sfynx.tool.string_manager')->cleanWhitespace($query); $q_array = explode(' ', $q_string); if ($options['searchByMotif']) { $q_array = array_map(function ($value) { return $value . '*'; }, array_values($q_array)); } switch ($options['searchBoolType']) { case 'OR': $new_query = implode(' OR ', $q_array); break; case 'AND': $new_query = implode(' AND ', $q_array); break; default: break; } } else { $new_query = $query; } // Open the index. self::open($this->_indexPath); // Set minimum prefix length. \Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength($options['setMinPrefixLength']); // Set result set limit. \Zend_Search_Lucene::setResultSetLimit($options['getResultSetLimit']); // Performs a query against the index. if (is_array($options['searchFields']) && $query != "Key:*") { $fields_vars = "\$hits = self::\$_index->find(\$new_query,"; $i = 0; foreach ($options['searchFields'] as $key => $valuesField) { $sortField = $valuesField["sortField"]; if (isset($valuesField["sortType"]) && !empty($valuesField["sortType"])) { $sortType = $valuesField["sortType"]; } else { $sortType = SORT_STRING; } if (isset($valuesField["sortOrder"]) && !empty($valuesField["sortOrder"])) { $sortOrder = $valuesField["sortOrder"]; } else { $sortOrder = $valuesField["sortOrder"]; } if ($i == 0) { $fields_vars .= " \"{$sortField}\", {$sortType}, {$sortOrder}"; } else { $fields_vars .= ", \"{$sortField}\", {$sortType}, {$sortOrder}"; } $i++; } $fields_vars .= ");"; try { setlocale(LC_ALL, $locale); eval($fields_vars); // print_r($options); // print_r($new_query); // print_r('<br />'); // print_r($fields_vars); // //exit; } catch (\Exception $e) { setlocale(LC_ALL, 'fr_FR'); eval($fields_vars); } //eval("\$hits = self::\$_index->find(\$query, \"\$sortField\", \$sortType, \$sortOrder);"); //$hits = self::$_index->find($query, "Contents", SORT_STRING, SORT_DESC); //$hits = self::$_index->find(' *"férence"* ', "Contents", SORT_STRING, SORT_ASC); //$hits = self::$_index->find(' *"MOTIVTelecommunication"* OR *"Sophisticated"* ', "Contents", SORT_STRING, SORT_ASC); } else { try { setlocale(LC_ALL, $locale); $hits = self::$_index->find($new_query); } catch (\Exception $e) { setlocale(LC_ALL, 'fr_FR'); $hits = self::$_index->find($new_query); } } $result_search = null; if (isset($hits) && is_array($hits)) { foreach ($hits as $hit) { $field = $hit->getDocument()->getFieldNames(); if (in_array('Key', $field)) { $data['Key'] = $hit->getDocument()->Key; } else { $data['Key'] = ""; } if (in_array('Route', $field)) { $data['Route'] = $hit->getDocument()->Route; } else { $data['Route'] = ""; } if (in_array('Title', $field)) { $data['Title'] = utf8_decode($hit->getDocument()->Title); } else { $data['Title'] = ""; } if (in_array('Keywords', $field)) { $data['Keywords'] = utf8_decode($hit->getDocument()->Keywords); } else { $data['Keywords'] = ""; } if (in_array('ModDate', $field)) { $data['ModDate'] = $hit->getDocument()->ModDate; } else { $data['ModDate'] = ""; } $data['MaxResultByWord'] = $options['searchMaxResultByWord']; $result_search[] = $data; } } return $result_search; } catch (\Exception $e) { return array(); } }