Skip to content

Commit

Permalink
Merge pull request #1 from lunarphp/typesense-hybrid-search
Browse files Browse the repository at this point in the history
Typesense hybrid search
  • Loading branch information
alecritson authored Dec 17, 2024
2 parents 0fe6650 + 785118f commit de88b9e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Engines/AbstractEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ abstract class AbstractEngine

protected string $sort = '';

protected string $sortRaw = '';


public function extendQuery(\Closure $callable): self
{
$this->queryExtenders[] = $callable;
Expand Down Expand Up @@ -96,6 +99,13 @@ public function sort(string $sort): self
return $this;
}

public function sortRaw(string $sort): self
{
$this->sortRaw = $sort;

return $this;
}

public function query(string $query): AbstractEngine
{
$this->query = $query;
Expand Down
12 changes: 10 additions & 2 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function get(): SearchResults
$response = $engine->getMultiSearch()->perform($request, [
'collection' => (new $this->modelType)->searchableAs(),
]);

$completeResults = $response['results'][0];

unset( $response['results'][0]);
Expand Down Expand Up @@ -178,12 +178,20 @@ protected function buildSearch(array $options): array
$filters->push($field.':='.collect($values)->join(','));
}

$queryBy = $options['query_by'];

if (!$this->query) {
$queryBy = str_replace('embedding,', '', $queryBy);
}

$params = [
...$options,
'query_by' => $queryBy,
'q' => $searchQuery->query,
'facet_query' => $facetQuery,
'prefix' => false,
'max_facet_values' => 50,
'sort_by' => $this->sortByIsValid() ? $this->sort : '',
'sort_by' => $this->sortRaw ?: ($this->sortByIsValid() ? $this->sort : '_text_match:desc'),
'facet_by' => implode(',', $searchQuery->facets),
];

Expand Down

0 comments on commit de88b9e

Please sign in to comment.