Skip to content

Commit 70bf417

Browse files
Merge pull request #79 from alexgschwend/fix/add-prefix-param
2 parents b8fdc98 + 21d23aa commit 70bf417

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Engines/TypesenseEngine.php

+28
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ class TypesenseEngine extends Engine
142142
*/
143143
private array $optionsMulti = [];
144144

145+
/**
146+
* @var string|null
147+
*/
148+
private ?string $prefix = null;
149+
145150
/**
146151
* TypesenseEngine constructor.
147152
*
@@ -306,6 +311,10 @@ private function buildSearchParams(Builder $builder, int $page, int|null $perPag
306311
}
307312
$params['sort_by'] .= $this->parseOrderBy($builder->orders);
308313
}
314+
315+
if (!empty($this->prefix)) {
316+
$params['prefix'] = $this->prefix;
317+
}
309318

310319
return $params;
311320
}
@@ -850,6 +859,25 @@ public function setPrioritizeExactMatch(bool $prioritizeExactMatch): static
850859
return $this;
851860
}
852861

862+
/**
863+
* Indicates that the last word in the query should be treated as a prefix, and not as a whole word.
864+
*
865+
* You can also control the behavior of prefix search on a per field basis.
866+
* For example, if you are querying 3 fields and want to enable prefix searching only on the first field, use ?prefix=true,false,false.
867+
* The order should match the order of fields in query_by.
868+
* If a single value is specified for prefix the same value is used for all fields specified in query_by.
869+
*
870+
* @param string $prefix
871+
*
872+
* @return $this
873+
*/
874+
public function setPrefix(string $prefix): static
875+
{
876+
$this->prefix = $prefix;
877+
878+
return $this;
879+
}
880+
853881
/**
854882
* If you have some overrides defined but want to disable all of them for a particular search query
855883
*

src/Mixin/BuilderMixin.php

+15
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,21 @@ public function setPrioritizeExactMatch(): Closure
346346
};
347347
}
348348

349+
/**
350+
* @param string $prefix
351+
*
352+
* @return \Closure
353+
*/
354+
public function setPrefix(): Closure
355+
{
356+
return function (string $prefix) {
357+
$this->engine()
358+
->setPrefix($prefix);
359+
360+
return $this;
361+
};
362+
}
363+
349364
/**
350365
* @param bool $enableOverrides
351366
*

0 commit comments

Comments
 (0)