From 3162202888feca15ec3ce08ed472e5443dec72f7 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 20 Aug 2024 13:26:46 +0100 Subject: [PATCH 1/3] Support sort_by when querying --- README.md | 5 +++++ src/Typesense/Index.php | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/README.md b/README.md index 60eb459..ba7c870 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,11 @@ Any additional settings you want to define per index can be included in the `sta ], ], ], + /* + Specify a custom sort by order, see the Typesense documentation for more info: + https://typesense.org/docs/guide/ranking-and-relevance.html#ranking-based-on-relevance-and-popularity + */ + 'sort_by' => '_text_match(buckets: 10):desc,weighted_score:desc', ], ], ``` diff --git a/src/Typesense/Index.php b/src/Typesense/Index.php index c2158a8..27e4dd0 100644 --- a/src/Typesense/Index.php +++ b/src/Typesense/Index.php @@ -103,6 +103,12 @@ public function searchUsingApi($query, array $options = []): Collection ->join(',') ?: '*'; } + if (! isset($options['sort_by'])) { + if ($sort = Arr::get($this->config, 'settings.sort_by', false)) { + $options['sort_by'] = $sort; + } + } + $searchResults = $this->getOrCreateIndex()->documents->search($options); return collect($searchResults['hits'] ?? []) From c17c2cda5779206aea1515e4402a5d2ca948e6cf Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 20 Aug 2024 13:28:29 +0100 Subject: [PATCH 2/3] Put it inside query for future proofing (we're likely to add others) --- README.md | 12 +++++++----- src/Typesense/Index.php | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ba7c870..56edc85 100644 --- a/README.md +++ b/README.md @@ -92,11 +92,13 @@ Any additional settings you want to define per index can be included in the `sta ], ], ], - /* - Specify a custom sort by order, see the Typesense documentation for more info: - https://typesense.org/docs/guide/ranking-and-relevance.html#ranking-based-on-relevance-and-popularity - */ - 'sort_by' => '_text_match(buckets: 10):desc,weighted_score:desc', + 'query' => [ + /* + Specify a custom sort by order, see the Typesense documentation for more info: + https://typesense.org/docs/guide/ranking-and-relevance.html#ranking-based-on-relevance-and-popularity + */ + 'sort_by' => '_text_match(buckets: 10):desc,weighted_score:desc', + ], ], ], ``` diff --git a/src/Typesense/Index.php b/src/Typesense/Index.php index 27e4dd0..feb7cd9 100644 --- a/src/Typesense/Index.php +++ b/src/Typesense/Index.php @@ -104,7 +104,7 @@ public function searchUsingApi($query, array $options = []): Collection } if (! isset($options['sort_by'])) { - if ($sort = Arr::get($this->config, 'settings.sort_by', false)) { + if ($sort = Arr::get($this->config, 'settings.query.sort_by', false)) { $options['sort_by'] = $sort; } } From 8c9546888e54c6941516701effa9712d5d852e32 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 20 Aug 2024 13:30:00 +0100 Subject: [PATCH 3/3] `search_options` --- README.md | 2 +- src/Typesense/Index.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 56edc85..4bbf190 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ Any additional settings you want to define per index can be included in the `sta ], ], ], - 'query' => [ + 'search_options' => [ /* Specify a custom sort by order, see the Typesense documentation for more info: https://typesense.org/docs/guide/ranking-and-relevance.html#ranking-based-on-relevance-and-popularity diff --git a/src/Typesense/Index.php b/src/Typesense/Index.php index feb7cd9..6f278aa 100644 --- a/src/Typesense/Index.php +++ b/src/Typesense/Index.php @@ -104,7 +104,7 @@ public function searchUsingApi($query, array $options = []): Collection } if (! isset($options['sort_by'])) { - if ($sort = Arr::get($this->config, 'settings.query.sort_by', false)) { + if ($sort = Arr::get($this->config, 'settings.search_options.sort_by', false)) { $options['sort_by'] = $sort; } }