@@ -40,6 +40,7 @@ class SearchQuery implements SortableQuery, CollapsibleQuery, HighlightingQuery
4040 protected ?int $ from = null ;
4141 protected array $ fields = [];
4242 protected array $ include = [];
43+ protected ?array $ pinnedIds = null ;
4344 protected array $ exclude = [];
4445 protected ?string $ searchType = null ;
4546
@@ -144,17 +145,24 @@ protected function execute(
144145 ?int $ from = null ,
145146 bool $ totals = false ,
146147 bool $ source = true ,
147- ?Cursor $ cursor = null
148+ ?Cursor $ cursor = null ,
148149 ): array |Promise {
149150 $ dsl = [
150151 'size ' => $ size ,
151152 'from ' => $ from ,
152- 'query ' => $ this -> boolQuery -> toDSL () ,
153+ 'query ' => [] ,
153154 'track_total_hits ' => $ totals ,
154155 '_source ' => $ this ->sourceToDSL ($ source ),
155156 'fields ' => $ source && $ this ->fields ? $ this ->fields : null ,
156157 ];
157158
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+
158166 $ sorts ??= $ this ->sorts ;
159167 if (!$ sorts ->isEmpty ()) {
160168 $ dsl ['sort ' ] = $ sorts ->toDSL ();
@@ -245,6 +253,13 @@ public function setPostFilter(BoolQueryBuilder $boolQueryBuilder): static
245253 return $ this ;
246254 }
247255
256+ public function pinned (array $ ids ): static
257+ {
258+ $ this ->pinnedIds = $ ids ;
259+
260+ return $ this ;
261+ }
262+
248263 public function addAggregations (Aggregation $ aggregation ): static
249264 {
250265 $ this ->aggregations ??= new AggregationCollection ();
0 commit comments