Skip to content

Commit

Permalink
Merge pull request #79 from alexgschwend/fix/add-prefix-param
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdullahFaqeir authored Nov 27, 2023
2 parents b8fdc98 + 21d23aa commit 70bf417
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ class TypesenseEngine extends Engine
*/
private array $optionsMulti = [];

/**
* @var string|null
*/
private ?string $prefix = null;

/**
* TypesenseEngine constructor.
*
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
*
Expand Down
15 changes: 15 additions & 0 deletions src/Mixin/BuilderMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 70bf417

Please sign in to comment.