Skip to content

Commit

Permalink
Fix searching through the CP
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell authored Aug 20, 2024
2 parents e6c8e96 + 88b5b84 commit aa83e31
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Typesense/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,26 @@ public function update()
return $this;
}

public function searchUsingApi($query, array $options = ['query_by' => ['title']]): Collection
public function searchUsingApi($query, array $options = []): Collection
{
$options['q'] = $query;

if (! isset($options['query_by'])) {
$schema = Arr::get($this->config, 'settings.schema', []);

// if we have fields in our schema use any strings, otherwise *
$options['query_by'] = collect($schema['fields'] ?? [])
->filter(fn ($field) => $field['type'] == 'string')
->map(fn ($field) => $field['name'])
->values()
->join(',') ?: '*';
}

$searchResults = $this->getOrCreateIndex()->documents->search($options);

return collect($searchResults['hits'] ?? [])
->map(function ($result, $i) {
$result['document']['reference'] = $result['document']['id'];
$result['document']['search_score'] = (int) ($result['text_match'] ?? 0);

return $result['document'];
Expand Down

0 comments on commit aa83e31

Please sign in to comment.