Skip to content

Commit

Permalink
Merge pull request #27 from cephee/Handled-whereIn-in-filters
Browse files Browse the repository at this point in the history
Add possibility of using the whereIns array in filter method
  • Loading branch information
jasonbosco committed Jul 20, 2022
2 parents d150ee8 + a4f3e5f commit d5b488a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
60 changes: 51 additions & 9 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class TypesenseEngine extends Engine
*/
private array $locationOrderBy = [];

/**
* @var bool
*/
private bool $exhaustiveSearch = false;

/**
* TypesenseEngine constructor.
*
Expand Down Expand Up @@ -148,6 +153,7 @@ private function buildSearchParams(Builder $builder, int $page, int | null $perP
'page' => $page,
'highlight_start_tag' => $this->startTag,
'highlight_end_tag' => $this->endTag,
'exhaustive_search' => $this->exhaustiveSearch,
];

if ($this->limitHits > 0) {
Expand Down Expand Up @@ -240,24 +246,34 @@ protected function performSearch(Builder $builder, array $options = []): mixed
*/
protected function filters(Builder $builder): string
{
return collect($builder->wheres)
->map([
$this,
'parseFilters',
])
->values()
->implode(' && ');
$whereFilter = collect($builder->wheres)
->map([
$this,
'parseWhereFilter',
])
->values()
->implode(' && ');

$whereInFilter = collect($builder->whereIns)
->map([
$this,
'parseWhereInFilter',
])
->values()
->implode(' && ');

return $whereFilter . ' && ' . $whereInFilter;
}

/**
* Parse typesense filters.
* Parse typesense where filter.
*
* @param array|string $value
* @param string $key
*
* @return string
*/
public function parseFilters(array|string $value, string $key): string
public function parseWhereFilter(array|string $value, string $key): string
{
if (is_array($value)) {
return sprintf('%s:%s', $key, implode('', $value));
Expand All @@ -266,6 +282,18 @@ public function parseFilters(array|string $value, string $key): string
return sprintf('%s:=%s', $key, $value);
}

/**
* Parse typesense whereIn filter.
*
* @param array $value
* @param string $key
*
* @return string
*/
public function parseWhereInFilter(array $value, string $key): string
{
return sprintf('%s:=%s', $key, '['. implode(', ', $value).']');
}

/**
* @param mixed $results
Expand Down Expand Up @@ -477,6 +505,20 @@ public function orderByLocation(string $column, float $lat, float $lng, string $
return $this;
}

/**
* Setting this to true will make Typesense consider all variations of prefixes and typo corrections of the words in the query exhaustively.
*
* @param bool $exhaustiveSearch
*
* @return $this
*/
public function exhaustiveSearch(bool $exhaustiveSearch): static
{
$this->exhaustiveSearch = $exhaustiveSearch;

return $this;
}

/**
* @param string $name
*
Expand Down
15 changes: 15 additions & 0 deletions src/Mixin/BuilderMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,19 @@ public function limitHits(): Closure
return $this;
};
}

/**
* @param bool $exhaustiveSearch
*
* @return Closure
*/
public function exhaustiveSearch(): Closure
{
return function (bool $exhaustiveSearch) {
$this->engine()
->exhaustiveSearch($exhaustiveSearch);

return $this;
};
}
}

0 comments on commit d5b488a

Please sign in to comment.