/**
  * @group functional
  */
 public function testShouldReturnTheRightNumberOfResult()
 {
     $filter = new Nested();
     $this->assertEquals(array('nested' => array()), $filter->toArray());
     $query = new Terms();
     $query->setTerms('hobby', array('guitar'));
     $filter->setPath('hobbies');
     $filter->setFilter($query);
     $client = $this->_getClient();
     $search = new Search($client);
     $index = $this->_getIndexForTest();
     $search->addIndex($index);
     $resultSet = $search->search($filter);
     $this->assertEquals(1, $resultSet->getTotalHits());
     $filter = new Nested();
     $this->assertEquals(array('nested' => array()), $filter->toArray());
     $query = new Terms();
     $query->setTerms('hobby', array('opensource'));
     $filter->setPath('hobbies');
     $filter->setFilter($query);
     $client = $this->_getClient();
     $search = new Search($client);
     $index = $this->_getIndexForTest();
     $search->addIndex($index);
     $resultSet = $search->search($filter);
     $this->assertEquals(2, $resultSet->getTotalHits());
 }
 /**
  * @group unit
  */
 public function testSetJoin()
 {
     $filter = new Nested();
     $this->assertTrue($filter->setJoin(true)->getParam('join'));
     $this->assertFalse($filter->setJoin(false)->getParam('join'));
     $returnValue = $filter->setJoin(true);
     $this->assertInstanceOf('Elastica\\Filter\\Nested', $returnValue);
 }
 /**
  * Returns a Nested filter object setup to filter sub documents by a path.
  *
  * ### Example:
  *
  * {{{
  *    $builder->nested('comments', $builder->term('author', 'mark'));
  * }}}
  *
  * Or using a query as filter:
  *
  * {{{
  *    $builder->nested('comments', new \Elastica\Query\SimpleQueryString('awesome'));
  * }}}
  *
  * @param string $path A dot separated string denoting the path to the property to filter.
  * @param Elastica\Query\AbstractQuery|Elastica\Filter\AbstractFilter $filter The filtering conditions.
  * @return Elastica\Filter\Nested
  * @see http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-filter.html
  */
 public function nested($path, $filter)
 {
     $nested = new Filter\Nested();
     $nested->setPath($path);
     if ($filter instanceof AbstractFilter) {
         $nested->setFilter($filter);
     }
     if ($filter instanceof AbstractQuery) {
         $nested->setQuery($filter);
     }
     return $nested;
 }
Ejemplo n.º 4
0
 /**
  * @param BoolOr $nestedOrFilter Nested or filter
  * @param string $nestedProperty Nested property (can be 'variants')
  */
 private function addAvailableProductsVariantFilters(BoolOr $nestedOrFilter, $nestedProperty)
 {
     $variantsNestedBool = new BoolQuery();
     $variantsNestedBool->setMinimumNumberShouldMatch(1);
     $availableOn = new QueryRange($nestedProperty . '.availableOn', array('lte' => "now"));
     $variantsNestedBool->addMust($availableOn);
     $availableUntil = new Filtered();
     $availableUntilFilter = new BoolOr();
     $availableUntilNull = new Missing($nestedProperty . '.availableUntil');
     $availableUntilFilter->addFilter($availableUntilNull);
     $availableUntilGte = new FilterRange($nestedProperty . '.availableUntil', array('gte' => time()));
     $availableUntilFilter->addFilter($availableUntilGte);
     $availableUntil->setFilter($availableUntilFilter);
     $variantsNestedBool->addMust($availableUntil);
     $availableOnDemand = new QueryTerm(array($nestedProperty . '.availableOnDemand' => true));
     $variantsNestedBool->addShould($availableOnDemand);
     $onHand = new QueryRange($nestedProperty . '.onHand', array('gt' => 0));
     $variantsNestedBool->addShould($onHand);
     $nested = new Nested();
     $nested->setPath($nestedProperty);
     $nested->setQuery($variantsNestedBool);
     $nestedOrFilter->addFilter($nested);
 }
 public function testShouldReturnTheRightNumberOfResult()
 {
     $f = new Nested();
     $this->assertEquals(array('nested' => array()), $f->toArray());
     $q = new Terms();
     $q->setTerms('hobby', array('guitar'));
     $f->setPath('hobbies');
     $f->setFilter($q);
     $c = $this->_getClient();
     $s = new Search($c);
     $i = $c->getIndex('elastica_test_filter_nested_abstract_filter');
     $s->addIndex($i);
     $r = $s->search($f);
     $this->assertEquals(1, $r->getTotalHits());
     $f = new Nested();
     $this->assertEquals(array('nested' => array()), $f->toArray());
     $q = new Terms();
     $q->setTerms('hobby', array('opensource'));
     $f->setPath('hobbies');
     $f->setFilter($q);
     $c = $this->_getClient();
     $s = new Search($c);
     $i = $c->getIndex('elastica_test_filter_nested_abstract_filter');
     $s->addIndex($i);
     $r = $s->search($f);
     $this->assertEquals(2, $r->getTotalHits());
 }