/** * Test filters in aggregations and post filter. */ public function testBoolWithFuzzyQueryAndSortFilter() { $repo = $this->getManager()->getRepository('AcmeTestBundle:Product'); $search = $repo->createSearch(); $rangeFilter = new RangeFilter('price', ['from' => 200, 'to' => 999]); $search->addPostFilter($rangeFilter); $name = 'foo'; $TermsAgg = new TermsAggregation($name); $TermsAgg->setField('title'); $TermsAgg->addParameter('include', $name); $filterAgg = new FilterAggregation($name . '-filter'); $filters = $search->getPostFilters(); $filterAgg->setFilter($filters); $filterAgg->addAggregation($TermsAgg); $search->addAggregation($filterAgg); $repo->execute($search, Repository::RESULTS_RAW); }
/** * Data provider for testFilterAggregation(). * * @return array */ public function getFilterAggregationData() { $out = []; // Case #0 Single filter aggregation. $aggregation = new FilterAggregation('test_agg'); $filter = new RegexpFilter('title', 'pizza'); $aggregation->setFilter($filter); $result = ['agg_test_agg' => ['doc_count' => 1]]; $out[] = [$aggregation, $result]; // Case #1 Nested filter aggregation. $aggregation = new FilterAggregation('test_agg'); $filter = new RegexpFilter('title', 'pizza'); $aggregation->setFilter($filter); $aggregation2 = new RangeAggregation('test_agg_2'); $aggregation2->setField('price'); $aggregation2->addRange(10, 20); $aggregation->addAggregation($aggregation2); $result = ['agg_test_agg' => ['doc_count' => 1, 'agg_test_agg_2' => ['buckets' => [['key' => '10.0-20.0', 'from' => 10, 'from_as_string' => '10.0', 'to' => 20, 'to_as_string' => '20.0', 'doc_count' => 1]]]]]; $out[] = [$aggregation, $result]; return $out; }
/** * {@inheritdoc} */ public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null) { $name = $state ? $state->getName() : $this->getField(); $aggregation = new TermsAggregation($name); $aggregation->setField($this->getField()); if ($this->getSortType()) { $aggregation->addParameter('order', [$this->getSortType()['type'] => $this->getSortType()['order']]); } $aggregation->addParameter('size', 0); if ($this->getSize() > 0) { $aggregation->addParameter('size', $this->getSize()); } if ($relatedSearch->getPostFilters()) { $filterAggregation = new FilterAggregation($name . '-filter'); $filterAggregation->setFilter($relatedSearch->getPostFilters()); $filterAggregation->addAggregation($aggregation); $search->addAggregation($filterAggregation); } else { $search->addAggregation($aggregation); } }
/** * Data provider for testReverseNestedAggregation. * * @return array */ public function getReverseNestedAggregationData() { $out = []; $mapping = ['product' => ['properties' => ['name' => ['type' => 'string', 'index' => 'not_analyzed'], 'sub_products' => ['type' => 'nested', 'properties' => ['id' => ['type' => 'string', 'index' => 'not_analyzed'], 'title' => ['type' => 'string'], 'price' => ['type' => 'float']]]]]]; // Case #0 simple terms aggregation. $aggregation = new TermsAggregation('test_terms_agg'); $aggregation->setField('sub_products.title'); $reverseAggregation = new TermsAggregation('test_reverse_term_agg'); $reverseAggregation->setField('name'); $result = ['doc_count' => 4, 'agg_test_terms_agg' => ['buckets' => [['key' => 'foo', 'doc_count' => 2, 'agg_test_reverse_nested_agg' => ['doc_count' => 2, 'agg_test_reverse_term_agg' => ['buckets' => [['key' => 'name-foo', 'doc_count' => 1], ['key' => 'name-foo-baz', 'doc_count' => 1]]]]], ['key' => 'bar', 'doc_count' => 1, 'agg_test_reverse_nested_agg' => ['doc_count' => 1, 'agg_test_reverse_term_agg' => ['buckets' => [['key' => 'name-bar', 'doc_count' => 1]]]]], ['key' => 'baz', 'doc_count' => 1, 'agg_test_reverse_nested_agg' => ['doc_count' => 1, 'agg_test_reverse_term_agg' => ['buckets' => [['key' => 'name-foo-baz', 'doc_count' => 1]]]]]]]]; $out[] = [$aggregation, $reverseAggregation, $result, $mapping]; // Case #1 simple filtred aggregation. $aggregation = new TermsAggregation('test_terms_agg'); $aggregation->setField('sub_products.title'); $termFilter = new TermFilter('name', 'name-foo'); $reverseAggregation = new FilterAggregation('test_reverse_term_agg'); $reverseAggregation->setFilter($termFilter); $result = ['doc_count' => 4, 'agg_test_terms_agg' => ['buckets' => [['key' => 'foo', 'doc_count' => 2, 'agg_test_reverse_nested_agg' => ['doc_count' => 2, 'agg_test_reverse_term_agg' => ['doc_count' => 1]]], ['key' => 'bar', 'doc_count' => 1, 'agg_test_reverse_nested_agg' => ['doc_count' => 1, 'agg_test_reverse_term_agg' => ['doc_count' => 0]]], ['key' => 'baz', 'doc_count' => 1, 'agg_test_reverse_nested_agg' => ['doc_count' => 1, 'agg_test_reverse_term_agg' => ['doc_count' => 0]]]]]]; $out[] = [$aggregation, $reverseAggregation, $result, $mapping]; return $out; }
/** * Test for toArray() with setting a filter. */ public function testToArrayWithFilter() { $aggregation = new FilterAggregation('test_agg'); $aggregation->setFilter(new MissingFilter('test')); $aggregation->toArray(); }