Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filtering to work in the CP #8

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Typesense/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ public function getOrCreateIndex()
return $collection;
}

public function getTypesenseSchemaFields(): Collection
{
return collect(Arr::get($this->getOrCreateIndex()->retrieve(), 'fields', []))
->pluck('type', 'name');
}

private function getDefaultFields(Searchable $entry): array
{
return [
Expand Down
84 changes: 83 additions & 1 deletion src/Typesense/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,91 @@ public function getSearchResults($query)
return $this;
}

public function whereStatus($string)
{
return $this->where('status', $string);
}

private function wheresToFilter(array $wheres): string
{
$filterBy = '';

$schemaFields = $this->index->getTypesenseSchemaFields();

foreach ($this->wheres as $where) {
if ($filterBy != '') {
$filterBy .= $where['boolean'] == 'and' ? ' && ' : ' || ';
}

if ($where['type'] == 'Nested') {
$filterBy .= ' ( '.$this->wheresToFilter($where->query['wheres']).' ) ';

continue;
}

// if its not in our typesense schema, we cant filter on it
if (! $schemaType = $schemaFields->get($where['column'])) {
continue;
}

$filterBy .= ' ( ';

switch ($where['type']) {
case 'JsonContains':
case 'JsonOverlaps':
case 'WhereIn':
$filterBy .= $where['column'].':'.$this->transformArrayOfValuesForTypeSense($schemaType, $where['values']);
break;

case 'JsonDoesnContain':
case 'JsonDoesntOverlap':
case 'WhereNotIn':
$filterBy .= $where['column'].':!='.$this->transformArrayOfValuesForTypeSense($schemaType, $where['values']);
break;

default:
$filterBy .= $where['column'].':'.($where['operator'] != '=' ? $where['operator'] : '').$this->transformValueForTypeSense($schemaType, $where['value']);
break;
}

$filterBy .= ' ) ';

}

return $filterBy;
}

private function transformArrayOfValuesForTypeSense(string $schemaType, array $values): array
{
return json_encode(
collect($where['values'])
->map(fn ($value) => $this->transformValueForTypeSense($schemaType, $values))
->values()
->all()
);
}

private function transformValueForTypeSense(string $schemaType, mixed $value): mixed
{
return match (str_replace('[]', '', $schemaType)) {
'int32', 'int64' => (int) $value,
'float' => (float) $value,
'bool' => $value ? 'true' : 'false',
default => '`'.$value.'`'
};
}

private function getApiResults()
{
return $this->index->searchUsingApi($this->query ?? '', ['per_page' => $this->perPage, 'page' => $this->page]);
$options = ['per_page' => $this->perPage, 'page' => $this->page];

$filterBy = $this->wheresToFilter($this->wheres);

if ($filterBy) {
$options['filter_by'] = $filterBy;
}

return $this->index->searchUsingApi($this->query ?? '', $options);
}

public function forPage($page, $perPage = null)
Expand Down
Loading