Skip to content

Commit

Permalink
Allow query extending
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed Nov 5, 2024
1 parent b2bd74d commit bb07d2c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Engines/AbstractEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ abstract class AbstractEngine
{
protected string $modelType = Product::class;

protected array $queryExtenders = [];

protected ?string $query = null;

protected array $filters = [];
Expand All @@ -21,6 +23,13 @@ abstract class AbstractEngine

protected string $sort = '';

public function extendQuery(\Closure $callable): self
{
$this->queryExtenders[] = $callable;

return $this;
}

public function filter(array $filters): self
{
foreach ($filters as $key => $value) {
Expand Down Expand Up @@ -135,6 +144,10 @@ protected function getSearchQueries(): Collection
]);
}

foreach ($this->queryExtenders as $extender) {
$params = call_user_func($extender, $this, $queries);
}

return collect($queries);
}

Expand Down Expand Up @@ -168,7 +181,7 @@ protected function sortByIsValid(): bool

return $field && ($field['sort'] ?? false);
}

abstract public function get(): mixed;

abstract protected function getFieldConfig(): array;
Expand Down
2 changes: 2 additions & 0 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public function get(): SearchResults
]);
}


protected function buildSearch(array $options): array
{
$searchQueries = $this->getSearchQueries();
Expand Down Expand Up @@ -196,6 +197,7 @@ protected function buildSearch(array $options): array
return $requests;
}


protected function getFieldConfig(): array
{
return config('scout.typesense.model-settings.'.$this->modelType.'.collection-schema.fields', []);
Expand Down

0 comments on commit bb07d2c

Please sign in to comment.