/**
  * @internal
  **/
 public static function _query($args, $pageIndex, $size, $sortByDate = false)
 {
     $query = new \Elastica\Query($args);
     $query->setFrom($pageIndex * $size);
     $query->setSize($size);
     $query->setFields(array('id'));
     Config::apply_filters('searcher_query', $query);
     try {
         $index = Indexer::_index(false);
         $search = new \Elastica\Search($index->getClient());
         $search->addIndex($index);
         if (!$query->hasParam('sort')) {
             if ($sortByDate) {
                 $query->addSort(array('post_date' => 'desc'));
             } else {
                 $query->addSort('_score');
             }
         }
         Config::apply_filters('searcher_search', $search, $query);
         $results = $search->search($query);
         return self::_parseResults($results);
     } catch (\Exception $ex) {
         error_log($ex);
         return null;
     }
 }
Exemplo n.º 2
0
 public function searchDataset($criteria, $sorts = array(), $offset = 0, $limit = 0, $operator = 'OR')
 {
     if ($limit <= 0) {
         $limit = Yii::app()->params['es_search']['limits']['default'];
     }
     $keyword = $criteria['keyword'];
     $eQueryString = new \Elastica\Query\QueryString();
     $eQueryString->setDefaultOperator($operator);
     $eQueryString->setQuery($keyword);
     $eQuery = new \Elastica\Query();
     $eQuery->setQuery($eQueryString);
     $eQuery->setFrom($offset);
     $eQuery->setLimit($limit);
     $filterAnd = new \Elastica\Filter\BoolAnd();
     if (isset($criteria['dataset_type'])) {
         $filterAnd->addFilter($this->newTerms('typenames', $criteria['dataset_type']));
     }
     if (isset($criteria['project'])) {
         $filterAnd->addFilter($this->newTerms('projects', $criteria['project']));
     }
     if (isset($criteria['external_link_type'])) {
         $filterAnd->addFilter($this->newTerms('link_types', $criteria['external_link_type']));
     }
     if (isset($criteria['pubdate_from']) && isset($criteria['pubdate_to'])) {
         $pubfilter = new \Elastica\Filter\Range();
         $pubfilter->addField('publication_date', array('from' => $criteria['pubdate_from'], 'to' => $criteria['pubdate_to']));
         $filterAnd->addFilter($pubfilter);
     } elseif (isset($criteria['pubdate_from'])) {
         $pubfilter = new \Elastica\Filter\Range();
         $pubfilter->addField('publication_date', array('from' => $criteria['pubdate_from']));
         $filterAnd->addFilter($pubfilter);
     } elseif (isset($criteria['pubdate_to'])) {
         $pubfilter = new \Elastica\Filter\Range();
         $pubfilter->addField('publication_date', array('to' => $criteria['pubdate_to']));
         $filterAnd->addFilter($pubfilter);
     }
     $arrayAnd = $filterAnd;
     $arrayAnd = $arrayAnd->toArray();
     if (count($arrayAnd['and']) > 0) {
         $eQuery->setPostFilter($filterAnd);
     }
     $sortList = array_merge(array('_score' => array('order' => 'desc')), $sorts);
     $eQuery->setSort($sortList);
     $index = Yii::app()->elastic->client->getIndex('gigadb');
     $type = $index->getType('dataset');
     $eResultSet = $type->search($eQuery);
     $results = $eResultSet->getResults();
     $total = $eResultSet->getTotalHits();
     $data = array();
     foreach ($results as $result) {
         if ($result) {
             $data[] = $result->getData();
         }
     }
     $re = array('data' => $data, 'total' => $total);
     //Yii::log(print_r($eResultSet, true), 'debug');
     return $re;
 }
Exemplo n.º 3
0
 public function getArticles($user, $searchTerm, $page, $pageOffset, $category, $colors, $finder)
 {
     $boolQuery = new \Elastica\Query\BoolQuery();
     if ($category != NULL) {
         $categoryQuery = new \Elastica\Query\Terms();
         $categoryQuery->setTerms('custdata.customCatRef', array($category));
         $boolQuery->addMust($categoryQuery);
     }
     if ($searchTerm) {
         $fieldQuery = new \Elastica\Query\Match();
         $fieldQuery->setFieldQuery('customerRef', $user->getCustomerRef());
         $boolQuery->addMust($fieldQuery);
         $fieldQuery = new \Elastica\Query\Match();
         $fieldQuery->setFieldQuery('allField', $searchTerm);
         $fieldQuery->setFieldOperator('allField', 'AND');
         $fieldQuery->setFieldMinimumShouldMatch('allField', '70%');
         $fieldQuery->setFieldFuzziness('allField', '0.8');
         $fieldQuery->setFieldAnalyzer('allField', 'custom_search_analyzer');
         $boolQuery->addMust($fieldQuery);
     } else {
         $fieldQuery = new \Elastica\Query\MatchAll();
         $boolQuery->addMust($fieldQuery);
     }
     $fieldQuery = new \Elastica\Query\Nested();
     $fieldQuery->setPath('custdata.custcat.perm');
     $boolNested = new \Elastica\Query\BoolQuery();
     $fieldNested = new \Elastica\Query\Match();
     $fieldNested->setField('custdata.custcat.perm.permStatus', 1);
     $boolNested->addMust($fieldNested);
     $fieldNested = new \Elastica\Query\Match();
     $fieldNested->setField('custdata.custcat.perm.userRef', $user->getId());
     $boolNested->addMust($fieldNested);
     $fieldQuery->setQuery($boolNested);
     $boolQuery->addMust($fieldQuery);
     if ($colors != NULL) {
         $colorQuery = new \Elastica\Query\Terms();
         $colorQuery->setTerms('variants.variantvalues.otherTerms', $colors);
         $colorNested = new \Elastica\Query\Nested('variants');
         $colorNested->setPath('variants.variantvalues');
         $colorNested->setQuery($colorQuery);
         $boolQuery->addMust($colorNested);
     }
     $query = new \Elastica\Query();
     $query->setQuery($boolQuery);
     $query->setSize(12);
     $query->setFrom($pageOffset);
     $articles = $finder->find($query);
     $result = array("articles" => $articles, "rQuery" => $query);
     return $result;
 }
Exemplo n.º 4
0
     $elasticaFilterAnd->addFilter($elasticaTypeOfFilter);
     $filteredQuery = new Elastica\Query\Filtered($queryString, $elasticaFilterAnd);
     $query = new Elastica\Query($filteredQuery);
 }
 //SORT BY
 if (isset($_GET['sort-by'])) {
     if (isset($_GET['sort-by-direction'])) {
         $sortdirection = $_GET['sort-by-direction'];
     } else {
         $sortdirection = "desc";
     }
     $sort = array($_GET['sort-by'] => array("order" => $sortdirection));
     //order by clause
     $query->setSort($sort);
 }
 $query->setFrom($_GET['from'])->setLimit($_GET['size']);
 if (isset($_GET['debug'])) {
     echo '<pre>';
     print_r($query);
     echo '</pre>';
 }
 // Configure and execute the search
 // Create the search object and inject the client
 $resultSet = $searchThrough->search($query);
 $data = array();
 foreach ($resultSet as $result) {
     $itemdata = $result->getData();
     $itemdata['id'] = $result->getId();
     $data[] = $itemdata;
 }
 $response = array();
Exemplo n.º 5
0
 /**
  *   To respect the design, 3 searches will be executed:
  *       1st:    retrieves the main facet (Text / Media / Portfolio / Users / Group) and the count for each of them
  *       2nd:    - retrieves the results of the first non empty facet term for display in the tab
  *               - retrieves the secondary facet to enable / disable the filter items
  *       3nd:    - retrieves the results with all filters applied
  * @param unknown $query_string
  * @param unknown $limit
  * @param unknown $offset
  * @param unknown $options
  * @param unknown $mainfacetterm
  * @param unknown $USER
  * @return multitype:number boolean unknown Ambigous <boolean, NULL> Ambigous <boolean, unknown> multitype:multitype:string number   Ambigous <string, unknown> |multitype:multitype:
  */
 public static function search($query_string, $limit, $offset, $options, $mainfacetterm, $USER)
 {
     $result = array('count' => 0, 'limit' => $limit, 'offset' => $offset, 'data' => false, 'selected' => isset($mainfacetterm) && strlen($mainfacetterm) > 0 ? $mainfacetterm : false, 'totalresults' => 0, 'facets' => array(array('term' => "Text", 'count' => 0, 'display' => "Text"), array('term' => "Media", 'count' => 0, 'display' => "Media"), array('term' => "Portfolio", 'count' => 0, 'display' => "Portfolio"), array('term' => "User", 'count' => 0, 'display' => "Users"), array('term' => "Group", 'count' => 0, 'display' => "Group")), 'content-filter' => array(array('term' => "all", 'count' => 0, 'display' => "All"), array('term' => "Audio", 'count' => 0, 'display' => "Audio"), array('term' => "Comment", 'count' => 0, 'display' => "Comment"), array('term' => "Document", 'count' => 0, 'display' => "Document"), array('term' => "Folder", 'count' => 0, 'display' => "Folder"), array('term' => "Forum", 'count' => 0, 'display' => "Forum"), array('term' => "Forumpost", 'count' => 0, 'display' => "Forum post"), array('term' => "Image", 'count' => 0, 'display' => "Image"), array('term' => "Journal", 'count' => 0, 'display' => "Journal"), array('term' => "Journalentry", 'count' => 0, 'display' => "Journal entry"), array('term' => "Note", 'count' => 0, 'display' => "Note"), array('term' => "Plan", 'count' => 0, 'display' => "Plan"), array('term' => "Profile", 'count' => 0, 'display' => "Profile"), array('term' => "Resume", 'count' => 0, 'display' => "Résumé"), array('term' => "Video", 'count' => 0, 'display' => "Video"), array('term' => "Wallpost", 'count' => 0, 'display' => "Wall post"), array('term' => "Collection", 'count' => 0, 'display' => "Collection"), array('term' => "Page", 'count' => 0, 'display' => "Page")), 'content-filter-selected' => isset($options['secfacetterm']) && strlen($options['secfacetterm']) > 0 ? $options['secfacetterm'] : 'all', 'owner-filter' => array(array('term' => "all", 'count' => 0, 'display' => "All"), array('term' => "me", 'count' => 0, 'display' => "Me"), array('term' => "others", 'count' => 0, 'display' => "Others")), 'owner-filter-selected' => isset($options['owner']) && strlen($options['owner']) > 0 ? $options['owner'] : 'all', 'tagsonly' => isset($options['tagsonly']) && $options['tagsonly'] == true ? true : Null, 'sort' => isset($options['sort']) && strlen($options['sort']) > 0 ? $options['sort'] : 'score', 'license' => isset($options['license']) && strlen($options['license']) > 0 ? $options['license'] : 'all');
     if (strlen($query_string) <= 0) {
         return $result;
     }
     //      1- Get main facet
     // ------------------------------------------------------------------------------------------
     $records = array();
     $elasticaClient = PluginSearchElasticsearch::make_client();
     $elasticaIndex = $elasticaClient->getIndex(get_config_plugin('search', 'elasticsearch', 'indexname'));
     $elasticaQueryString = new \Elastica\Query\QueryString();
     $elasticaAnalyzer = get_config_plugin('search', 'elasticsearch', 'analyzer');
     $elasticaQueryString->setAnalyzer($elasticaAnalyzer);
     $elasticaQueryString->setDefaultOperator('AND');
     $elasticaQueryString->setQuery($query_string);
     // if tags only => set fields to tags
     if ($result['tagsonly'] === true) {
         $elasticaQueryString->setFields(array('tags'));
     }
     // Create the $elasticaQuery object
     $elasticaQuery = new \Elastica\Query();
     $elasticaQuery->setFrom($offset);
     $elasticaQuery->setLimit($limit);
     $elasticaQuery->setQuery($elasticaQueryString);
     $elasticaFilterAnd = new \Elastica\Filter\BoolAnd();
     // Apply ACL filters
     $elasticaFilterACL = new ElasticsearchFilterAcl($USER);
     $elasticaFilterAnd->addFilter($elasticaFilterACL);
     $elasticaQuery->setFilter($elasticaFilterAnd);
     // Define a new facet: mainFacetTerm  - WARNING: don't forget to apply the same filter to the facet
     $elasticaFacet = new \Elastica\Facet\Terms('mainFacetTerm');
     $elasticaFacet->setField('mainfacetterm');
     $elasticaFacet->setOrder('count');
     $elasticaFacet->setFilter($elasticaFilterAnd);
     $elasticaQuery->addFacet($elasticaFacet);
     $elasticaResultSet = $elasticaIndex->search($elasticaQuery);
     $result['totalresults'] = $elasticaResultSet->getTotalHits();
     $elasticaFacets = $elasticaResultSet->getFacets();
     $facets = self::process_facets($elasticaFacets['mainFacetTerm']['terms']);
     if (count($facets) == 0) {
         return $result;
     }
     array_walk($result['facets'], 'self::process_tabs', $facets);
     if ($result['selected'] === false || $facets[$result['selected']] == 0) {
         $result['selected'] = self::get_selected_facet($result['facets']);
     }
     //      2- Retrieve results of selected facet
     // ------------------------------------------------------------------------------------------
     $elasticaFilterType = new \Elastica\Filter\Term(array('mainfacetterm' => $result['selected']));
     $elasticaFilterAnd->addFilter($elasticaFilterType);
     $elasticaQuery->setFilter($elasticaFilterAnd);
     // Define a new facet: secFacetTerm  - WARNING: don't forget to apply the same filter to the facet
     $elasticaFacet = new \Elastica\Facet\Terms('secFacetTerm');
     $elasticaFacet->setField('secfacetterm');
     $elasticaFacet->setOrder('count');
     $elasticaFacet->setFilter($elasticaFilterAnd);
     $elasticaQuery->addFacet($elasticaFacet);
     // Sorting
     // Sorting is defined on a per field level, so we must make sure the field exists in the mapping
     $sort = explode('_', $result['sort']);
     if ($sort[0] == 'score') {
         $sort[0] = '_score';
     }
     // set the second column to sort by the score (to break any 'ties').
     $elasticaQuery->setSort(array(array($sort[0] => array('order' => isset($sort[1]) ? $sort[1] : 'desc')), array('_score' => array('order' => 'desc'))));
     $elasticaResultSet = $elasticaIndex->search($elasticaQuery);
     $result['count'] = $elasticaResultSet->getTotalHits();
     $elasticaFacets = $elasticaResultSet->getFacets();
     $facets = $elasticaFacets['secFacetTerm']['terms'];
     $facets = self::process_facets($elasticaFacets['secFacetTerm']['terms']);
     array_walk($result['content-filter'], 'self::process_tabs', $facets);
     // set the count of "all" to the total hits
     $result['content-filter'][0]['count'] = $result['count'];
     //      3- Apply filters and retrieve final results
     // ------------------------------------------------------------------------------------------
     // Apply Content filter if different from "all"
     if ($result['content-filter-selected'] != 'all') {
         $elasticaFilterContent = new \Elastica\Filter\Term(array('secfacetterm' => $result['content-filter-selected']));
         $elasticaFilterAnd->addFilter($elasticaFilterContent);
     }
     // Apply Owner filter if different from "all"
     if ($result['owner-filter-selected'] != 'all') {
         $uid = $USER->get('id');
         $elasticaFilterOwner = new \Elastica\Filter\Term(array('owner' => $uid));
         if ($result['owner-filter-selected'] == 'others') {
             $elasticaFilterOwner = new \Elastica\Filter\BoolNot($elasticaFilterOwner);
         }
         $elasticaFilterAnd->addFilter($elasticaFilterOwner);
     }
     // Apply license filter if different from "all"
     if ($result['license'] != 'all') {
         $elasticaFilterLicense = new \Elastica\Filter\Term(array('license' => $result['license']));
         $elasticaFilterAnd->addFilter($elasticaFilterLicense);
     }
     $elasticaQuery->setFilter($elasticaFilterAnd);
     $elasticaResultSet = $elasticaIndex->search($elasticaQuery);
     $elasticaResults = $elasticaResultSet->getResults();
     $result['count'] = $elasticaResultSet->getTotalHits();
     foreach ($elasticaResults as $elasticaResult) {
         $tmp = array();
         $tmp['type'] = $elasticaResult->getType();
         $ES_class = 'ElasticsearchType_' . $tmp['type'];
         $tmp = $tmp + $elasticaResult->getData();
         // Get all the data from the DB table
         $dbrec = $ES_class::getRecordDataById($tmp['type'], $tmp['id']);
         if ($dbrec) {
             $tmp['db'] = $dbrec;
             $tmp['db']->deleted = false;
         } else {
             // If the record has been deleted, so just pass the cached data
             // from the search result. Let the template decide how to handle
             // it.
             $tmp['db'] = (object) $tmp;
             $tmp['db']->deleted = true;
         }
         $records[] = $tmp;
     }
     $result['data'] = $records;
     return $result;
 }
 /**
  * Run the search against a sanitized query string via ElasticSearch.
  *
  * @param string $string
  * @param array $scriptProperties The scriptProperties array from the SimpleSearch snippet
  * @return array
  */
 public function search($string, array $scriptProperties = array())
 {
     $fields = $this->modx->getOption('sisea.elastic.search_fields', null, 'pagetitle^20,introtext^10,alias^5,content^1');
     $fields = explode(',', $fields);
     $fields = array_map('trim', $fields);
     $fields = array_keys(array_flip($fields));
     $fields = array_filter($fields);
     if (empty($fields)) {
         return false;
     }
     /** @var \Elastica\Query\MultiMatch $query */
     $query = new \Elastica\Query\MultiMatch();
     $query->setFields($fields);
     $query->setQuery($string);
     $customFilterScore = new \Elastica\Query\CustomFiltersScore();
     $customFilterScore->setQuery($query);
     $searchBoosts = $this->modx->getOption('sisea.elastic.search_boost', null, '');
     $searchBoosts = explode('|', $searchBoosts);
     $searchBoosts = array_map('trim', $searchBoosts);
     $searchBoosts = array_keys(array_flip($searchBoosts));
     $searchBoosts = array_filter($searchBoosts);
     $boosts = array();
     foreach ($searchBoosts as $boost) {
         $arr = array('field' => '', 'value' => '', 'boost' => 1.0);
         $field = explode('=', $boost);
         $field = array_map('trim', $field);
         $field = array_keys(array_flip($field));
         $field = array_filter($field);
         if (count($field) != 2) {
             continue;
         }
         $value = explode('^', $field[1]);
         $value = array_map('trim', $value);
         $value = array_keys(array_flip($value));
         $value = array_filter($value);
         if (count($value) != 2) {
             continue;
         }
         $arr['field'] = $field[0];
         $arr['value'] = $value[0];
         $arr['boost'] = $value[1];
         $boosts[] = $arr;
     }
     if (empty($boosts)) {
         $customFilterScore->addFilter(new \Elastica\Filter\Term(array('type' => 'document')), 1);
     } else {
         foreach ($boosts as $boost) {
             $customFilterScore->addFilter(new \Elastica\Filter\Term(array($boost['field'] => $boost['value'])), $boost['boost']);
         }
     }
     /** @var \Elastica\Query $elasticaQuery */
     $elasticaQuery = new \Elastica\Query();
     $elasticaQuery->setQuery($customFilterScore);
     /* set limit */
     $perPage = $this->modx->getOption('perPage', $scriptProperties, 10);
     if (!empty($perPage)) {
         $offset = $this->modx->getOption('start', $scriptProperties, 0);
         $offsetIndex = $this->modx->getOption('offsetIndex', $scriptProperties, 'sisea_offset');
         if (isset($_REQUEST[$offsetIndex])) {
             $offset = (int) $_REQUEST[$offsetIndex];
         }
         $elasticaQuery->setFrom($offset);
         $elasticaQuery->setSize($perPage);
     }
     $elasticaFilterAnd = new \Elastica\Filter\BoolAnd();
     /* handle hidemenu option */
     $hideMenu = $this->modx->getOption('hideMenu', $scriptProperties, 2);
     if ($hideMenu != 2) {
         $elasticaFilterHideMenu = new \Elastica\Filter\Term();
         $elasticaFilterHideMenu->setTerm('hidemenu', $hideMenu ? 1 : 0);
         $elasticaFilterAnd->addFilter($elasticaFilterHideMenu);
     }
     /* handle contexts */
     $contexts = $this->modx->getOption('contexts', $scriptProperties, '');
     $contexts = !empty($contexts) ? $contexts : $this->modx->context->get('key');
     $contexts = explode(',', $contexts);
     $elasticaFilterContext = new \Elastica\Filter\Terms();
     $elasticaFilterContext->setTerms('context_key', $contexts);
     $elasticaFilterAnd->addFilter($elasticaFilterContext);
     /* handle restrict search to these IDs */
     $ids = $this->modx->getOption('ids', $scriptProperties, '');
     if (!empty($ids)) {
         $idType = $this->modx->getOption('idType', $this->config, 'parents');
         $depth = $this->modx->getOption('depth', $this->config, 10);
         $ids = $this->processIds($ids, $idType, $depth);
         $elasticaFilterId = new \Elastica\Filter\Term();
         $elasticaFilterId->setTerm('id', $ids);
         $elasticaFilterAnd->addFilter($elasticaFilterId);
     }
     /* handle exclude IDs from search */
     $exclude = $this->modx->getOption('exclude', $scriptProperties, '');
     if (!empty($exclude)) {
         $exclude = $this->cleanIds($exclude);
         $exclude = explode(',', $exclude);
         $elasticaFilterExcludeId = new \Elastica\Filter\Term();
         $elasticaFilterExcludeId->setTerm('id', $exclude);
         $elasticaFilterNotId = new \Elastica\Filter\BoolNot($elasticaFilterExcludeId);
         $elasticaFilterAnd->addFilter($elasticaFilterNotId);
     }
     /* basic always-on conditions */
     $elasticaFilterPublished = new \Elastica\Filter\Term();
     $elasticaFilterPublished->setTerm('published', 1);
     $elasticaFilterAnd->addFilter($elasticaFilterPublished);
     $elasticaFilterSearchable = new \Elastica\Filter\Term();
     $elasticaFilterSearchable->setTerm('searchable', 1);
     $elasticaFilterAnd->addFilter($elasticaFilterSearchable);
     $elasticaFilterDeleted = new \Elastica\Filter\Term();
     $elasticaFilterDeleted->setTerm('deleted', 0);
     $elasticaFilterAnd->addFilter($elasticaFilterDeleted);
     $elasticaQuery->setFilter($elasticaFilterAnd);
     /* sorting */
     if (!empty($scriptProperties['sortBy'])) {
         $sortDir = $this->modx->getOption('sortDir', $scriptProperties, 'desc');
         $sortDirs = explode(',', $sortDir);
         $sortBys = explode(',', $scriptProperties['sortBy']);
         $dir = 'desc';
         $sortArray = array();
         for ($i = 0; $i < count($sortBys); $i++) {
             if (isset($sortDirs[$i])) {
                 $dir = $sortDirs[$i];
             }
             $sortArray[] = array($sortBys[$i] => $dir);
         }
         $elasticaQuery->setSort($sortArray);
     }
     /* prepare response array */
     $response = array('total' => 0, 'start' => !empty($offset) ? $offset : 0, 'limit' => $perPage, 'status' => 0, 'query_time' => 0, 'results' => array());
     $elasticaResultSet = $this->index->search($elasticaQuery);
     $elasticaResults = $elasticaResultSet->getResults();
     $totalResults = $elasticaResultSet->getTotalHits();
     if ($totalResults > 0) {
         $response['total'] = $totalResults;
         $response['query_time'] = $elasticaResultSet->getTotalTime();
         $response['status'] = 1;
         $response['results'] = array();
         foreach ($elasticaResults as $doc) {
             $d = $doc->getData();
             /** @var modResource $resource */
             $resource = $this->modx->newObject($d['class_key']);
             if ($resource->checkPolicy('list')) {
                 $response['results'][] = $d;
             }
         }
     }
     return $response;
 }
Exemplo n.º 7
0
 public function getArticles($user, $searchTerm, $page, $pageOffset, $category, $orderby, $colors, $finder, $elasticIndex)
 {
     $boolQuery = new \Elastica\Query\BoolQuery();
     if ($category != NULL) {
         $query = $this->getEntityManager()->createQuery("SELECT c.id FROM OrthIndexBundle:Categories c WHERE c.id LIKE :category")->setParameter('category', $category . "%");
         $queryResult = $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
         foreach ($queryResult as $categoryId) {
             $categoryArray[] = $categoryId['id'];
         }
         $categoryQuery = new \Elastica\Query\Terms();
         $categoryQuery->setTerms('catRef', $categoryArray);
         $boolQuery->addMust($categoryQuery);
     }
     if ($searchTerm) {
         $fieldQuery = new \Elastica\Query\Match();
         $fieldQuery->setFieldQuery('allField', $searchTerm);
         $fieldQuery->setFieldOperator('allField', 'AND');
         $fieldQuery->setFieldMinimumShouldMatch('allField', '70%');
         $fieldQuery->setFieldFuzziness('allField', '0.8');
         $fieldQuery->setFieldAnalyzer('allField', 'custom_search_analyzer');
         $boolQuery->addMust($fieldQuery);
     }
     if ($colors != NULL) {
         $colorQuery = new \Elastica\Query\Terms();
         $colorQuery->setTerms('variants.variantvalues.otherTerms', $colors);
         $colorNested = new \Elastica\Query\Nested('variants');
         $colorNested->setPath('variants.variantvalues');
         $colorNested->setQuery($colorQuery);
         $boolQuery->addMust($colorNested);
     }
     $agg = new \Elastica\Aggregation\Terms("catRef");
     $agg->setSize(5000);
     $agg->setField('catRef');
     $boolFilter = new \Elastica\Filter\Bool();
     if ($user == "anon.") {
         $boolFilter->addShould(new \Elastica\Filter\Terms('customized', array(0)));
     } else {
         $boolFilter->addShould(new \Elastica\Filter\Terms('customized', array(0, $user->getCustomerRef())));
     }
     $filtered = new \Elastica\Query\Filtered($boolQuery, $boolFilter);
     $query = new \Elastica\Query();
     $query->setQuery($filtered);
     //if( $colors != NULL) {
     //  $query->setFilter($colorNested);
     //}
     if ($orderby == 'desc') {
         $query->setSort(array('variants.price' => array('order' => 'desc')));
     } elseif ($orderby == 'asc') {
         $query->setSort(array('variants.price' => array('order' => 'asc')));
     }
     $query->addAggregation($agg);
     $query->setSize(12);
     $query->setFrom($pageOffset);
     $articles = $finder->find($query);
     $aggregations = $elasticIndex->search($query);
     $result = array("articles" => $articles, "aggs" => $aggregations, "rQuery" => $query);
     return $result;
 }
Exemplo n.º 8
0
 public function searchLoans($conditions = array(), $page = 1, $limit = 20)
 {
     $conditions += ['search' => false];
     $search = $conditions['search'];
     unset($conditions['search']);
     $queryString = new \Elastica\Query\QueryString();
     $loanIndex = $this->getLoanIndex();
     $query = new \Elastica\Query();
     if ($search) {
         $queryString->setDefaultOperator('AND');
         $queryString->setQuery($search);
         $query->setQuery($queryString);
     }
     $filterAnd = new \Elastica\Filter\BoolAnd();
     foreach ($conditions as $field => $value) {
         $termFilter = new \Elastica\Filter\Term();
         $termFilter->setTerm($field, $value);
         $filterAnd->addFilter($termFilter);
     }
     if ($conditions) {
         $query->setFilter($filterAnd);
     }
     $query->setFrom(($page - 1) * $limit);
     $query->setSize($page * $limit);
     $results = $loanIndex->search($query);
     $ids = [];
     foreach ($results as $result) {
         $data = $result->getData();
         $ids[$data['id']] = $data['id'];
     }
     $loans = LoanQuery::create()->filterById($ids)->find();
     $sortedLoans = $ids;
     foreach ($loans as $loan) {
         $sortedLoans[$loan->getId()] = $loan;
     }
     $sortedLoans = array_filter($sortedLoans, function ($l) {
         return !is_scalar($l);
     });
     $paginatorFactory = \App::make('paginator');
     return $paginatorFactory->make($sortedLoans, $results->getTotalHits(), $limit);
 }
Exemplo n.º 9
0
 /**
  * Main method to make a search
  */
 public function search()
 {
     $this->getSearchFields();
     $word = JRequest::getString('searchword', null, 'get');
     $offset = JRequest::getInt('start', 0, 'get');
     $limit = JRequest::getInt('limit', 10, 'get');
     if ($limit == 0) {
         // no limit
         $limit = 10000;
     }
     $this->getSearchAreas();
     $elasticaQueryString = new \Elastica\Query\QueryString();
     // Log search word only on the first page
     if ($offset == 0) {
         SearchHelper::logSearch($word);
     }
     // Convert accents
     $word = htmlentities($word, ENT_NOQUOTES, 'utf-8');
     $word = preg_replace('#\\&([A-za-z])(?:uml|circ|tilde|acute|grave|cedil|ring)\\;#', '\\1', $word);
     $word = preg_replace('#\\&([A-za-z]{2})(?:lig)\\;#', '\\1', $word);
     $word = preg_replace('#\\&[^;]+\\;#', '', $word);
     // Check if there are quotes ( for exact search )
     $exactSearch = false;
     if (strlen($word) > 1 && $word[0] == '"' && $word[strlen($word) - 1] == '"') {
         $exactSearch = true;
         $word = substr($word, 1, strlen($word) - 2);
         // Remove external "
     }
     $word = Elastica\Util::replaceBooleanWordsAndEscapeTerm($word);
     // Escape ElasticSearch specials char
     if ($exactSearch) {
         $word = '"' . $word . '"';
     }
     if ($word == "") {
         $word = "*";
     }
     $elasticaQueryString->setQuery($word);
     //Create the actual search object with some data.
     $elasticaQuery = new Elastica\Query();
     $elasticaQuery->setQuery($elasticaQueryString);
     if (ElasticSearchConfig::getHighLigthEnable()) {
         $fields = $this->getHighlightFields();
         $hlfields = array();
         foreach ($fields as $field) {
             foreach ($field as $highlight) {
                 $hlfields[] = array($highlight => array('fragment_size' => 1000, 'number_of_fragments' => 1));
             }
         }
         $highlightFields = array('pre_tags' => array(ElasticSearchConfig::getHighLigthPre()), 'post_tags' => array(ElasticSearchConfig::getHighLigthPost()), "order" => "score", 'fields' => $hlfields);
         $elasticaQuery->setHighlight($highlightFields);
     }
     // Set offset and limit for pagination
     $elasticaQuery->setFrom($offset);
     $elasticaQuery->setLimit($limit);
     //Create a filter for _type
     $elasticaFilterype = $this->createFilterType($this->areas);
     // Add filter to the search object.
     $elasticaQuery->setFilter($elasticaFilterype);
     //Search on the index.
     $elasticaResultSet = $this->elasticaIndex->search($elasticaQuery);
     $this->results = $elasticaResultSet->getResults();
     $this->totalHits = $elasticaResultSet->getTotalHits();
 }
 public function search($queryString, $opts, $highlight)
 {
     $query = new \Elastica\Query();
     list($searchQuery, $highlights) = $this->parseQueryString($queryString, $opts);
     $query->setQuery($searchQuery);
     $language = new \Elastica\Facet\Terms('language');
     $language->setField('language');
     $language->setSize(500);
     $query->addFacet($language);
     $group = new \Elastica\Facet\Terms('group');
     $group->setField('group');
     // Would like to prioritize the top level groups and not show subgroups
     // if the top group has only few hits, but that doesn't seem to be possile.
     $group->setSize(500);
     $query->addFacet($group);
     $query->setSize($opts['limit']);
     $query->setFrom($opts['offset']);
     // BoolAnd filters are executed in sequence per document. Bool filters with
     // multiple must clauses are executed by converting each filter into a bit
     // field then anding them together. The latter is normally faster if either
     // of the subfilters are reused. May not make a difference in this context.
     $filters = new \Elastica\Filter\Bool();
     $language = $opts['language'];
     if ($language !== '') {
         $languageFilter = new \Elastica\Filter\Term();
         $languageFilter->setTerm('language', $language);
         $filters->addMust($languageFilter);
     }
     $group = $opts['group'];
     if ($group !== '') {
         $groupFilter = new \Elastica\Filter\Term();
         $groupFilter->setTerm('group', $group);
         $filters->addMust($groupFilter);
     }
     // Check that we have at least one filter to avoid invalid query errors.
     if ($language !== '' || $group !== '') {
         $query->setPostFilter($filters);
     }
     list($pre, $post) = $highlight;
     $query->setHighlight(array('pre_tags' => array($pre), 'post_tags' => array($post), 'fields' => $highlights));
     try {
         return $this->getType()->getIndex()->search($query);
     } catch (\Elastica\Exception\ExceptionInterface $e) {
         throw new TTMServerException($e->getMessage());
     }
 }