function getMatchingConnections($criteria) { $index = $this->getIndex(); Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(0); Zend_Search_Lucene::setResultSetLimit(25); // TODO during dev $results = $index->find($criteria); $ret = array(); foreach ($results as $hit) { $res = array(); $res['created'] = $hit->created; try { $res['title'] = $hit->title; } catch (Zend_Search_Lucene_Exception $e) { $res['title'] = ''; } try { $res['url'] = $hit->url; } catch (Zend_Search_Lucene_Exception $e) { $res['url'] = ''; } try { $res['keywords'] = $hit->keywords; } catch (Zend_Search_Lucene_Exception $e) { $res['keywords'] = ''; } try { $res['language'] = $hit->language; } catch (Zend_Search_Lucene_Exception $e) { $res['language'] = ''; } try { $res['geo_lat'] = $hit->geo_lat; } catch (Zend_Search_Lucene_Exception $e) { $res['geo_lat'] = ''; } try { $res['geo_lon'] = $hit->geo_lon; } catch (Zend_Search_Lucene_Exception $e) { $res['geo_lon'] = ''; } try { $res['geo_zoom'] = $hit->geo_zoom; } catch (Zend_Search_Lucene_Exception $e) { $res['geo_zoom'] = ''; } $res['class'] = 'tablename'; $res['metadata'] = ''; if ($res['geo_lat'] && $res['geo_lon']) { $res['class'] .= ' geolocated connection'; $res['metadata'] = " data-geo-lat=\"{$res['geo_lat']}\" data-geo-lon=\"{$res['geo_lon']}\""; if (isset($res['geo_zoom'])) { $res['metadata'] .= " data-geo-zoom=\"{$res['geo_zoom']}\""; } $res['metadata'] .= ' data-icon-name="tiki"'; } $ret[] = $res; } return $ret; }
/** * @param $queryString * @return array */ public function find($queryString) { $queryString = trim($queryString); if (empty($queryString)) { return ["queryString" => $queryString, "message" => "No String"]; } else { $index = \Zend_Search_Lucene::open($this->indexfile); $res = explode(' ', $queryString); \Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(1); \Zend_Search_Lucene::setResultSetLimit(5); $query = new \Zend_Search_Lucene_Search_Query_Boolean(); foreach ($res as $val) { if (!empty($val)) { $subquery = new \Zend_Search_Lucene_Search_Query_Boolean(); $searchkey1 = $val . "*"; $pattern = new \Zend_Search_Lucene_Index_Term($searchkey1, "name"); $userQuery = new \Zend_Search_Lucene_Search_Query_Wildcard($pattern); $patternUsername = new \Zend_Search_Lucene_Index_Term($searchkey1, "username"); $usernameQuery = new \Zend_Search_Lucene_Search_Query_Wildcard($patternUsername); $subquery->addSubquery($userQuery, null); $subquery->addSubquery($usernameQuery, null); $query->addSubquery($subquery, true); } } $hits = $index->find($query); if (!empty($hits)) { $results = []; foreach ($hits as $hit) { if ($hit->username != $_SESSION['user']->username) { $results[] = $hit->username; } } if (!empty($results)) { /** @noinspection PhpUndefinedMethodInspection */ /** @var Users $users */ $users = $_SESSION['user']->getTable(); if (isset($_POST['friends'])) { /** @noinspection PhpUndefinedMethodInspection */ $friends = $_SESSION['user']->getFriendList(); if (empty($friends)) { return ["queryString" => $queryString, "users" => []]; } else { $userresult = $users->getSet($results, 'u.username'); } } else { $userresult = $users->getSet($results, "u.username", ["u.userid", "u.username", "u.name"]); } return ["queryString" => $queryString, "users" => $userresult->toArray()]; } } } return ["queryString" => $queryString]; }
public function search($query, $limit = 'none') { $config = Zend_Registry::get('config'); try { $index = Zend_Search_Lucene::open($config->app->search->workshopIndexPath); } catch (Exception $e) { $index = Zend_Search_Lucene::create($config->app->search->workshopIndexPath); } if ($limit != 'none') { Zend_Search_Lucene::setResultSetLimit($limit); } return $index->find($query); }
/** * * @param $name * index name can be specified */ function getIndex($name = NULL) { require_once 'Zend/Search/Lucene.php'; $index_path = JuceneHelper::getIndexPath($name); if (!JFolder::exists($index_path)) { JFolder::create($index_path); $index = Zend_Search_Lucene::create($index_path); } else { try { $index = Zend_Search_Lucene::open($index_path); } catch (Exception $ex) { echo $ex->getMessage(); } } $component = JComponentHelper::getComponent('com_jucene'); $params =& JComponentHelper::getParams($component->params); Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num()); Zend_Search_Lucene::setResultSetLimit($params->get('resuls_limit', 100)); return $index; }
/** * 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); } }
function searchQuery($query, $size = SEARCH_RESULTS_SIZE) { $index = searchGetIndex(); Zend_Search_Lucene::setResultSetLimit($size); return $index->find($query); }
private function __readData(&$model, $queryData) { $highlight = false; if (isset($queryData['highlight']) && $queryData['highlight'] == true) { $highlight = true; } $limit = 1000; // The following 3 lines break pagination. And since I can't figure out how to make Zend Lucene support // offset, we'll just have to get all the results for pagination to work. // // if (!empty($queryData['limit'])) { // $limit = $queryData['limit']; // } $query = $this->__parseQuery($queryData); Zend_Search_Lucene::setResultSetLimit($limit); $hits = $this->__index->find($query); $data = array(); foreach ($hits as $i => $hit) { $fields = $this->__getFieldInfo($hit->getDocument()); $returnArray = array(); foreach ($fields as $field) { if ($highlight && $field->isIndexed == 1) { $returnArray[$field->name] = $query->htmlFragmentHighlightMatches($hit->{$field->name}); } else { $returnArray[$field->name] = $hit->{$field->name}; } } $returnArray['id'] = $hit->id; $returnArray['score'] = $hit->score; $data[$i][$model->alias] = $returnArray; } return $data; }
/** * Indexer Constructor. * * @global type $webDir */ public function __construct() { global $webDir; if (!get_config('enable_indexing')) { return; } $index_path = $webDir . self::$_index_dir; // Give read-writing permissions only for current user and group Zend_Search_Lucene_Storage_Directory_Filesystem::setDefaultFilePermissions(0600); // Utilize UTF-8 compatible text analyzer Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive()); try { if (file_exists($index_path)) { $this->__index = Zend_Search_Lucene::open($index_path); // Open index } else { $this->__index = Zend_Search_Lucene::create($index_path); // Create index } } catch (Zend_Search_Lucene_Exception $e) { require_once 'fatal_error.php'; } $this->__index->setFormatVersion(Zend_Search_Lucene::FORMAT_2_3); // Set Index Format Version Zend_Search_Lucene::setResultSetLimit(self::$_resultSetLimit); // Set Result Set Limit // write an .htaccess to prevent raw access to index files $htaccess = $index_path . '/.htaccess'; if (!file_exists($htaccess)) { $fd = fopen($htaccess, "w"); fwrite($fd, "deny from all\n"); fclose($fd); } if (!file_exists($index_path . '/index.php')) { touch($index_path . '/index.php'); } }
public static function search( $query, $subqueries = array()) { $query = strtolower($query); Loader::library('3rdparty/Zend/Search/Lucene'); Loader::library('3rdparty/StandardAnalyzer/Analyzer/Standard/English'); $index = new Zend_Search_Lucene(DIR_FILES_CACHE_PAGES); $index->setResultSetLimit(200); //Zend_Search_Lucene_Analysis_Analyzer::setDefault(new StandardAnalyzer_Analyzer_Standard_English()); Zend_Search_Lucene_Analysis_Analyzer::setDefault(new StandardAnalyzer_Analyzer_Standard_English()); $queryModifiers=array(); $mainQuery = Zend_Search_Lucene_Search_QueryParser::parse($query, APP_CHARSET); $query = new Zend_Search_Lucene_Search_Query_Boolean(); $query->addSubquery($mainQuery, true); foreach($subqueries as $subQ) { if( !is_array($subQ) || !isset( $subQ['query'] ) ) $subQuery = $subQ; else $subQuery = $subQ['query']; if( !is_array($subQ) || !isset($subQ['required']) ) $required=true; else $required=$subQ['required']; $query->addSubquery( $subQuery, $required ); } $query = utf8_encode($query); $resultsTmp = $index->find($query); $results = array(); foreach($resultsTmp as $r) $results[] = new IndexedSearchResult($r->cID, $r->cName, $r->cDescription, $r->score, $r->cPath, $r->cBody); return $results; }
/** * search function * searches the index * * @param mixed $Model * @param mixed $query * @param int $limit * @param int $page * @access public * @return void */ function search(&$Model, $query, $limit = 20, $page = 1) { // open the index if (!$this->open_index($Model)) { return false; } try { // set the default encoding Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8'); // zend search results limiting (We will use the LimitIterator) // we can use it for some maximum value like 1000 if its likely that there could be more results Zend_Search_Lucene::setResultSetLimit(1000); // set the parser default operator to AND Zend_Search_Lucene_Search_QueryParser::setDefaultOperator(Zend_Search_Lucene_Search_QueryParser::B_AND); // utf-8 num analyzer Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive()); // parse the query $Query = Zend_Search_Lucene_Search_QueryParser::parse($query); $Terms = $Query->getQueryTerms(); foreach ($Terms as $Term) { $this->terms[] = $Term->text; } // do the search $Hits = new ArrayObject($this->Index->find($Query)); } catch (Zend_Search_Lucene_Exception $e) { $this->log("Zend_Search_Lucene error: " . $e->getMessage(), 'searchable'); } $this->hits_count = count($Hits); if (!count($Hits)) { return null; } $Hits = new LimitIterator($Hits->getIterator(), ($page - 1) * $limit, $limit); $results = array(); foreach ($Hits as $Hit) { $Document = $Hit->getDocument(); $fields = $Document->getFieldNames(); $result = array(); foreach ($fields as $field) { $result['Result'][$field] = $Document->{$field}; } $results[] = $result; } return $results; }
/** * 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(); } }
/** * Execute the query * * @param string|Zym_Search_Lucene_IQuery $query * @param int $resultSetLimit * @return array */ public function search($query, $resultSetLimit = null) { if (!$resultSetLimit) { $resultSetLimit = self::$_defaultResultSetLimit; } Zend_Search_Lucene::setResultSetLimit((int) $resultSetLimit); return $this->_searchIndex->find((string) $query); }
/** * getPageSimilarPageLinks * @return string $strReturn * @author Cornelius Hansjakob <*****@*****.**> * @version 1.0 */ public function getPageSimilarPageLinks($intNumber = 5, $strContainerClass = 'links', $strItemClass = 'item') { $strReturn = ''; $strQuery = ''; $objPageTags = $this->objPage->getTagsValues('page_tags'); if (count($objPageTags) > 0) { foreach ($objPageTags as $objTag) { $strQuery .= 'page_tags:"' . $objTag->title . '" OR '; } } $objPageCategories = $this->objPage->getCategoriesValues('category'); if (count($objPageCategories) > 0) { foreach ($objPageCategories as $objCategory) { $strQuery .= 'category:"' . $objCategory->title . '" OR '; } } $strQuery = rtrim($strQuery, ' OR '); if ($strQuery != '' && count(scandir(GLOBAL_ROOT_PATH . $this->core->sysConfig->path->search_index->page)) > 2) { Zend_Search_Lucene::setResultSetLimit($intNumber); $objIndex = Zend_Search_Lucene::open(GLOBAL_ROOT_PATH . $this->core->sysConfig->path->search_index->page); $objHits = $objIndex->find($strQuery); if (count($objHits) > 0) { $strReturn .= ' <div class="' . $strContainerClass . '"> <h3>' . $this->objTranslate->_('Similar_pages') . '</h3>'; $counter = 1; foreach ($objHits as $objHit) { if ($objHit->key != $this->objPage->getPageId()) { $objDoc = $objHit->getDocument(); $arrDocFields = $objDoc->getFieldNames(); if (array_search('url', $arrDocFields) && array_search('title', $arrDocFields) && array_search('date', $arrDocFields)) { $strReturn .= ' <div class="item"> <a href="' . $objHit->url . '">' . htmlentities($objHit->title, ENT_COMPAT, $this->core->sysConfig->encoding->default) . '</a><br/> <span>' . $this->objTranslate->_('Created_at') . '</span> <span class="black">' . $objHit->date . '</span> </div>'; } } } $strReturn .= ' <div class="clear"></div> </div>'; } } return $strReturn; }
/** * Perform a search query on the index */ public function performSearch() { try { $index = Zend_Search_Lucene::open(self::get_index_location()); Zend_Search_Lucene::setResultSetLimit(200); $this->results = $index->find($this->getQuery()); $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); } }
/** * * @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']); }
function lucene_get_search_link_ids($by_date = false, $start = 0, $count = 50) { global $globals; $ids = array(); if (!empty($_REQUEST['q'])) { if ($_REQUEST['p']) { //Allows variable "p" as prefix too $_REQUEST['q'] = $_REQUEST['p'] . ':' . $_REQUEST['q']; } $words = $_REQUEST['q'] = trim(substr(strip_tags($_REQUEST['q']), 0, 250)); // Basic filtering to avoid Lucene errors $words = preg_replace('/\\^([^1-9])/', '$1', $words); // Only allow ^ followed by numbers //$words = preg_replace('/(.*) +(\w{1,2} *$)/', '$2 $1', $words); // Lucene dies if the last word has 2 chars, move it to the begining //$words = preg_replace('/[\~\*\(\)\[\]\|\{\}]/',' ',$words); //$words = preg_replace('/^ *(and|not|no|or|\&) *$/','',$words); if (preg_match('/^ *(\\w+): *(.*)/', mb_strtolower($words), $matches)) { $prefix = $matches[1]; $words = $matches[2]; } if (preg_match('/^http[s]*/', $prefix)) { // It's an url search $words = "{$prefix}:{$words}"; $prefix = false; $field = 'url'; } $words_count = count(explode(" ", $words)); if ($words_count == 1 || $prefix == 'date') { $words = preg_replace('/(^| )(\\w)/', '+$2', $words); $by_date = true; } if ($prefix) { switch ($prefix) { case 'url': $field = 'url'; break; case 'title': $field = 'title'; break; case 'tag': case 'tags': $field = 'tags'; break; } } // if there is only a word and is a number, do not search in urls if ($words_count == 1 && !$field && preg_match('/^\\+*[0-9]{1,4}$/', $words)) { //$words = preg_replace('/\+/', '', $words); $words = "title:{$words} tags:{$words} content:{$words}"; } if ($field) { $query = "{$field}:({$words})"; } else { $query = $words; } if (empty($query)) { return false; } if ($globals['bot']) { Zend_Search_Lucene::setResultSetLimit(40); } elseif ($words_count == 1) { Zend_Search_Lucene::setResultSetLimit(6000); } else { Zend_Search_Lucene::setResultSetLimit(3000); } $index = lucene_open(); echo "<!--Query: {$query} -->\n"; $hits = lucene_search($index, $query, $by_date); if (!$by_date && count($hits) > 200 && $words_count > 1 && !preg_match('/[\\+\\-\\"]|(^|[ :])(AND|OR|NOT|TO) /i', $words)) { Zend_Search_Lucene::setResultSetLimit(200); $query = preg_replace('/(^|[: ]+)(\\w)/', '$1+$2', $query); echo "\n<!-- Trying to refine with a new query: {$query} -->\n"; $hits2 = lucene_search($index, $query, $by_date); if (count($hits2) > 0) { $base_hits = 0; $nhits2 = min(count($hits2), 10); // Add the second hits if they are not already among the fist 10 for ($i = $nhits2 - 1; $i >= 0; $i--) { $found = false; for ($j = $base_hits; $j < $base_hits + 10 && !$found; $j++) { if ($hits2[$i]->link_id == $hits[$j]->link_id) { //$hits[$j] = false; unset($hits[$j]); $found = true; } } array_unshift($hits, $hits2[$i]); $base_hits++; } //$hits = array_merge($hits2, $hits); } } $globals['rows'] = count($hits); // Save the number of hits $elements = min($globals['rows'], $start + $count); if ($elements == 0 || $elements < $start) { return false; } $previous = 0; $i = $start; for ($i = $start; $i < $elements; $i++) { $hit = $hits[$i]; if ($hit && $hit->link_id != $previous) { array_push($ids, $hit->link_id); $previous = $hit->link_id; } } return $ids; } return false; }
private function __readData(&$model, $queryData) { $highlight = false; if (isset($queryData['highlight']) && $queryData['highlight'] == true) { $highlight = true; } $limit = 1000; // The following 3 lines break pagination. And since I can't figure out how to make Zend Lucene support // offset, we'll just have to get all the results for pagination to work. // // if (!empty($queryData['limit'])) { // $limit = $queryData['limit']; // } /** * The ZendSearchLucene queries cannot query based upon the internal document * id. This is a workaround that involves directly calling getDocument if * an id is specified in a query. */ $hitData = array(); $docData = array(); $id = null; if (isset($queryData['conditions']['id']) && is_numeric($queryData['conditions']['id'])) { $id = $queryData['conditions']['id']; $doc = $this->__index->getDocument($id); $docData = $this->__documentToArray($doc); $docData['id'] = $id; $hitData = array($docData); } if (!empty($queryData) && $id === null) { $query = $this->__parseQuery($queryData); Zend_Search_Lucene::setResultSetLimit($limit); $hits = $this->__index->find($query); foreach ($hits as $i => $hit) { $returnArray = $this->__documentToArray($hit->getDocument()); $returnArray['id'] = $hit->id; $returnArray['score'] = $hit->score; $hitData[$i][$model->alias] = $returnArray; } } return $hitData; }
/** * 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; }
/** * Execute the query * * @param string|Zym_Search_Lucene_IQuery $query * @param int $resultSetLimit * @return array */ public function search($query, $resultSetLimit = null) { // If the query is an instance of Zym_Search_Lucene_IQuery serialize it to a string $query = (string) $query; if (!$resultSetLimit) { $resultSetLimit = self::$_defaultResultSetLimit; } Zend_Search_Lucene::setResultSetLimit((int) $resultSetLimit); $cache = self::$_resultCache; if (null !== $cache) { $queryHash = $this->_getQueryHash($query); if (!($results = $cache->load($queryHash))) { $results = $this->_executeSearch($query, $resultSetLimit); $this->_cacheResults($results); } } else { $results = $this->_executeSearch($query, $resultSetLimit); } return $results; }
public function getHits($keyword, $params = array()) { //Open the index to search in. Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive()); Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8'); $query = Zend_Search_Lucene_Search_QueryParser::parse($keyword, 'UTF-8'); $Index = Zend_Search_Lucene::open('../application/searchindex'); Zend_Search_Lucene::setResultSetLimit(350); //Search. if (isset($params['search_full']) == 1) { $query = '"' . $query . '"'; } $hits = $Index->find($query); //Filter $results = array(); if (isset($params['types']) && !empty($params['types'])) { if (!is_array($params['types'])) { $params['types'] = (array) $params['types']; } foreach ($hits as $hit) { if (in_array($hit->type, $params['types'])) { $results[] = $hit; } } } else { $results = $hits; } return $results; }
public function searchAction() { Zend_Registry::set('theaction', 'content'); $this->toTpl('theInclude', 'search'); Zend_Registry::set('module', 'Search Results'); //error_reporting(E_ALL); $data = $this->_request->getParams(); $dirs = $this->dirs; $word = strtolower($data['q']); $index = new Zend_Search_Lucene(APPL_PATH . $dirs['structure']['indexes'] . DIR_SEP . "objects"); $exp = explode(" ", $word); $query = new Zend_Search_Lucene_Search_Query_Phrase($exp); $query->setSlop(2); //get all available indexed Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive()); Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8'); Zend_Search_Lucene::setResultSetLimit(10); $result = $index->find($query); foreach ($result as $hit) { $cid = $this->getIdBySlug($hit->slug); $content = $this->getContent($hit->type_id, $cid); $content['id'] = $cid; $content['type_id'] = $hit->type_id; $content['type'] = $this->getContentType($hit->type_id); $resu[] = $content; } $this->toTpl('theList', $resu); }
public function searchAction() { Zend_Registry::set('theaction', 'content'); $this->toTpl('theInclude', 'list'); Zend_Registry::set('module', 'Search Results'); //error_reporting(E_ALL); $data = $this->_request->getParams(); if (trim($data['q'])) { $dirs = $this->dirs; $word = strtolower($data['q']); $index = new Zend_Search_Lucene(APPL_PATH . $dirs['structure']['indexes'] . DIR_SEP . "objects"); $exp = explode(" ", $word); $query = new Zend_Search_Lucene_Search_Query_Phrase($exp); $query->setSlop(2); //get all available indexed Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive()); Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8'); Zend_Search_Lucene::setResultSetLimit(10); $result = $index->find($query); foreach ($result as $hit) { $obj = $this->getAllBySlug($hit->slug); $type = $this->getContentType($obj['type_id']); $content = $this->getContent($type['title'], $obj['id']); $resu[] = $content; } $resu = $this->doQoolHook('front_pre_assign_search_results', $resu); $this->toTpl('content', $resu); } else { $params = array("message" => $this->t("Please fill in a search term"), "msgtype" => 'error'); $this->addMessage($params); $this->_helper->redirector('index', 'index', 'default'); } }
/** * livesearchByPath * @param string $strIndexPath * @return Zend_Search_Lucene_Search_QueryHit */ private function livesearchByPath($strIndexPath) { if ($this->intLimitLiveSearch > 0 && $this->intLimitLiveSearch != '') { Zend_Search_Lucene::setResultSetLimit($this->intLimitLiveSearch); } $objIndex = Zend_Search_Lucene::open($strIndexPath); $strQuery = ''; if (strlen($this->strSearchValue) < 3) { $strQuery = $this->strSearchValue; } else { $arrSearchValue = explode(' ', $this->strSearchValue); foreach ($arrSearchValue as $strSearchValue) { $strQuery .= '+(title:' . $strSearchValue . ' OR articletitle:' . $strSearchValue . ' OR page_tags:' . $strSearchValue . ' OR '; $strSearchValue = preg_replace('/([^\\pL\\s\\d])/u', '?', $strSearchValue); $strQuery .= 'title:' . $strSearchValue . '* OR articletitle:' . $strSearchValue . '* OR page_tags:' . $strSearchValue . '* OR '; $strSearchValue = str_replace('?', '', $strSearchValue); $strQuery .= 'title:' . $strSearchValue . '~ OR articletitle:' . $strSearchValue . '~ OR page_tags:' . $strSearchValue . '~)'; } } $strQuery = $strQuery . ' +(languageId:' . $this->intLanguageId . ') +(rootLevelId:' . $this->intRootLevelId . ')'; if ($this->strParentFolderId != '') { $strQuery .= ' +(parentFolderIds:' . $this->strParentFolderId . ')'; } $objQuery = Zend_Search_Lucene_Search_QueryParser::parse($strQuery, $this->core->sysConfig->encoding->default); return $objIndex->find($objQuery); }
* This file is part of the allocPSA application <*****@*****.**>. * * allocPSA is free software: you can redistribute it and/or modify it * under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or (at * your option) any later version. * * allocPSA is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public * License for more details. * * You should have received a copy of the GNU Affero General Public License * along with allocPSA. If not, see <http://www.gnu.org/licenses/>. */ define("NO_REDIRECT", 1); require_once "../alloc.php"; $index = new Zend_Search_Lucene(ATTACHMENTS_DIR . 'search/task'); $index->setResultSetLimit(10); $needle = 'name:(' . $_GET["taskName"] . ') AND pid:' . $_GET["projectID"]; $query = Zend_Search_Lucene_Search_QueryParser::parse($needle); $hits = $index->find($needle); foreach ($hits as $hit) { $d = $hit->getDocument(); $str .= "<div style='padding-bottom:3px'>"; $str .= "<a href=\"" . $TPL["url_alloc_task"] . "taskID=" . $d->getFieldValue('id') . "\">" . $d->getFieldValue('id') . " " . $d->getFieldValue('name') . "</a>"; $str .= "</div>"; } if ($str) { echo $str; }
static function getLuceneHits($query, $limit = 20) { ProjectConfiguration::registerZend(); $index = self::getLuceneIndex(); Zend_Search_Lucene::setResultSetLimit($limit); $originalQuery = $query; //if no ORs in query, assume all terms are ANDs if (!strstr($query, ' OR ')) { $query = preg_replace('/\\s{1,}/', ' AND ', $query); } return $index->find($query); }
public function testLimitingResult() { $index = Zend_Search_Lucene::open(dirname(__FILE__) . '/_index23Sample/_files'); $storedResultSetLimit = Zend_Search_Lucene::getResultSetLimit(); Zend_Search_Lucene::setResultSetLimit(3); $hits = $index->find('"reporting bugs"', 'path'); $this->assertEquals(count($hits), 3); $expectedResultset = array(array(7, 0.212395, 'IndexSource/contributing.bugs.html'), array(0, 0.247795, 'IndexSource/contributing.documentation.html'), array(2, 0.176996, 'IndexSource/contributing.patches.html')); foreach ($hits as $resId => $hit) { $this->assertEquals($hit->id, $expectedResultset[$resId][0]); $this->assertTrue(abs($hit->score - $expectedResultset[$resId][1]) < 1.0E-6); $this->assertEquals($hit->path, $expectedResultset[$resId][2]); } Zend_Search_Lucene::setResultSetLimit($storedResultSetLimit); }
/** * get_page_similar_page_links * @param integer $intNumber * @return string $strHtmlOutput * @author Thomas Schedler <*****@*****.**> * @version 1.0 */ function get_page_similar_page_links($intNumber = 5) { $strHtmlOutput = ''; $core = getCoreObject(); $objPage = getPageObject(); $strQuery = ''; $objPageTags = $objPage->getTagsValues('page_tags'); if (count($objPageTags) > 0) { foreach ($objPageTags as $objTag) { $strQuery .= 'page_tags:"' . $objTag->title . '" OR '; } } $objPageCategories = $objPage->getCategoriesValues('category'); if (count($objPageCategories) > 0) { foreach ($objPageCategories as $objCategory) { $strQuery .= 'category:"' . $objCategory->title . '" OR '; } } $strQuery = rtrim($strQuery, ' OR '); if ($strQuery != '' && count(scandir(GLOBAL_ROOT_PATH . $core->sysConfig->path->search_index->page)) > 2) { Zend_Search_Lucene::setResultSetLimit($intNumber); $objIndex = Zend_Search_Lucene::open(GLOBAL_ROOT_PATH . $core->sysConfig->path->search_index->page); $objHits = $objIndex->find($strQuery); if (count($objHits) > 0) { $strHtmlOutput .= ' <div class="divLinks"> <h3>Ähnliche Beiträge</h3>'; $counter = 1; foreach ($objHits as $objHit) { if ($objHit->key != $objPage->getPageId()) { $objDoc = $objHit->getDocument(); $arrDocFields = $objDoc->getFieldNames(); if (array_search('url', $arrDocFields) && array_search('title', $arrDocFields) && array_search('date', $arrDocFields)) { $strHtmlOutput .= ' <div class="divLinkItem"> <a href="' . $objHit->url . '">' . htmlentities($objHit->title, ENT_COMPAT, $core->sysConfig->encoding->default) . '</a><br/> <span>Erstellt am</span> <span class="black">' . $objHit->date . '</span><!-- <span>unter</span> <span class="black">Sportservice News</span> --> </div>'; } } } $strHtmlOutput .= ' <div class="clear"></div> </div>'; } } echo $strHtmlOutput; }
/** * Set result set limit. * * 0 (default) means no limit * * @param integer $limit */ public static function setResultSetLimit($limit) { Zend_Search_Lucene::setResultSetLimit($limit); }
/** * Проверить, есть ли слово с индексе. * @param string $term * @return boolean */ public function has_term($term) { //return $this->get_index()->hasTerm(new Zend_Search_Lucene_Index_Term($term)); $q = new Zend_Search_Lucene_Search_Query_Term(new Zend_Search_Lucene_Index_Term($term)); Zend_Search_Lucene::setResultSetLimit(1); $has_term = sizeof($this->get_index()->find($q)) > 0; Zend_Search_Lucene::setResultSetLimit($this->get_setting('ZendSearchLucene_ResultSetLimit')); return $has_term; }