/**
  * Test for stats aggregation toArray() method.
  */
 public function testToArray()
 {
     $aggregation = new StatsAggregation('test_agg');
     $aggregation->setField('test_field');
     $expectedResult = ['agg_test_agg' => ['stats' => ['field' => 'test_field']]];
     $this->assertEquals($expectedResult, $aggregation->toArray());
 }
 /**
  * Test for stats aggregation when script is set.
  */
 public function testStatsAggregationWithScriptSet()
 {
     /** @var Repository $repo */
     $repo = $this->getManager()->getRepository('AcmeTestBundle:Product');
     $aggregation = new StatsAggregation('test_agg');
     $aggregation->setField('price');
     $aggregation->setScript('_value * 1.2');
     $search = $repo->createSearch()->addAggregation($aggregation);
     $results = $repo->execute($search, Repository::RESULTS_RAW);
     $expectedResult = ['agg_test_agg' => ['count' => 3, 'min' => 12.54, 'max' => 38.4, 'sum' => 69.06, 'avg' => 23.02]];
     $this->assertArrayHasKey('aggregations', $results);
     foreach ($expectedResult['agg_test_agg'] as $checkKey => $checkValue) {
         $this->assertEquals($checkValue, $results['aggregations']['agg_test_agg'][$checkKey], '', 0.01);
     }
 }
 /**
  * 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 for histogram aggregation. Order and min doc count set. Elasticsearch version >= 1.5.0.
  */
 public function testHistogramAggregationWithOrderAndMinDocCountSet()
 {
     /** @var Repository $repo */
     $repo = $this->getManager()->getRepository('AcmeTestBundle:Product');
     $aggregation = new HistogramAggregation('test_agg');
     $aggregation->setField('price');
     $aggregation->setInterval(5);
     $aggregation->setMinDocCount(2);
     $statsAggregation = new StatsAggregation('price_stats');
     $statsAggregation->setField('price');
     $aggregation->setOrder($statsAggregation->getName() . '.min', HistogramAggregation::DIRECTION_ASC);
     $aggregation->addAggregation($statsAggregation);
     $expectedResults = ['doc_count' => 2, 'agg_price_stats' => ['count' => 2, 'min' => 2.0, 'max' => 3.0, 'avg' => 2.5, 'sum' => 5.0]];
     $search = $repo->createSearch()->addAggregation($aggregation);
     $results = $repo->execute($search, Repository::RESULTS_RAW)['aggregations'];
     $resultBucket = $results['agg_test_agg']['buckets'][0];
     $this->assertEquals($expectedResults['doc_count'], $resultBucket['doc_count']);
     foreach ($expectedResults['agg_price_stats'] as $checkKey => $checkValue) {
         $this->assertEquals($checkValue, $resultBucket['agg_price_stats'][$checkKey], '', 0.01);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null)
 {
     $stateAgg = new StatsAggregation('date_range_agg');
     $stateAgg->setField($this->getField());
     $search->addAggregation($stateAgg);
 }