/** * Data provider for testChildrenAggregation. * * @return array */ public function getChildrenAggregationData() { $out = []; $mapping = ['product' => ['properties' => ['title' => ['type' => 'string']]], 'comment' => ['_parent' => ['type' => 'product'], '_routing' => ['required' => true], 'properties' => ['sub_title' => ['type' => 'string']]]]; // Case #0 simple terms aggregation. $aggregation = new TermsAggregation('test_terms_agg'); $aggregation->setField('comment.sub_title'); $result = ['doc_count' => 2, 'agg_test_terms_agg' => ['buckets' => [['key' => 'bar', 'doc_count' => 1], ['key' => 'foo', 'doc_count' => 1]]]]; $out[] = [$aggregation, $result, $mapping]; return $out; }
/** * Get profiles from Elasticsearch. * * @return array */ public function getProfiles() { $repo = $this->manager->getRepository('ONGRSettingsBundle:Setting'); // Create aggregated profiles list from all available settings. $aggregation = new TermsAggregation('profile_agg'); $aggregation->setField('profile'); // Create query. $search = $repo->createSearch()->addAggregation($aggregation)->setFields(['profile']); // Process query. Get RESULTS_RAW. $results = $repo->execute($search, Repository::RESULTS_ARRAY); return $results; }
/** * Data provider for testNestedAggregation. * * @return array */ public function getNestedAggregationData() { $out = []; $mapping = ['product' => ['properties' => ['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'); $result = ['doc_count' => 4, 'agg_test_terms_agg' => ['buckets' => [['key' => 'foo', 'doc_count' => 2], ['key' => 'bar', 'doc_count' => 1], ['key' => 'baz', 'doc_count' => 1]]]]; $out[] = [$aggregation, $result, $mapping]; // Case #1 simple stats aggregation. $aggregation = new StatsAggregation('test_stats_agg'); $aggregation->setField('sub_products.price'); $result = ['doc_count' => 4, 'agg_test_stats_agg' => ['count' => 4, 'min' => 10, 'max' => 1000, 'sum' => 1135, 'avg' => 283.75]]; $out[] = [$aggregation, $result, $mapping]; return $out; }
/** * 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); }
/** * {@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; }
/** * Check if top hits aggregation works when it's nested. */ public function testTopHitsAggregationNested() { $topAggregation = new TopHitsAggregation('test-top_hits'); $termAggregation = new TermsAggregation('test_term'); $termAggregation->setField('description'); $termAggregation->addAggregation($topAggregation); /** @var Repository $repo */ $repo = $this->getManager()->getRepository('AcmeTestBundle:Product'); $search = $repo->createSearch()->addAggregation($termAggregation)->addSort(new Sort('_id', Sort::ORDER_ASC)); $results = $repo->execute($search, Repository::RESULTS_RAW); $this->assertTrue(isset($results['aggregations']['agg_test_term']['buckets'][0]['agg_test-top_hits']['hits'])); $result = $results['aggregations']['agg_test_term']['buckets'][0]['agg_test-top_hits']['hits']; $this->assertEquals(2, $result['total']); $this->assertTrue(isset($results['aggregations']['agg_test_term']['buckets'][1]['agg_test-top_hits']['hits'])); $result = $results['aggregations']['agg_test_term']['buckets'][1]['agg_test-top_hits']['hits']; $this->assertEquals(1, $result['total']); }
/** * Tests setOrder DESC method. */ public function testTermsAggregationSetOrderDESC() { // Case #7 terms aggregation with order term mode, desc direction. $aggregation = new TermsAggregation('test_agg'); $aggregation->setField('test_field'); $aggregation->addParameter('order', ['_term' => 'desc']); $result = ['agg_test_agg' => ['terms' => ['field' => 'test_field', 'order' => ['_term' => 'desc']]]]; $this->assertEquals($aggregation->toArray(), $result); }
/** * Builds term aggregation. * * @param array $options * * @return TermsAggregation */ private function getAggregation($options) { $term = new TermsAggregation($options['name']); $term->setField($options['field']); if (array_key_exists('exclude', $options)) { $term->addParameter('exclude', $options['exclude']); } if (array_key_exists('include', $options)) { $term->addParameter('include', $options['include']); } if (array_key_exists('min_document_count', $options)) { $term->addParameter('min_doc_count', $options['min_document_count']); } if (array_key_exists('order', $options)) { $term->addParameter('order', [$options['order'][0] => $options['order'][1]]); } if (array_key_exists('size', $options)) { $term->addParameter('size', $options['size']); } return $term; }