diff --git a/src/Concerns/ExtendsSort.php b/src/Concerns/ExtendsSort.php index 7e4a921..b4fe865 100644 --- a/src/Concerns/ExtendsSort.php +++ b/src/Concerns/ExtendsSort.php @@ -9,7 +9,7 @@ /** * @psalm-require-implements \Ensi\LaravelElasticQuery\Contracts\SortableQuery * - * @method static sortBy(string $field, string $order = SortOrder::ASC, ?string $mode = null, ?string $missingValues = null) + * @method static sortBy(string $field, string $order = SortOrder::ASC, ?string $mode = null, ?string $missingValues = null, ?string $unmappedType = null) */ trait ExtendsSort { diff --git a/src/Contracts/SortableQuery.php b/src/Contracts/SortableQuery.php index 9009ce3..5379cc7 100644 --- a/src/Contracts/SortableQuery.php +++ b/src/Contracts/SortableQuery.php @@ -7,7 +7,7 @@ interface SortableQuery extends BoolQuery { - public function sortBy(string $field, string $order = SortOrder::ASC, ?string $mode = null, ?string $missingValues = null): static; + public function sortBy(string $field, string $order = SortOrder::ASC, ?string $mode = null, ?string $missingValues = null, ?string $unmappedType = null): static; public function minSortBy(string $field, string $order = SortOrder::ASC): static; diff --git a/src/Search/SearchQuery.php b/src/Search/SearchQuery.php index 36eccd3..c5bfda8 100644 --- a/src/Search/SearchQuery.php +++ b/src/Search/SearchQuery.php @@ -198,10 +198,10 @@ protected function parseHits(array $response): Collection //endregion //region Customization - public function sortBy(string $field, string $order = SortOrder::ASC, ?string $mode = null, ?string $missingValues = null): static + public function sortBy(string $field, string $order = SortOrder::ASC, ?string $mode = null, ?string $missingValues = null, ?string $unmappedType = null): static { (new SortBuilder($this->sorts)) - ->sortBy($field, $order, $mode, $missingValues); + ->sortBy($field, $order, $mode, $missingValues, $unmappedType); return $this; } diff --git a/src/Search/Sorting/Sort.php b/src/Search/Sorting/Sort.php index ed82f53..8a6ec4b 100644 --- a/src/Search/Sorting/Sort.php +++ b/src/Search/Sorting/Sort.php @@ -19,6 +19,7 @@ public function __construct( private ?string $missingValues = null, private ?string $type = null, private ?Script $script = null, + private ?string $unmappedType = null, ) { Assert::stringNotEmpty(trim($field)); Assert::oneOf($order, SortOrder::cases()); @@ -54,6 +55,10 @@ public function toDSL(): array $details['script'] = $this->script->toDSL(); } + if ($this->unmappedType !== null) { + $details['unmapped_type'] = $this->unmappedType; + } + if (!$details) { return [$this->field => $this->order]; } diff --git a/src/Search/Sorting/SortBuilder.php b/src/Search/Sorting/SortBuilder.php index 0dfb850..a887efe 100644 --- a/src/Search/Sorting/SortBuilder.php +++ b/src/Search/Sorting/SortBuilder.php @@ -28,7 +28,7 @@ public function __construct(SortCollection $sorts) $this->levels = new Collection(); } - public function sortBy(string $field, string $order = SortOrder::ASC, ?string $mode = null, ?string $missingValues = null): static + public function sortBy(string $field, string $order = SortOrder::ASC, ?string $mode = null, ?string $missingValues = null, ?string $unmappedType = null): static { $path = $this->absolutePath($field); @@ -37,7 +37,8 @@ public function sortBy(string $field, string $order = SortOrder::ASC, ?string $m strtolower($order), $mode === null ? $mode : strtolower($mode), $this->buildNested(), - $missingValues + $missingValues, + unmappedType: $unmappedType ); $this->sorts->add($sort);