Skip to content

Commit d5b488a

Browse files
authored
Merge pull request #27 from cephee/Handled-whereIn-in-filters
Add possibility of using the whereIns array in filter method
2 parents d150ee8 + a4f3e5f commit d5b488a

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

src/Engines/TypesenseEngine.php

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ class TypesenseEngine extends Engine
5757
*/
5858
private array $locationOrderBy = [];
5959

60+
/**
61+
* @var bool
62+
*/
63+
private bool $exhaustiveSearch = false;
64+
6065
/**
6166
* TypesenseEngine constructor.
6267
*
@@ -148,6 +153,7 @@ private function buildSearchParams(Builder $builder, int $page, int | null $perP
148153
'page' => $page,
149154
'highlight_start_tag' => $this->startTag,
150155
'highlight_end_tag' => $this->endTag,
156+
'exhaustive_search' => $this->exhaustiveSearch,
151157
];
152158

153159
if ($this->limitHits > 0) {
@@ -240,24 +246,34 @@ protected function performSearch(Builder $builder, array $options = []): mixed
240246
*/
241247
protected function filters(Builder $builder): string
242248
{
243-
return collect($builder->wheres)
244-
->map([
245-
$this,
246-
'parseFilters',
247-
])
248-
->values()
249-
->implode(' && ');
249+
$whereFilter = collect($builder->wheres)
250+
->map([
251+
$this,
252+
'parseWhereFilter',
253+
])
254+
->values()
255+
->implode(' && ');
256+
257+
$whereInFilter = collect($builder->whereIns)
258+
->map([
259+
$this,
260+
'parseWhereInFilter',
261+
])
262+
->values()
263+
->implode(' && ');
264+
265+
return $whereFilter . ' && ' . $whereInFilter;
250266
}
251267

252268
/**
253-
* Parse typesense filters.
269+
* Parse typesense where filter.
254270
*
255271
* @param array|string $value
256272
* @param string $key
257273
*
258274
* @return string
259275
*/
260-
public function parseFilters(array|string $value, string $key): string
276+
public function parseWhereFilter(array|string $value, string $key): string
261277
{
262278
if (is_array($value)) {
263279
return sprintf('%s:%s', $key, implode('', $value));
@@ -266,6 +282,18 @@ public function parseFilters(array|string $value, string $key): string
266282
return sprintf('%s:=%s', $key, $value);
267283
}
268284

285+
/**
286+
* Parse typesense whereIn filter.
287+
*
288+
* @param array $value
289+
* @param string $key
290+
*
291+
* @return string
292+
*/
293+
public function parseWhereInFilter(array $value, string $key): string
294+
{
295+
return sprintf('%s:=%s', $key, '['. implode(', ', $value).']');
296+
}
269297

270298
/**
271299
* @param mixed $results
@@ -477,6 +505,20 @@ public function orderByLocation(string $column, float $lat, float $lng, string $
477505
return $this;
478506
}
479507

508+
/**
509+
* Setting this to true will make Typesense consider all variations of prefixes and typo corrections of the words in the query exhaustively.
510+
*
511+
* @param bool $exhaustiveSearch
512+
*
513+
* @return $this
514+
*/
515+
public function exhaustiveSearch(bool $exhaustiveSearch): static
516+
{
517+
$this->exhaustiveSearch = $exhaustiveSearch;
518+
519+
return $this;
520+
}
521+
480522
/**
481523
* @param string $name
482524
*

src/Mixin/BuilderMixin.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,19 @@ public function limitHits(): Closure
120120
return $this;
121121
};
122122
}
123+
124+
/**
125+
* @param bool $exhaustiveSearch
126+
*
127+
* @return Closure
128+
*/
129+
public function exhaustiveSearch(): Closure
130+
{
131+
return function (bool $exhaustiveSearch) {
132+
$this->engine()
133+
->exhaustiveSearch($exhaustiveSearch);
134+
135+
return $this;
136+
};
137+
}
123138
}

0 commit comments

Comments
 (0)