diff --git a/src/Search/SearchQuery.php b/src/Search/SearchQuery.php index 0b14dbd..ec879b0 100644 --- a/src/Search/SearchQuery.php +++ b/src/Search/SearchQuery.php @@ -40,6 +40,7 @@ class SearchQuery implements SortableQuery, CollapsibleQuery, HighlightingQuery protected ?int $from = null; protected array $fields = []; protected array $include = []; + protected ?array $pinnedIds = null; protected array $exclude = []; protected ?string $searchType = null; @@ -144,17 +145,24 @@ protected function execute( ?int $from = null, bool $totals = false, bool $source = true, - ?Cursor $cursor = null + ?Cursor $cursor = null, ): array|Promise { $dsl = [ 'size' => $size, 'from' => $from, - 'query' => $this->boolQuery->toDSL(), + 'query' => [], 'track_total_hits' => $totals, '_source' => $this->sourceToDSL($source), 'fields' => $source && $this->fields ? $this->fields : null, ]; + if ($this->pinnedIds) { + $dsl['query']['pinned']['ids'] = $this->pinnedIds; + $dsl['query']['pinned']['organic'] = $this->boolQuery->toDSL(); + } else { + $dsl['query'] = $this->boolQuery->toDSL(); + } + $sorts ??= $this->sorts; if (!$sorts->isEmpty()) { $dsl['sort'] = $sorts->toDSL(); @@ -245,6 +253,13 @@ public function setPostFilter(BoolQueryBuilder $boolQueryBuilder): static return $this; } + public function pinned(array $ids): static + { + $this->pinnedIds = $ids; + + return $this; + } + public function addAggregations(Aggregation $aggregation): static { $this->aggregations ??= new AggregationCollection();