Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions Query/Aggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -108,6 +112,7 @@ private function __construct(
$this->subgroup = $subgroup;
$this->sort = $sort;
$this->limit = $limit;
$this->promoted = $promoted;
}

/**
Expand Down Expand Up @@ -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
*/
Expand All @@ -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,
Expand All @@ -209,7 +224,8 @@ public static function create(
$filterType,
$subgroup,
$sort,
$limit
$limit,
$promoted
);
}

Expand Down Expand Up @@ -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
!(
Expand Down Expand Up @@ -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'] ?? []
);
}
}
21 changes: 15 additions & 6 deletions Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ public function sortBy(SortBy $sortBy): self
* @param int $applicationType
* @param array $aggregationSort
* @param int $limit
* @param array $promoted
*
* @return Query
*/
Expand All @@ -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,
Expand All @@ -754,7 +756,8 @@ public function aggregateBy(
Filter::TYPE_FIELD,
[],
$aggregationSort,
$limit
$limit,
$promoted
);

return $this;
Expand All @@ -770,6 +773,7 @@ public function aggregateBy(
* @param string $filterType
* @param array $aggregationSort
* @param int $limit
* @param array $promoted
*
* @return Query
*/
Expand All @@ -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;
Expand All @@ -793,7 +798,8 @@ public function aggregateByRange(
$filterType,
$options,
$aggregationSort,
$limit
$limit,
$promoted
);

return $this;
Expand All @@ -808,6 +814,7 @@ public function aggregateByRange(
* @param int $applicationType
* @param array $aggregationSort
* @param int $limit
* @param array $promoted
*
* @return Query
*/
Expand All @@ -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;
Expand All @@ -830,7 +838,8 @@ public function aggregateByDateRange(
Filter::TYPE_DATE_RANGE,
$options,
$aggregationSort,
$limit
$limit,
$promoted
);

return $this;
Expand Down
5 changes: 5 additions & 0 deletions Tests/Query/AggregationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -103,6 +105,7 @@ public function testToArray()
'subgroup' => ['xxx'],
'sort' => Aggregation::SORT_BY_COUNT_ASC,
'limit' => 10,
'promoted' => ['item1', 'item2'],
];

$this->assertEquals(
Expand All @@ -124,6 +127,7 @@ public function testToArrayDefaultFields()
'subgroup' => [],
'sort' => Aggregation::SORT_BY_COUNT_DESC,
'limit' => 0,
'promoted' => [],
];

$this->assertEquals(
Expand Down Expand Up @@ -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());
}
}
14 changes: 14 additions & 0 deletions Tests/Query/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}