@@ -40,6 +40,7 @@ class SearchQuery implements SortableQuery, CollapsibleQuery, HighlightingQuery
40
40
protected ?int $ from = null ;
41
41
protected array $ fields = [];
42
42
protected array $ include = [];
43
+ protected ?array $ pinnedIds = null ;
43
44
protected array $ exclude = [];
44
45
protected ?string $ searchType = null ;
45
46
@@ -144,17 +145,24 @@ protected function execute(
144
145
?int $ from = null ,
145
146
bool $ totals = false ,
146
147
bool $ source = true ,
147
- ?Cursor $ cursor = null
148
+ ?Cursor $ cursor = null ,
148
149
): array |Promise {
149
150
$ dsl = [
150
151
'size ' => $ size ,
151
152
'from ' => $ from ,
152
- 'query ' => $ this -> boolQuery -> toDSL () ,
153
+ 'query ' => [] ,
153
154
'track_total_hits ' => $ totals ,
154
155
'_source ' => $ this ->sourceToDSL ($ source ),
155
156
'fields ' => $ source && $ this ->fields ? $ this ->fields : null ,
156
157
];
157
158
159
+ if ($ this ->pinnedIds ) {
160
+ $ dsl ['query ' ]['pinned ' ]['ids ' ] = $ this ->pinnedIds ;
161
+ $ dsl ['query ' ]['pinned ' ]['organic ' ] = $ this ->boolQuery ->toDSL ();
162
+ } else {
163
+ $ dsl ['query ' ] = $ this ->boolQuery ->toDSL ();
164
+ }
165
+
158
166
$ sorts ??= $ this ->sorts ;
159
167
if (!$ sorts ->isEmpty ()) {
160
168
$ dsl ['sort ' ] = $ sorts ->toDSL ();
@@ -245,6 +253,13 @@ public function setPostFilter(BoolQueryBuilder $boolQueryBuilder): static
245
253
return $ this ;
246
254
}
247
255
256
+ public function pinned (array $ ids ): static
257
+ {
258
+ $ this ->pinnedIds = $ ids ;
259
+
260
+ return $ this ;
261
+ }
262
+
248
263
public function addAggregations (Aggregation $ aggregation ): static
249
264
{
250
265
$ this ->aggregations ??= new AggregationCollection ();
0 commit comments