diff --git a/src/Engines/AbstractEngine.php b/src/Engines/AbstractEngine.php index 786a311..54005a1 100644 --- a/src/Engines/AbstractEngine.php +++ b/src/Engines/AbstractEngine.php @@ -23,6 +23,9 @@ abstract class AbstractEngine protected string $sort = ''; + protected string $sortRaw = ''; + + public function extendQuery(\Closure $callable): self { $this->queryExtenders[] = $callable; @@ -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; diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index 6511b60..e7631d7 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -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]); @@ -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), ];