Skip to content

Commit

Permalink
Just pass anything specified in search_options over (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell authored Aug 20, 2024
2 parents 28e733f + a9f6ddb commit 74c3878
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
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());
}
}

0 comments on commit 74c3878

Please sign in to comment.