diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index 76c30e9..588a921 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -142,6 +142,11 @@ class TypesenseEngine extends Engine */ private array $optionsMulti = []; + /** + * @var string|null + */ + private ?string $prefix = null; + /** * TypesenseEngine constructor. * @@ -306,6 +311,10 @@ private function buildSearchParams(Builder $builder, int $page, int|null $perPag } $params['sort_by'] .= $this->parseOrderBy($builder->orders); } + + if (!empty($this->prefix)) { + $params['prefix'] = $this->prefix; + } return $params; } @@ -850,6 +859,25 @@ public function setPrioritizeExactMatch(bool $prioritizeExactMatch): static return $this; } + /** + * Indicates that the last word in the query should be treated as a prefix, and not as a whole word. + * + * You can also control the behavior of prefix search on a per field basis. + * For example, if you are querying 3 fields and want to enable prefix searching only on the first field, use ?prefix=true,false,false. + * The order should match the order of fields in query_by. + * If a single value is specified for prefix the same value is used for all fields specified in query_by. + * + * @param string $prefix + * + * @return $this + */ + public function setPrefix(string $prefix): static + { + $this->prefix = $prefix; + + return $this; + } + /** * If you have some overrides defined but want to disable all of them for a particular search query * diff --git a/src/Mixin/BuilderMixin.php b/src/Mixin/BuilderMixin.php index 9e42c30..8843d9a 100644 --- a/src/Mixin/BuilderMixin.php +++ b/src/Mixin/BuilderMixin.php @@ -346,6 +346,21 @@ public function setPrioritizeExactMatch(): Closure }; } + /** + * @param string $prefix + * + * @return \Closure + */ + public function setPrefix(): Closure + { + return function (string $prefix) { + $this->engine() + ->setPrefix($prefix); + + return $this; + }; + } + /** * @param bool $enableOverrides *