From 6da5cae72c53d4de03ea79a2550ae5216e3295f3 Mon Sep 17 00:00:00 2001 From: Martin Kluska Date: Tue, 5 Apr 2022 15:31:31 +0200 Subject: [PATCH] Add stats aggregation --- src/Aggregation/StatsAggregation.php | 16 ++++++ tests/Aggregation/StatsAggregationTest.php | 61 ++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/Aggregation/StatsAggregation.php create mode 100644 tests/Aggregation/StatsAggregationTest.php diff --git a/src/Aggregation/StatsAggregation.php b/src/Aggregation/StatsAggregation.php new file mode 100644 index 0000000..8153032 --- /dev/null +++ b/src/Aggregation/StatsAggregation.php @@ -0,0 +1,16 @@ +setField('price'); + + $this->assertEquals([ + 'stats' => [ + 'field' => 'price', + ], + ], $query->build()); + } + + public function testItBuildTheAggregationUsingAScript(): void + { + $query = new StatsAggregation('price'); + $query->setScript('doc.price.value'); + + $this->assertEquals([ + 'stats' => [ + 'script' => [ + 'source' => 'doc.price.value', + ], + ], + ], $query->build()); + } + + public function testWithFieldName(): void + { + $query = new StatsAggregation('price'); + + $this->assertEquals([ + 'stats' => [ + 'field' => 'price', + ], + ], $query->build()); + } + + public function testItBuildTheAggregationWithMissingValue(): void + { + $query = new StatsAggregation('price', 'price'); + $query->setMissing(10); + + $this->assertEquals([ + 'stats' => [ + 'field' => 'price', + 'missing' => 10, + ], + ], $query->build()); + } +}