diff --git a/Query/Aggregation.php b/Query/Aggregation.php index 15c051a..215cd88 100644 --- a/Query/Aggregation.php +++ b/Query/Aggregation.php @@ -83,14 +83,17 @@ class Aggregation implements HttpTransportable */ private $limit; + private $promoted; + /** - * @param string $name - * @param string $field - * @param int $applicationType - * @param string $filterType - * @param array $subgroup - * @param array $sort - * @param int $limit + * @param string $name + * @param string $field + * @param int $applicationType + * @param string $filterType + * @param array $subgroup + * @param array $sort + * @param int $limit + * @param string[] $promoted */ private function __construct( string $name, @@ -99,7 +102,8 @@ private function __construct( string $filterType, array $subgroup, array $sort, - int $limit + int $limit, + array $promoted ) { $this->name = $name; $this->field = $field; @@ -108,6 +112,7 @@ private function __construct( $this->subgroup = $subgroup; $this->sort = $sort; $this->limit = $limit; + $this->promoted = $promoted; } /** @@ -180,16 +185,25 @@ public function getLimit(): int return $this->limit; } + /** + * @return string[] + */ + public function getPromoted(): array + { + return $this->promoted; + } + /** * Create. * - * @param string $name - * @param string $field - * @param int $applicationType - * @param string $filterType - * @param array $subgroup - * @param array $sort - * @param int $limit + * @param string $name + * @param string $field + * @param int $applicationType + * @param string $filterType + * @param array $subgroup + * @param array $sort + * @param int $limit + * @param string[] $promoted * * @return Aggregation */ @@ -200,7 +214,8 @@ public static function create( string $filterType, array $subgroup = [], array $sort = self::SORT_BY_COUNT_DESC, - int $limit = self::NO_LIMIT + int $limit = self::NO_LIMIT, + array $promoted = [] ): self { return new self( $name, @@ -209,7 +224,8 @@ public static function create( $filterType, $subgroup, $sort, - $limit + $limit, + $promoted ); } @@ -240,6 +256,9 @@ public function toArray(): array 'limit' => self::NO_LIMIT === $this->limit ? null : $this->limit, + 'promoted' => $this->promoted === [] + ? [] + : $this->promoted, ], function ($element) { return !( @@ -269,7 +288,8 @@ public static function createFromArray(array $array): self $array['filter_type'] ?? Filter::TYPE_FIELD, $array['subgroup'] ?? [], $array['sort'] ?? self::SORT_BY_COUNT_DESC, - $array['limit'] ?? self::NO_LIMIT + $array['limit'] ?? self::NO_LIMIT, + $array['promoted'] ?? [] ); } } diff --git a/Query/Query.php b/Query/Query.php index 2b92701..234557d 100644 --- a/Query/Query.php +++ b/Query/Query.php @@ -737,6 +737,7 @@ public function sortBy(SortBy $sortBy): self * @param int $applicationType * @param array $aggregationSort * @param int $limit + * @param array $promoted * * @return Query */ @@ -745,7 +746,8 @@ public function aggregateBy( string $field, int $applicationType, array $aggregationSort = Aggregation::SORT_BY_COUNT_DESC, - int $limit = Aggregation::NO_LIMIT + int $limit = Aggregation::NO_LIMIT, + array $promoted = [] ): self { $this->aggregations[$filterName] = Aggregation::create( $filterName, @@ -754,7 +756,8 @@ public function aggregateBy( Filter::TYPE_FIELD, [], $aggregationSort, - $limit + $limit, + $promoted ); return $this; @@ -770,6 +773,7 @@ public function aggregateBy( * @param string $filterType * @param array $aggregationSort * @param int $limit + * @param array $promoted * * @return Query */ @@ -780,7 +784,8 @@ public function aggregateByRange( int $applicationType, string $filterType = Filter::TYPE_RANGE, array $aggregationSort = Aggregation::SORT_BY_COUNT_DESC, - int $limit = Aggregation::NO_LIMIT + int $limit = Aggregation::NO_LIMIT, + array $promoted = [] ): self { if (empty($options)) { return $this; @@ -793,7 +798,8 @@ public function aggregateByRange( $filterType, $options, $aggregationSort, - $limit + $limit, + $promoted ); return $this; @@ -808,6 +814,7 @@ public function aggregateByRange( * @param int $applicationType * @param array $aggregationSort * @param int $limit + * @param array $promoted * * @return Query */ @@ -817,7 +824,8 @@ public function aggregateByDateRange( array $options, int $applicationType, array $aggregationSort = Aggregation::SORT_BY_COUNT_DESC, - int $limit = Aggregation::NO_LIMIT + int $limit = Aggregation::NO_LIMIT, + array $promoted = [] ): self { if (empty($options)) { return $this; @@ -830,7 +838,8 @@ public function aggregateByDateRange( Filter::TYPE_DATE_RANGE, $options, $aggregationSort, - $limit + $limit, + $promoted ); return $this; diff --git a/Tests/Query/AggregationTest.php b/Tests/Query/AggregationTest.php index d56767c..d88a3ea 100644 --- a/Tests/Query/AggregationTest.php +++ b/Tests/Query/AggregationTest.php @@ -47,6 +47,7 @@ public function testCreate() $this->assertEquals(['xxx'], $aggregation->getSubgroup()); $this->assertEquals(Aggregation::SORT_BY_COUNT_ASC, $aggregation->getSort()); $this->assertEquals(10, $aggregation->getLimit()); + $this->assertEquals([], $aggregation->getPromoted()); } /** @@ -88,6 +89,7 @@ public function testCreateDefaultValues() $this->assertEquals([], $aggregation->getSubgroup()); $this->assertEquals(Aggregation::SORT_BY_COUNT_DESC, $aggregation->getSort()); $this->assertEquals(Aggregation::NO_LIMIT, $aggregation->getLimit()); + $this->assertEquals([], $aggregation->getPromoted()); } /** @@ -103,6 +105,7 @@ public function testToArray() 'subgroup' => ['xxx'], 'sort' => Aggregation::SORT_BY_COUNT_ASC, 'limit' => 10, + 'promoted' => ['item1', 'item2'], ]; $this->assertEquals( @@ -124,6 +127,7 @@ public function testToArrayDefaultFields() 'subgroup' => [], 'sort' => Aggregation::SORT_BY_COUNT_DESC, 'limit' => 0, + 'promoted' => [], ]; $this->assertEquals( @@ -158,5 +162,6 @@ public function testCreateFromArrayWithDefaults() $this->assertEquals([], $aggregation->getSubgroup()); $this->assertEquals(Aggregation::SORT_BY_COUNT_DESC, $aggregation->getSort()); $this->assertEquals(Aggregation::NO_LIMIT, $aggregation->getLimit()); + $this->assertEquals([], $aggregation->getPromoted()); } } diff --git a/Tests/Query/QueryTest.php b/Tests/Query/QueryTest.php index bd58aee..88d494d 100644 --- a/Tests/Query/QueryTest.php +++ b/Tests/Query/QueryTest.php @@ -16,6 +16,8 @@ namespace Apisearch\Tests\Query; use Apisearch\Model\IndexUUID; +use Apisearch\Query\Aggregation; +use Apisearch\Query\Filter; use Apisearch\Query\Query; use Apisearch\Query\ScoreStrategies; use Apisearch\Query\ScoreStrategy; @@ -316,4 +318,16 @@ public function testSetQueryText() $this->assertEquals('lolazo', $queryAsArray['q']); $this->assertEquals('lolazo', $newQuery->getQueryText()); } + + public function testAggregationPromoted() + { + $query = Query::createMatchAll() + ->aggregateBy('field1', 'field1', Filter::AT_LEAST_ONE, Aggregation::SORT_BY_COUNT_ASC, 0, ['item1', 'item2']) + ->aggregateByRange('field2', 'field2', ['op1'], Filter::AT_LEAST_ONE, Filter::TYPE_RANGE, Aggregation::SORT_BY_COUNT_ASC, 0, ['item1', 'item3']) + ->aggregateByDateRange('field3', 'field3', ['op2'], Filter::AT_LEAST_ONE, Aggregation::SORT_BY_COUNT_ASC, 0, ['item4']); + + $this->assertEquals(['item1', 'item2'], $query->getAggregation('field1')->getPromoted()); + $this->assertEquals(['item1', 'item3'], $query->getAggregation('field2')->getPromoted()); + $this->assertEquals(['item4'], $query->getAggregation('field3')->getPromoted()); + } }