From 724e555bbedd2250e66ad38f6223df05915bfa6f Mon Sep 17 00:00:00 2001 From: Andrey Kapitanov Date: Wed, 11 Jun 2025 12:36:05 +0300 Subject: [PATCH] V7 added pinned --- src/Search/SearchQuery.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Search/SearchQuery.php b/src/Search/SearchQuery.php index b91f310..6673f9a 100644 --- a/src/Search/SearchQuery.php +++ b/src/Search/SearchQuery.php @@ -39,6 +39,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; @@ -149,12 +150,19 @@ protected function execute( $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(); @@ -247,6 +255,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();