Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Just pass anything specified in search_options over #3

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions src/Typesense/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public function searchUsingApi($query, array $options = []): Collection
->join(',') ?: '*';
}

if (! isset($options['sort_by'])) {
if ($sort = Arr::get($this->config, 'settings.search_options.sort_by', false)) {
$options['sort_by'] = $sort;
foreach (Arr::get($this->config, 'settings.search_options', []) as $handle => $value) {
if (! isset($options[$handle])) {
$options[$handle] = $value;
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
],
],
],
]);
}
}
24 changes: 24 additions & 0 deletions tests/Unit/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Loading