Exemplo n.º 1
2
 public function findWithTenant($searchText, $tenantId, $sortDirection = 'desc')
 {
     $boolFilter = new Filter\Bool();
     $boolFilter->addMust(new Filter\Term(['tenant.id' => $tenantId]));
     $boolQuery = new Query\Bool();
     if ($searchText !== null) {
         $fieldQuery = new Query\MultiMatch();
         $fieldQuery->setQuery("*" . $searchText . "*");
         $fieldQuery->setFields(['name', 'description']);
         $boolQuery->addMust($fieldQuery);
     }
     $filtered = new Query\Filtered($boolQuery, $boolFilter);
     $query = Query::create($filtered);
     $query->addSort(['created_at' => $sortDirection]);
     return $this->findPaginated($query);
 }
 public function testBoolFilter()
 {
     $index = $this->_createIndex('bool_filter_test');
     $type = $index->getType('book');
     //index some test data
     $type->addDocument(new \Elastica\Document(1, array('author' => 'Michael Shermer', 'title' => 'The Believing Brain', 'publisher' => 'Robinson')));
     $type->addDocument(new \Elastica\Document(2, array('author' => 'Jared Diamond', 'title' => 'Guns, Germs and Steel', 'publisher' => 'Vintage')));
     $type->addDocument(new \Elastica\Document(3, array('author' => 'Jared Diamond', 'title' => 'Collapse', 'publisher' => 'Penguin')));
     $type->addDocument(new \Elastica\Document(4, array('author' => 'Richard Dawkins', 'title' => 'The Selfish Gene', 'publisher' => 'OUP Oxford')));
     $type->addDocument(new \Elastica\Document(5, array('author' => 'Anthony Burges', 'title' => 'A Clockwork Orange', 'publisher' => 'Penguin')));
     $index->refresh();
     //use the terms lookup feature to query for some data
     //build query
     //must
     //  should
     //      author = jared
     //      author = richard
     //  must_not
     //      publisher = penguin
     //construct the query
     $query = new Query();
     $mainBoolFilter = new Bool();
     $shouldFilter = new Bool();
     $authorFilter1 = new Term();
     $authorFilter1->setTerm('author', 'jared');
     $authorFilter2 = new Term();
     $authorFilter2->setTerm('author', 'richard');
     $shouldFilter->addShould(array($authorFilter1, $authorFilter2));
     $mustNotFilter = new Bool();
     $publisherFilter = new Term();
     $publisherFilter->setTerm('publisher', 'penguin');
     $mustNotFilter->addMustNot($publisherFilter);
     $mainBoolFilter->addMust(array($shouldFilter, $mustNotFilter));
     $query->setFilter($mainBoolFilter);
     //execute the query
     $results = $index->search($query);
     //check the number of results
     $this->assertEquals($results->count(), 2, 'Bool filter with child Bool filters: number of results check');
     //count compare the id's
     $ids = array();
     /** @var \Elastica\Result $result **/
     foreach ($results as $result) {
         $ids[] = $result->getId();
     }
     $this->assertEquals($ids, array("2", "4"), 'Bool filter with child Bool filters: result ID check');
     $index->delete();
 }
 public function findByUserAndTags(User $user, $tags = null)
 {
     $filterQuery = new Query\Filtered();
     $searchFilter = new Filter\Bool();
     $searchFilter->addMust(new Filter\Term(array('user' => $user->getId())));
     if (!empty($tags)) {
         $tagFilter = new Filter\Terms('tags', explode('+', $tags));
         $tagFilter->setExecution('and');
         $searchFilter->addMust($tagFilter);
     }
     $filterQuery->setFilter($searchFilter);
     $searchQuery = new Query($filterQuery);
     $searchQuery->addSort(array('createdAt' => 'desc'));
     $agg = new Aggregation\Terms('tags');
     $agg->setField('tags')->setOrder('_count', 'desc')->setSize(100);
     $searchQuery->addAggregation($agg);
     return $searchQuery;
 }
 /**
  * Convenient helper for building Bool filters.
  * @param AbstractFilter|array(AbstractFilter) $must must filters
  * @param AbstractFilter|array(AbstractFilter) $mustNot must not filters
  * @return Bool a bool filter containing $must and $mustNot
  */
 private static function newBool($must, $mustNot)
 {
     $bool = new Bool();
     if (is_array($must)) {
         foreach ($must as $m) {
             $bool->addMust($m);
         }
     } else {
         $bool->addMust($must);
     }
     if (is_array($mustNot)) {
         foreach ($mustNot as $m) {
             $bool->addMustNot($m);
         }
     } else {
         $bool->addMustNot($mustNot);
     }
     return $bool;
 }
 /**
  * @param string $term text to search
  * @return \Status
  */
 public function searchText($term)
 {
     $term = trim($term);
     // No searching for nothing!  That takes forever!
     if (!$term) {
         return null;
     }
     $query = new Query();
     $offset = min($this->offset, static::MAX_OFFSET);
     if ($offset) {
         $query->setFrom($offset);
     }
     if ($this->limit) {
         $query->setSize($this->limit);
     }
     $filter = new Bool();
     // filters
     if ($this->namespaces) {
         $filter->addMust(new Terms('namespace', $this->namespaces));
     }
     if ($this->pageIds) {
         $filter->addMust(new Terms('pageid', $this->pageIds));
     }
     if ($this->moderationStates) {
         $filter->addMust(new Terms('revisions.moderation_state', $this->moderationStates));
     }
     // only apply filters if there are any
     if ($filter->toArray()) {
         $query->setFilter($filter);
     }
     $sortArgs = $this->getSortArgs();
     if (isset($sortArgs[$this->sort]) && $sortArgs[$this->sort]) {
         $query->setSort($sortArgs[$this->sort]);
     }
     // @todo: interwiki stuff? (see \CirrusSearch)
     $searcher = new Searcher($query, false, $this->type);
     return $searcher->searchText($term);
 }
 /**
  * Unify non-script filters into a single filter.
  * @param array(\Elastica\AbstractFilter) $must filters that must be found
  * @param array(\Elastica\AbstractFilter) $mustNot filters that must not be found
  * @return null|\Elastica\AbstractFilter null if there are no filters or one that will execute
  *     all of the provided filters
  */
 private static function unifyNonScript($mustFilters, $mustNotFilters)
 {
     $mustFilterCount = count($mustFilters);
     $mustNotFilterCount = count($mustNotFilters);
     if ($mustFilterCount + $mustNotFilterCount === 0) {
         return null;
     }
     if ($mustFilterCount === 1 && $mustNotFilterCount == 0) {
         return $mustFilters[0];
     }
     if ($mustFilterCount === 0 && $mustNotFilterCount == 1) {
         return new \Elastica\Filter\BoolNot($mustNotFilters[0]);
     }
     $boolFilter = new \Elastica\Filter\Bool();
     foreach ($mustFilters as $must) {
         $boolFilter->addMust($must);
     }
     foreach ($mustNotFilters as $mustNot) {
         $boolFilter->addMustNot($mustNot);
     }
     return $boolFilter;
 }
 /**
  * Builds an or between many categories that the page could be in.
  * @param string[] $categories categories to match
  * @return \Elastica\Filter\Bool|null A null return value means all values are filtered
  *  and an empty result set should be returned.
  */
 public function matchPageCategories($categories)
 {
     $filter = new \Elastica\Filter\Bool();
     $ids = array();
     $names = array();
     foreach ($categories as $category) {
         if (substr($category, 0, 3) === 'id:') {
             $id = substr($category, 3);
             if (ctype_digit($id)) {
                 $ids[] = $id;
             }
         } else {
             $names[] = $category;
         }
     }
     foreach (Title::newFromIds($ids) as $title) {
         $names[] = $title->getText();
     }
     if (!$names) {
         return null;
     }
     foreach ($names as $name) {
         $filter->addShould($this->matchPage('category.lowercase_keyword', $name));
     }
     return $filter;
 }
Exemplo n.º 8
0
 /**
  * Search children.
  *
  * @param string $type Post type
  * @param int $parent Parent ID to get all children
  * @param string $order Order way
  * @return array $search Combine of all results, total and aggregations
  *
  * @since 3.0.0
  */
 public function searchChildren($type, $parent, $order = 'desc')
 {
     //Check page
     if (is_search()) {
         return;
     }
     //Return array
     $return = array('parent' => $parent, 'total' => 0, 'results' => array());
     //Check request
     if (empty($parent)) {
         return $return;
     }
     //Get query vars
     $results = array();
     $types = array();
     //Get Elasticsearch datas
     $index = $this->getIndex();
     //Check index
     if (null === $index || empty($index)) {
         return $return;
     }
     //Create the actual search object with some data.
     $es_query = new Query();
     //Define term
     $es_term = new Term();
     $es_term->setTerm($type . '.parent', $parent);
     //Filter 'And'
     $es_filter = new Bool();
     $es_filter->addMust($es_term);
     //Add filter to the search object
     $es_query->setFilter($es_filter);
     //Add sort
     $es_query->setSort(array($type . '.date' => array('order' => $order)));
     //Search!
     $es_resultset = $index->search($es_query);
     //Retrieve data
     $es_results = $es_resultset->getResults();
     //Check results
     if (null == $es_results || empty($es_results)) {
         return $return;
     }
     //Iterate to retrieve all IDs
     foreach ($es_results as $res) {
         $typ = $res->getType();
         //Save type
         $types[$typ] = $typ;
         //Save datas
         $results[$typ][] = array('id' => $res->getId(), 'score' => $res->getScore(), 'source' => $res->getSource());
     }
     //Get total
     $total = $es_resultset->getTotalHits();
     //Return everything
     $return = array('parent' => $parent, 'total' => $total, 'results' => $results);
     return $return;
 }
Exemplo n.º 9
0
 /**
  * @return array
  */
 protected function addFilters()
 {
     if (!isset($_GET['filters'])) {
         return false;
     }
     $filterCount = 0;
     $filters = new El\Filter\Bool();
     foreach ($_GET['filters'] as $key => $filterValue) {
         $filterCatCount = 0;
         $filterPart = new El\Filter\BoolOr();
         foreach ($filterValue as $term => $enabled) {
             if ($enabled == 'true') {
                 $term = str_replace('_', ' ', $term);
                 $filter = new El\Filter\Term();
                 $filter->setTerm($key, $term);
                 $filterPart->addFilter($filter);
                 $filterCount++;
                 $filterCatCount++;
             }
         }
         if ($filterCatCount > 0) {
             $filters->addMust($filterPart);
         }
     }
     if ($filterCount === 0) {
         return false;
     }
     return $filters;
 }
 /**
  * getFilter
  *
  * @access private
  * @return AbstractFilter
  */
 private function getFilter()
 {
     if (!$this->filterList) {
         return null;
     }
     if (count($this->filterList) == 1) {
         return current($this->filterList);
     }
     $boolFilters = [];
     $andFilters = [];
     foreach ($this->filterList as $tmpFilter) {
         if ($this->isAndFilter($tmpFilter)) {
             $andFilters[] = $tmpFilter;
         } else {
             $boolFilters[] = $tmpFilter;
         }
     }
     $boolFilter = null;
     $nbBoolFilters = count($boolFilters);
     if ($nbBoolFilters > 1) {
         $boolFilter = new Filter\Bool();
         foreach ($boolFilters as $tmpFilter) {
             $boolFilter->addMust($tmpFilter);
         }
         array_unshift($andFilters, $boolFilter);
     } elseif ($nbBoolFilters == 1) {
         $andFilters = array_merge($boolFilters, $andFilters);
     }
     $nbAndFilters = count($andFilters);
     if ($nbAndFilters == 1) {
         return current($andFilters);
     } elseif ($nbAndFilters > 1) {
         $filter = new Filter\BoolAnd();
         $filter->setFilters($andFilters);
         return $filter;
     }
     return null;
 }
Exemplo n.º 11
0
 /**
  * @param string $term
  * @param string $value
  * @param int    $size
  *
  * @return \Elastica\ResultSet
  */
 public function termQuery($term, $value, $size = 30)
 {
     $_agg = new Aggregation();
     $_facet = $_agg->date_histogram('occurred_on', '@timestamp', ElkIntervals::DAY);
     //        $_facet = new DateHistogram('occurred_on');
     //        $_facet->setField('@timestamp');
     //        $_facet->setInterval('day');
     $_query = new Query();
     $_query->setSize($size);
     $_query->setSort(['@timestamp']);
     //	Filter for term
     $_filter = new Prefix($term, $value);
     $_and = new Bool();
     $_and->addMust($_filter);
     $_query->setPostFilter($_and);
     $_query->addAggregation($_facet);
     $_results = $this->_doSearch($_query);
     return $_results;
 }
Exemplo n.º 12
0
 /**
  * @return BoolOr
  */
 private function addFilters()
 {
     //		$filterCount = 0;
     $filters = new Bool();
     $typeFilter = new Type('issue');
     $filters->addMust($typeFilter);
     if (!isset($_GET['filters'])) {
         return $filters;
     }
     foreach ($_GET['filters'] as $key => $filterValue) {
         $filterCatCount = 0;
         $filterPart = new BoolOr();
         foreach ($filterValue as $term => $enabled) {
             if ($enabled == 'true') {
                 $term = str_replace('_', ' ', $term);
                 $filter = new Term();
                 if ($key == 'tracker' || $key == 'project' || $key == 'category' || $key == 'status' || $key == 'author' || $key == 'priority' || $key == 'fixed_version') {
                     $filter->setTerm($key . '.name', $term);
                 } else {
                     $filter->setTerm($key, $term);
                 }
                 $filterPart->addFilter($filter);
                 //					$filterCount++;
                 $filterCatCount++;
             }
         }
         if ($filterCatCount > 0) {
             $filters->addMust($filterPart);
         }
     }
     //		if($filterCount === 0) {
     //			return false;
     //		}
     return $filters;
 }