From 4b69bcb111251ff747961bfec9ae94a940a4631f Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 20 Aug 2024 13:46:02 +0100 Subject: [PATCH 1/4] Just pass search_options over --- src/Typesense/Index.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Typesense/Index.php b/src/Typesense/Index.php index c2158a8..66cc5a2 100644 --- a/src/Typesense/Index.php +++ b/src/Typesense/Index.php @@ -103,6 +103,12 @@ public function searchUsingApi($query, array $options = []): Collection ->join(',') ?: '*'; } + foreach (Arr::get($this->config, 'settings.search_options', []) as $handle => $value) { + if (! isset($options[$handle])) { + $options[$handle] = $value; + } + } + $searchResults = $this->getOrCreateIndex()->documents->search($options); return collect($searchResults['hits'] ?? []) From f099d09f2d50fb964c079bfeda2e1cd880f98938 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 20 Aug 2024 13:48:09 +0100 Subject: [PATCH 2/4] Allow anything but q and query_by --- README.md | 5 ++++- src/Typesense/Index.php | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4bbf190..3cd83a2 100644 --- a/README.md +++ b/README.md @@ -92,9 +92,12 @@ Any additional settings you want to define per index can be included in the `sta ], ], ], + /* + Pass any of the options from https://typesense.org/docs/26.0/api/search.html#search-parameters + */ 'search_options' => [ /* - Specify a custom sort by order, see the Typesense documentation for more info: + eg 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 66cc5a2..70fc266 100644 --- a/src/Typesense/Index.php +++ b/src/Typesense/Index.php @@ -104,6 +104,10 @@ public function searchUsingApi($query, array $options = []): Collection } foreach (Arr::get($this->config, 'settings.search_options', []) as $handle => $value) { + if (in_array($handle, ['query_by', 'q'])) { + continue; + } + if (! isset($options[$handle])) { $options[$handle] = $value; } From 78c3eed681bf0417c505f9e88134b9cb9285feb5 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 20 Aug 2024 13:54:03 +0100 Subject: [PATCH 3/4] check isnt needed as the isset check will handle them --- src/Typesense/Index.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Typesense/Index.php b/src/Typesense/Index.php index 70fc266..66cc5a2 100644 --- a/src/Typesense/Index.php +++ b/src/Typesense/Index.php @@ -104,10 +104,6 @@ public function searchUsingApi($query, array $options = []): Collection } foreach (Arr::get($this->config, 'settings.search_options', []) as $handle => $value) { - if (in_array($handle, ['query_by', 'q'])) { - continue; - } - if (! isset($options[$handle])) { $options[$handle] = $value; } From a9f6ddb0d06362c9d2f06cd1f77656d79ced1f23 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 20 Aug 2024 14:13:59 +0100 Subject: [PATCH 4/4] Add some test coverage --- tests/TestCase.php | 11 +++++++++++ tests/Unit/IndexTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/tests/TestCase.php b/tests/TestCase.php index 9ca79d5..008e0d0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -44,6 +44,17 @@ protected function resolveApplicationConfiguration($app) $app['config']->set('statamic.search.indexes.typesense_index', [ 'driver' => 'typesense', 'searchables' => ['collection:pages'], + 'settings' => [ + 'schema' => [ + 'fields' => [ + [ + 'type' => 'string', + 'name' => 'title', + 'sort' => true, + ], + ], + ], + ], ]); } } diff --git a/tests/Unit/IndexTest.php b/tests/Unit/IndexTest.php index 9c738e9..95d721f 100644 --- a/tests/Unit/IndexTest.php +++ b/tests/Unit/IndexTest.php @@ -108,4 +108,28 @@ public function it_removes_documents_from_the_index() $this->assertNotContains('entry::test-1', $export); $this->assertContains('entry::test-2', $export); } + + #[Test] + public function it_sorts_by_specified_order() + { + $entry1 = Facades\Entry::make() + ->id('test-2') + ->collection('pages') + ->data(['title' => 'Entry 1']) + ->save(); + + $entry2 = tap(Facades\Entry::make() + ->id('test-1') + ->collection('pages') + ->data(['title' => 'Entry 2'])) + ->save(); + + $results = Facades\Search::index('typesense_index')->searchUsingApi('*', ['sort_by' => 'title:asc']); + + $this->assertSame(['Entry 1', 'Entry 2'], collect($results)->pluck('title')->all()); + + $results = Facades\Search::index('typesense_index')->searchUsingApi('*', ['sort_by' => 'title:desc']); + + $this->assertSame(['Entry 2', 'Entry 1'], collect($results)->pluck('title')->all()); + } }