コード例 #1
0
 /**
  * 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;
 }
コード例 #2
0
 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();
 }
コード例 #3
0
 /**
  * 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;
 }