Skip to content

Commit 8fefd03

Browse files
authored
Merge pull request #139 from apisearch-io/feature/added-aggregations-promoted-items
Added aggregation promoted items
2 parents d8de45a + ec7541d commit 8fefd03

File tree

4 files changed

+72
-24
lines changed

4 files changed

+72
-24
lines changed

Query/Aggregation.php

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,17 @@ class Aggregation implements HttpTransportable
8383
*/
8484
private $limit;
8585

86+
private $promoted;
87+
8688
/**
87-
* @param string $name
88-
* @param string $field
89-
* @param int $applicationType
90-
* @param string $filterType
91-
* @param array $subgroup
92-
* @param array $sort
93-
* @param int $limit
89+
* @param string $name
90+
* @param string $field
91+
* @param int $applicationType
92+
* @param string $filterType
93+
* @param array $subgroup
94+
* @param array $sort
95+
* @param int $limit
96+
* @param string[] $promoted
9497
*/
9598
private function __construct(
9699
string $name,
@@ -99,7 +102,8 @@ private function __construct(
99102
string $filterType,
100103
array $subgroup,
101104
array $sort,
102-
int $limit
105+
int $limit,
106+
array $promoted
103107
) {
104108
$this->name = $name;
105109
$this->field = $field;
@@ -108,6 +112,7 @@ private function __construct(
108112
$this->subgroup = $subgroup;
109113
$this->sort = $sort;
110114
$this->limit = $limit;
115+
$this->promoted = $promoted;
111116
}
112117

113118
/**
@@ -180,16 +185,25 @@ public function getLimit(): int
180185
return $this->limit;
181186
}
182187

188+
/**
189+
* @return string[]
190+
*/
191+
public function getPromoted(): array
192+
{
193+
return $this->promoted;
194+
}
195+
183196
/**
184197
* Create.
185198
*
186-
* @param string $name
187-
* @param string $field
188-
* @param int $applicationType
189-
* @param string $filterType
190-
* @param array $subgroup
191-
* @param array $sort
192-
* @param int $limit
199+
* @param string $name
200+
* @param string $field
201+
* @param int $applicationType
202+
* @param string $filterType
203+
* @param array $subgroup
204+
* @param array $sort
205+
* @param int $limit
206+
* @param string[] $promoted
193207
*
194208
* @return Aggregation
195209
*/
@@ -200,7 +214,8 @@ public static function create(
200214
string $filterType,
201215
array $subgroup = [],
202216
array $sort = self::SORT_BY_COUNT_DESC,
203-
int $limit = self::NO_LIMIT
217+
int $limit = self::NO_LIMIT,
218+
array $promoted = []
204219
): self {
205220
return new self(
206221
$name,
@@ -209,7 +224,8 @@ public static function create(
209224
$filterType,
210225
$subgroup,
211226
$sort,
212-
$limit
227+
$limit,
228+
$promoted
213229
);
214230
}
215231

@@ -240,6 +256,9 @@ public function toArray(): array
240256
'limit' => self::NO_LIMIT === $this->limit
241257
? null
242258
: $this->limit,
259+
'promoted' => $this->promoted === []
260+
? []
261+
: $this->promoted,
243262
], function ($element) {
244263
return
245264
!(
@@ -269,7 +288,8 @@ public static function createFromArray(array $array): self
269288
$array['filter_type'] ?? Filter::TYPE_FIELD,
270289
$array['subgroup'] ?? [],
271290
$array['sort'] ?? self::SORT_BY_COUNT_DESC,
272-
$array['limit'] ?? self::NO_LIMIT
291+
$array['limit'] ?? self::NO_LIMIT,
292+
$array['promoted'] ?? []
273293
);
274294
}
275295
}

Query/Query.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ public function sortBy(SortBy $sortBy): self
737737
* @param int $applicationType
738738
* @param array $aggregationSort
739739
* @param int $limit
740+
* @param array $promoted
740741
*
741742
* @return Query
742743
*/
@@ -745,7 +746,8 @@ public function aggregateBy(
745746
string $field,
746747
int $applicationType,
747748
array $aggregationSort = Aggregation::SORT_BY_COUNT_DESC,
748-
int $limit = Aggregation::NO_LIMIT
749+
int $limit = Aggregation::NO_LIMIT,
750+
array $promoted = []
749751
): self {
750752
$this->aggregations[$filterName] = Aggregation::create(
751753
$filterName,
@@ -754,7 +756,8 @@ public function aggregateBy(
754756
Filter::TYPE_FIELD,
755757
[],
756758
$aggregationSort,
757-
$limit
759+
$limit,
760+
$promoted
758761
);
759762

760763
return $this;
@@ -770,6 +773,7 @@ public function aggregateBy(
770773
* @param string $filterType
771774
* @param array $aggregationSort
772775
* @param int $limit
776+
* @param array $promoted
773777
*
774778
* @return Query
775779
*/
@@ -780,7 +784,8 @@ public function aggregateByRange(
780784
int $applicationType,
781785
string $filterType = Filter::TYPE_RANGE,
782786
array $aggregationSort = Aggregation::SORT_BY_COUNT_DESC,
783-
int $limit = Aggregation::NO_LIMIT
787+
int $limit = Aggregation::NO_LIMIT,
788+
array $promoted = []
784789
): self {
785790
if (empty($options)) {
786791
return $this;
@@ -793,7 +798,8 @@ public function aggregateByRange(
793798
$filterType,
794799
$options,
795800
$aggregationSort,
796-
$limit
801+
$limit,
802+
$promoted
797803
);
798804

799805
return $this;
@@ -808,6 +814,7 @@ public function aggregateByRange(
808814
* @param int $applicationType
809815
* @param array $aggregationSort
810816
* @param int $limit
817+
* @param array $promoted
811818
*
812819
* @return Query
813820
*/
@@ -817,7 +824,8 @@ public function aggregateByDateRange(
817824
array $options,
818825
int $applicationType,
819826
array $aggregationSort = Aggregation::SORT_BY_COUNT_DESC,
820-
int $limit = Aggregation::NO_LIMIT
827+
int $limit = Aggregation::NO_LIMIT,
828+
array $promoted = []
821829
): self {
822830
if (empty($options)) {
823831
return $this;
@@ -830,7 +838,8 @@ public function aggregateByDateRange(
830838
Filter::TYPE_DATE_RANGE,
831839
$options,
832840
$aggregationSort,
833-
$limit
841+
$limit,
842+
$promoted
834843
);
835844

836845
return $this;

Tests/Query/AggregationTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function testCreate()
4747
$this->assertEquals(['xxx'], $aggregation->getSubgroup());
4848
$this->assertEquals(Aggregation::SORT_BY_COUNT_ASC, $aggregation->getSort());
4949
$this->assertEquals(10, $aggregation->getLimit());
50+
$this->assertEquals([], $aggregation->getPromoted());
5051
}
5152

5253
/**
@@ -88,6 +89,7 @@ public function testCreateDefaultValues()
8889
$this->assertEquals([], $aggregation->getSubgroup());
8990
$this->assertEquals(Aggregation::SORT_BY_COUNT_DESC, $aggregation->getSort());
9091
$this->assertEquals(Aggregation::NO_LIMIT, $aggregation->getLimit());
92+
$this->assertEquals([], $aggregation->getPromoted());
9193
}
9294

9395
/**
@@ -103,6 +105,7 @@ public function testToArray()
103105
'subgroup' => ['xxx'],
104106
'sort' => Aggregation::SORT_BY_COUNT_ASC,
105107
'limit' => 10,
108+
'promoted' => ['item1', 'item2'],
106109
];
107110

108111
$this->assertEquals(
@@ -124,6 +127,7 @@ public function testToArrayDefaultFields()
124127
'subgroup' => [],
125128
'sort' => Aggregation::SORT_BY_COUNT_DESC,
126129
'limit' => 0,
130+
'promoted' => [],
127131
];
128132

129133
$this->assertEquals(
@@ -158,5 +162,6 @@ public function testCreateFromArrayWithDefaults()
158162
$this->assertEquals([], $aggregation->getSubgroup());
159163
$this->assertEquals(Aggregation::SORT_BY_COUNT_DESC, $aggregation->getSort());
160164
$this->assertEquals(Aggregation::NO_LIMIT, $aggregation->getLimit());
165+
$this->assertEquals([], $aggregation->getPromoted());
161166
}
162167
}

Tests/Query/QueryTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
namespace Apisearch\Tests\Query;
1717

1818
use Apisearch\Model\IndexUUID;
19+
use Apisearch\Query\Aggregation;
20+
use Apisearch\Query\Filter;
1921
use Apisearch\Query\Query;
2022
use Apisearch\Query\ScoreStrategies;
2123
use Apisearch\Query\ScoreStrategy;
@@ -316,4 +318,16 @@ public function testSetQueryText()
316318
$this->assertEquals('lolazo', $queryAsArray['q']);
317319
$this->assertEquals('lolazo', $newQuery->getQueryText());
318320
}
321+
322+
public function testAggregationPromoted()
323+
{
324+
$query = Query::createMatchAll()
325+
->aggregateBy('field1', 'field1', Filter::AT_LEAST_ONE, Aggregation::SORT_BY_COUNT_ASC, 0, ['item1', 'item2'])
326+
->aggregateByRange('field2', 'field2', ['op1'], Filter::AT_LEAST_ONE, Filter::TYPE_RANGE, Aggregation::SORT_BY_COUNT_ASC, 0, ['item1', 'item3'])
327+
->aggregateByDateRange('field3', 'field3', ['op2'], Filter::AT_LEAST_ONE, Aggregation::SORT_BY_COUNT_ASC, 0, ['item4']);
328+
329+
$this->assertEquals(['item1', 'item2'], $query->getAggregation('field1')->getPromoted());
330+
$this->assertEquals(['item1', 'item3'], $query->getAggregation('field2')->getPromoted());
331+
$this->assertEquals(['item4'], $query->getAggregation('field3')->getPromoted());
332+
}
319333
}

0 commit comments

Comments
 (0)