/**
  * @return array
  */
 public function getFilters()
 {
     if (!empty($this->filters)) {
         $boolFilter = new \Elastica\Filter\BoolAnd();
         foreach ($this->filters as $filter) {
             $boolFilter->addFilter($filter);
         }
         return $boolFilter;
     }
 }
Ejemplo n.º 2
0
 /**
  * render collection filters
  *
  * @return Hackathon_ElasticgentoCore_Model_Resource_Collection
  */
 protected function _renderFilters()
 {
     $this->_queryFilter = new Elastica\Filter\BoolAnd();
     foreach ($this->_queryAttributeFilters as $filter) {
         $this->_queryFilter->addFilter($filter);
     }
     return $this;
 }
Ejemplo n.º 3
0
     $query_collection = new Elastica\Query\Match();
     $query_collection->setFieldQuery('content_type', $_GET['collection']);
     $elasticaTypeOfFilter = new Elastica\Filter\Query();
     $elasticaTypeOfFilter->setQuery($query_collection);
     $elasticaFilterAnd = new Elastica\Filter\BoolAnd();
     $elasticaFilterAnd->addFilter($elasticaTypeOfFilter);
     $filteredQuery = new Elastica\Query\Filtered($queryString, $elasticaFilterAnd);
 }
 $query = new Elastica\Query($filteredQuery);
 if (isset($_GET['speaker'])) {
     $boolQuery = new Elastica\Query\Bool();
     $query_collection = new Elastica\Query\Match();
     $query_collection->setFieldQuery('speaker', $_GET['speaker'])->setFieldParam('speaker', 'type', 'phrase');
     $elasticaTypeOfFilter = new Elastica\Filter\Query();
     $elasticaTypeOfFilter->setQuery($query_collection);
     $elasticaFilterAnd = new Elastica\Filter\BoolAnd();
     $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);
 }
Ejemplo n.º 4
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;
 }
Ejemplo n.º 5
0
 public function search(SiteSearch $siteSearch)
 {
     // We create a query to return all the articles but if the criteria text is specified, we use it
     if ($siteSearch->getText() != null && $siteSearch != '') {
         $baseQuery = new \Elastica\Query\MultiMatch();
         $baseQuery->setQuery($siteSearch->getText())->setFields(array('title', 'subtitle', 'courseContent', 'content'));
         $baseQuery->setFuzziness(0.7);
         $baseQuery->setMinimumShouldMatch('80%');
     } else {
         $baseQuery = new \Elastica\Query\MatchAll();
     }
     // Then we create filters depending on the chosen criterias
     // Filter courses only if type is not "product"
     $productTypeFilter = new \Elastica\Filter\Type();
     $productTypeFilter->setType('product');
     $productNotFilter = new \Elastica\Filter\BoolNot($productTypeFilter);
     // Filter for products with available courses
     $nestedFilter = new \Elastica\Filter\Nested();
     $nestedFilter->setPath('courses');
     $nestedFilter->setQuery(new \Elastica\Query\Range('beginDate', array('gte' => \Elastica\Util::convertDate((new \DateTime())->getTimestamp()))));
     // Filter not(products) OR products with available courses
     $orFilter = new \Elastica\Filter\BoolOr();
     $orFilter->addFilter($productNotFilter);
     $orFilter->addFilter($nestedFilter);
     // Create a bool filter to put everything together
     $boolFilter = new \Elastica\Filter\Bool();
     $boolFilter->addMust($orFilter);
     // Filter type
     if ($siteSearch->getIsProduct() || $siteSearch->getIsInfoEvent() || $siteSearch->getIsContent()) {
         // Create OR filter to put together the types
         $typeOrFilter = new \Elastica\Filter\BoolOr();
         // Filter products
         if ($siteSearch->getIsProduct()) {
             $productAndFilter = new \Elastica\Filter\BoolAnd();
             $productAndFilter->addFilter($productTypeFilter);
             $infoFilter = new \Elastica\Filter\Term(array('infoVa' => false));
             $productAndFilter->addFilter($infoFilter);
             $typeOrFilter->addFilter($productAndFilter);
         }
         // Filter info events if isProduct is not selected
         if ($siteSearch->getIsInfoEvent()) {
             $productAndFilter = new \Elastica\Filter\BoolAnd();
             $productAndFilter->addFilter($productTypeFilter);
             $infoFilter = new \Elastica\Filter\Term(array('infoVa' => true));
             $productAndFilter->addFilter($infoFilter);
             $typeOrFilter->addFilter($productAndFilter);
         }
         // Filter content
         if ($siteSearch->getIsContent()) {
             $typeOrFilter->addFilter($productNotFilter);
         }
         $boolFilter->addMust($typeOrFilter);
     }
     // Filter product type
     if ($siteSearch->getProductType()) {
         $productTypeFilter = new \Elastica\Filter\Nested();
         $productTypeFilter->setPath('productType');
         $productTypeFilter->setFilter(new \Elastica\Filter\Term(array('productType._id' => $siteSearch->getProductType()->getId())));
         $boolFilter->addMust($productTypeFilter);
     }
     // Filter day time
     if ($siteSearch->getDayTime()) {
         $dayTimeFilter = new \Elastica\Filter\Nested();
         $dayTimeFilter->setPath('courses');
         $dayTimeFilter->setFilter(new \Elastica\Filter\Term(array('courses.dayTimes' => $siteSearch->getDayTime())));
         $boolFilter->addMust($dayTimeFilter);
     }
     // Filter category
     if ($siteSearch->getCategory()) {
         $categoryFilter = new \Elastica\Filter\BoolOr();
         $mainCategoryFilter = new \Elastica\Filter\Nested();
         $mainCategoryFilter->setPath('category');
         $mainCategoryFilter->setFilter(new \Elastica\Filter\Term(array('category._id' => $siteSearch->getCategory()->getId())));
         $subCategoryFilter = new \Elastica\Filter\Nested();
         $subCategoryFilter->setPath('subcategory');
         $subCategoryFilter->setFilter(new \Elastica\Filter\Term(array('subcategory._id' => $siteSearch->getCategory()->getId())));
         $additionalCategoryFilter = new \Elastica\Filter\Nested();
         $additionalCategoryFilter->setPath('additionalCategories');
         $additionalCategoryFilter->setFilter(new \Elastica\Filter\Term(array('additionalCategories._id' => $siteSearch->getCategory()->getId())));
         $categoryFilter->addFilter($mainCategoryFilter);
         $categoryFilter->addFilter($subCategoryFilter);
         $categoryFilter->addFilter($additionalCategoryFilter);
         $boolFilter->addMust($categoryFilter);
     }
     $filtered = new \Elastica\Query\Filtered($baseQuery, $boolFilter);
     $query = \Elastica\Query::create($filtered);
     $sort = $siteSearch->getSort();
     if (!empty($sort)) {
         $sort = explode(' ', $sort);
         $query->setSort(array($sort[0] => array('order' => $sort[1]), "_score" => array('order' => 'desc')));
     }
     $paginated = $this->finder->findPaginated($query);
     $paginated->setMaxPerPage($siteSearch->getPerPage())->setCurrentPage($siteSearch->getPage());
     return $paginated;
 }
 /**
  * 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;
 }
 /**
  * Get views linked to a particular artefact, applying ACL
  * This is used to display the list of views in an artefact result, because it's faster to retrieve the info
  * from Elastic search that running the SQL query.
  */
 public static function views_by_artefact_acl_filter($views = array())
 {
     global $USER;
     $ret = array();
     $elasticaClient = PluginSearchElasticsearch::make_client();
     $elasticaIndex = $elasticaClient->getIndex(get_config_plugin('search', 'elasticsearch', 'indexname'));
     $elasticaQuery = new \Elastica\Query();
     // check user access to the views
     $elasticaFilterAnd = new \Elastica\Filter\BoolAnd();
     $elasticaFilterType = new \Elastica\Filter\Term(array('_type' => 'view'));
     $elasticaFilterAnd->addFilter($elasticaFilterType);
     $elasticaFilterIds = new \Elastica\Filter\Terms('id', array_keys($views));
     $elasticaFilterAnd->addFilter($elasticaFilterIds);
     // Apply ACL filters
     $elasticaFilterACL = new ElasticsearchFilterAcl($USER);
     $elasticaFilterAnd->addFilter($elasticaFilterACL);
     $elasticaFilteredQuery = new \Elastica\Query\Filtered(null, $elasticaFilterAnd);
     $elasticaQuery->setQuery($elasticaFilteredQuery);
     $elasticaResultSet = $elasticaIndex->search($elasticaQuery);
     $elasticaResults = $elasticaResultSet->getResults();
     foreach ($elasticaResults as $elasticaResult) {
         $data = $elasticaResult->getData();
         $ret[$data['id']] = $views[$data['id']];
     }
     return $ret;
 }
Ejemplo n.º 8
0
 public function searchSample($criteria, $dataset, $sorts = array(), $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['common_name'])) {
         $filterAnd->addFilter($this->newTerms('common_name', $criteria['common_name']));
     }
     if (is_array($dataset) && !empty($dataset)) {
         $ids = new \Elastica\Filter\Ids();
         $ids->setIds($dataset);
         $filterAnd->addFilter($ids);
     }
     $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('sample');
     $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($re, true), 'debug');
     return $re;
 }
Ejemplo n.º 9
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);
 }
Ejemplo n.º 10
0
 /**
  * @SWG\Api(
  *   path="/posts/search",
  *   @SWG\Operation(
  *     method="POST",
  *     summary="Search items in posts",
  *     nickname="posts_search",
  *      @SWG\Parameter(
  *           name="post_type",
  *           required=false,
  *           type="string"
  *         ),
  *      @SWG\Parameter(
  *           name="filters",
  *           description="filter values, eg. [{my_parameter: 'mustmatchthis'}]
  *           required=false,
  *           type="array[]"
  *         ),
  *      @SWG\Parameter(
  *           name="search_phrase",
  *           required=false,
  *           type="string"
  *         ),
  *      @SWG\Parameter(
  *           name="sort",
  *           description="sort configuration, eg [{'post_name':{'order':'asc'}}]",
  *           required=false,
  *           type="array[]"
  *         ),
  *      @SWG\Parameter(
  *           name="limit",
  *           description="Max number of documents in return",
  *           required=false,
  *           type="integer"
  *         )
  *   )
  * )
  */
 function search()
 {
     $result = array();
     $body = $this->getBodyAsArray();
     $required_parameters = array();
     $optional_parameters = array('post_type', 'filters', 'search_phrase', 'sort', 'limit', 'size', 'from');
     $this->check_req_opt_param($required_parameters, $optional_parameters, $body);
     $index = $this->elastica->getIndex($this->getElasticsearchIndex());
     $query = new \Elastica\Query();
     $boolean = new \Elastica\Query\Bool();
     $added = false;
     $type = isset($body['post_type']) ? $body['post_type'] : null;
     if ($type) {
         $q = new \Elastica\Query\Term(array('_type' => $type));
         $boolean->addMust($q);
         $added = true;
     }
     $filters = isset($body['filters']) ? (array) $body['filters'] : array();
     $filters = array_filter($filters);
     $filterAnd = '';
     if ($filters) {
         foreach ($filters as $key => $vals) {
             if (!is_array($vals)) {
                 $vals = array($vals);
             }
             if (strpos(implode('', $vals), '*')) {
                 foreach ($vals as $val) {
                     $extraQuery = new \Elastica\Query\SimpleQueryString($val, array($key));
                     $boolean->addMust($extraQuery);
                 }
             } else {
                 $filterOr = new \Elastica\Filter\BoolOr();
                 foreach ($vals as $val) {
                     $filter = new \Elastica\Filter\Term();
                     $filter->setTerm($key, $val);
                     $filterOr->addFilter($filter);
                 }
                 if (!$filterAnd) {
                     $filterAnd = new \Elastica\Filter\BoolAnd();
                 }
                 $filterAnd->addFilter($filterOr);
             }
         }
     }
     $search_phrase = isset($body['search_phrase']) ? $body['search_phrase'] : null;
     if (isset($search_phrase) && !empty($search_phrase)) {
         $word = strtolower($search_phrase) . '*';
         $elasticaQueryString = new \Elastica\Query\SimpleQueryString($word);
         $boolean->addMust($elasticaQueryString);
         $added = true;
     }
     if ($added) {
         $query->setQuery($boolean);
     }
     if ($filterAnd) {
         $query->setPostFilter($filterAnd);
     }
     $limit = isset($body['limit']) ? (int) $body['limit'] : null;
     if ($limit) {
         $query->setSize($limit);
     }
     $sort = isset($body['sort']) ? (array) $body['sort'] : null;
     if ($sort) {
         $query->setSort($sort);
         // example: array( 'post_date' => array( 'order' => 'desc' ) )
     }
     $body['from'] = isset($body['from']) && $body['from'] ? $body['from'] : 0;
     $body['size'] = isset($body['size']) && $body['size'] ? $body['size'] : 1000;
     $elasticaResultSet = $index->search($query, ['from' => $body['from'], 'size' => $body['size']]);
     foreach ($elasticaResultSet->getResults() as $elasticaResult) {
         $result[] = $elasticaResult->getData();
     }
     echo json_encode(array('hits' => $result, 'count' => $index->count($query)));
 }
Ejemplo n.º 11
0
 /**
  * @param array $params
  * @return \Elastica\Filter\AbstractFilter
  */
 protected function _getQueryFilter(array $params)
 {
     if (!isset($params['filters']) || empty($params['filters'])) {
         $filters = '*';
     } else {
         $filters = $this->_helper->_prepareFilter($params['filters']);
     }
     $queryFilter = new \Elastica\Filter\Query(new \Elastica\Query\QueryString($filters));
     if (isset($params['range_filters']) && !empty($params['range_filters'])) {
         $andFilter = new \Elastica\Filter\BoolAnd();
         $andFilter->addFilter($queryFilter);
         $filter = new \Elastica\Filter\Range();
         foreach ($params['range_filters'] as $field => $rangeFilter) {
             $filter->addField($field, $rangeFilter);
         }
         $andFilter->addFilter($filter);
         $queryFilter = $andFilter;
     }
     return $queryFilter;
 }
Ejemplo n.º 12
0
 public function search(Category $currentCategory, CategorySearch $categorySearch)
 {
     // We create a query to return all the products
     $baseQuery = new \Elastica\Query\MatchAll();
     // Then we create filters depending on the chosen criterias
     // Filter products
     $productTypeFilter = new \Elastica\Filter\Type();
     $productTypeFilter->setType('product');
     // Filter for products with available courses
     $nestedFilter = new \Elastica\Filter\Nested();
     $nestedFilter->setPath('courses');
     $nestedFilter->setQuery(new \Elastica\Query\Range('beginDate', array('gte' => \Elastica\Util::convertDate((new \DateTime())->getTimestamp()))));
     // Create a bool filter to put everything together
     $boolFilter = new \Elastica\Filter\Bool();
     $boolFilter->addMust($productTypeFilter);
     $boolFilter->addMust($nestedFilter);
     // Show only products
     // Filter type
     if ($categorySearch->getIsProduct() || $categorySearch->getIsInfoEvent()) {
         // Create OR filter to put together the types
         $typeOrFilter = new \Elastica\Filter\BoolOr();
         // Filter products
         if ($categorySearch->getIsProduct()) {
             $productAndFilter = new \Elastica\Filter\BoolAnd();
             $productAndFilter->addFilter($productTypeFilter);
             $infoFilter = new \Elastica\Filter\Term(array('infoVa' => false));
             $productAndFilter->addFilter($infoFilter);
             $typeOrFilter->addFilter($productAndFilter);
         }
         // Filter info events if isProduct is not selected
         if ($categorySearch->getIsInfoEvent()) {
             $productAndFilter = new \Elastica\Filter\BoolAnd();
             $productAndFilter->addFilter($productTypeFilter);
             $infoFilter = new \Elastica\Filter\Term(array('infoVa' => true));
             $productAndFilter->addFilter($infoFilter);
             $typeOrFilter->addFilter($productAndFilter);
         }
         $boolFilter->addMust($typeOrFilter);
     }
     // Filter product type
     if ($categorySearch->getProductType()) {
         $productTypeFilter = new \Elastica\Filter\Nested();
         $productTypeFilter->setPath('productType');
         $productTypeFilter->setFilter(new \Elastica\Filter\Term(array('productType._id' => $categorySearch->getProductType()->getId())));
         $boolFilter->addMust($productTypeFilter);
     }
     // Filter day time
     if ($categorySearch->getDayTime()) {
         $dayTimeFilter = new \Elastica\Filter\Nested();
         $dayTimeFilter->setPath('courses');
         $dayTimeFilter->setFilter(new \Elastica\Filter\Term(array('courses.dayTimes' => $categorySearch->getDayTime())));
         $boolFilter->addMust($dayTimeFilter);
     }
     // Filter categories
     $categoryIds = array();
     if ($categorySearch->getSubcategories() instanceof \Traversable) {
         foreach ($categorySearch->getSubcategories() as $category) {
             if (is_object($category)) {
                 $categoryIds[] = $category->getId();
             } else {
                 $categoryIds[] = $category;
             }
         }
     }
     if (empty($categoryIds)) {
         $categoryIds[] = $currentCategory->getId();
         foreach ($currentCategory->getChildren() as $child) {
             $categoryIds[] = $child->getId();
         }
     }
     $categoryFilter = new \Elastica\Filter\BoolOr();
     $mainCategoryFilter = new \Elastica\Filter\Nested();
     $mainCategoryFilter->setPath('category');
     $mainCategoryFilter->setFilter(new \Elastica\Filter\Terms('category._id', array($categoryIds)));
     $subCategoryFilter = new \Elastica\Filter\Nested();
     $subCategoryFilter->setPath('subcategory');
     $subCategoryFilter->setFilter(new \Elastica\Filter\Terms('subcategory._id', array($categoryIds)));
     $additionalCategoryFilter = new \Elastica\Filter\Nested();
     $additionalCategoryFilter->setPath('additionalCategories');
     $additionalCategoryFilter->setFilter(new \Elastica\Filter\Terms('additionalCategories._id', array($categoryIds)));
     $categoryFilter->addFilter($mainCategoryFilter);
     $categoryFilter->addFilter($subCategoryFilter);
     $categoryFilter->addFilter($additionalCategoryFilter);
     $boolFilter->addMust($categoryFilter);
     $filtered = new \Elastica\Query\Filtered($baseQuery, $boolFilter);
     $query = \Elastica\Query::create($filtered);
     $sort = $categorySearch->getSort();
     if (!empty($sort)) {
         $sort = explode(' ', $sort);
         $query->setSort(array($sort[0] => array('order' => $sort[1]), "_score" => array('order' => 'desc')));
     }
     $paginated = $this->finder->findPaginated($query);
     $paginated->setMaxPerPage($categorySearch->getPerPage())->setCurrentPage($categorySearch->getPage());
     return $paginated;
 }
Ejemplo n.º 13
0
<?php

require_once '../vendor/autoload.php';
$param_key = array('message', 'aid', 'rid', 'date_from', 'date_to');
$param = array();
foreach ($param_key as $key) {
    if (isset($_GET[$key])) {
        $param[$key] = $_GET[$key];
    }
}
$client = new Elastica\Client(array('url' => 'http://localhost:9200/'));
$chat_type = $client->getIndex('cw')->getType('chat');
// create search parameter
$query = new Elastica\Query\MatchAll();
$filter = new Elastica\Filter\BoolAnd();
$filter_list = array();
if (!empty($param['message'])) {
    $chat_filter = new Elastica\Filter\Term();
    $chat_filter->setTerm('message', $param['message']);
    $filter_list[] = $chat_filter;
}
if (!empty($param['rid'])) {
    $rid_filter = new Elastica\Filter\Terms();
    $rid = explode(',', $param['rid']);
    $rid_filter->setTerms('rid', $rid);
    $filter_list[] = $rid_filter;
}
if (!empty($param['aid'])) {
    $aid_filter = new Elastica\Filter\Terms();
    $aid = explode(',', $param['aid']);
    $aid_filter->setTerms('aid', $aid);