From ac0fa228b332315a0667820260aa239ca7e69a3f Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Wed, 11 Dec 2024 14:17:20 +0000 Subject: [PATCH 1/3] hybrid testing --- src/Engines/TypesenseEngine.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index 6511b60..8e4b975 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -27,6 +27,7 @@ public function get(): SearchResults ) ]; + $response = $engine->getMultiSearch()->perform($request, [ 'collection' => (new $this->modelType)->searchableAs(), ]); @@ -182,8 +183,9 @@ protected function buildSearch(array $options): array ...$options, 'q' => $searchQuery->query, 'facet_query' => $facetQuery, + 'prefix' => false, 'max_facet_values' => 50, - 'sort_by' => $this->sortByIsValid() ? $this->sort : '', + 'sort_by' => '_text_match:desc', 'facet_by' => implode(',', $searchQuery->facets), ]; From fea04aef88b5043b115ad70404754f4f3c7db8af Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Fri, 13 Dec 2024 12:35:07 +0000 Subject: [PATCH 2/3] Handle empty searches --- src/Engines/TypesenseEngine.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index 8e4b975..6fd61bf 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -27,7 +27,6 @@ public function get(): SearchResults ) ]; - $response = $engine->getMultiSearch()->perform($request, [ 'collection' => (new $this->modelType)->searchableAs(), ]); @@ -179,13 +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' => '_text_match:desc', + 'sort_by' => $this->sortByIsValid() ? $this->sort : '_text_match:desc', 'facet_by' => implode(',', $searchQuery->facets), ]; From 785118fb138f312e68043d6c3f105e084ab8c01c Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Tue, 17 Dec 2024 09:38:35 +0000 Subject: [PATCH 3/3] Add sortRaw methos --- src/Engines/AbstractEngine.php | 10 ++++++++++ src/Engines/TypesenseEngine.php | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) 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 6fd61bf..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]); @@ -191,7 +191,7 @@ protected function buildSearch(array $options): array 'facet_query' => $facetQuery, 'prefix' => false, 'max_facet_values' => 50, - 'sort_by' => $this->sortByIsValid() ? $this->sort : '_text_match:desc', + 'sort_by' => $this->sortRaw ?: ($this->sortByIsValid() ? $this->sort : '_text_match:desc'), 'facet_by' => implode(',', $searchQuery->facets), ];