Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed Oct 22, 2024
1 parent bade995 commit 4ed8895
Showing 1 changed file with 76 additions and 74 deletions.
150 changes: 76 additions & 74 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,79 +14,6 @@

class TypesenseEngine extends AbstractEngine
{
protected function buildSearchOptions(array $options, string $query, $useFacetFilters = true): array
{
$filters = collect($options['filter_by']);
$facets = $this->getFacetConfig();

$facetQuery = collect();

foreach ($facets as $facetConfig) {
if (empty($facetConfig['facet_query'])) {
continue;
}
$facetQuery->push($facetConfig['facet_query']);
}

$facetQuery = $facetQuery->join(',');

foreach ($this->filters as $key => $value) {
$filters->push($key.':'.collect($value)->join(','));
}

if ($useFacetFilters) {
foreach ($this->facets as $field => $values) {
$values = collect($values)->map(function ($value) {
if ($value == 'false' || $value == 'true') {
return $value;
}
return '`'.$value.'`';
});


if ($values->count() > 1) {
$filters->push($field.':['.collect($values)->join(',').']');

continue;
}

$filters->push($field.':'.collect($values)->join(','));
}
}

$options['q'] = $query;
$options['facet_query'] = $facetQuery;
$facets = $this->getFacetConfig();
$facetBy = array_keys($facets);

foreach ($facets as $field => $config) {
if (!($config['hierarchy'] ?? false)) {
continue;
}
unset(
$facetBy[array_search($field, $facetBy)]
);
$facetBy = [
...$facetBy,
...array_map(
fn ($value) => "{$field}.{$value}",
$config['levels'] ?? []
)
];
}

$options['facet_by'] = implode(',', $facetBy);
$options['max_facet_values'] = 50;

$options['sort_by'] = $this->sortByIsValid() ? $this->sort : '';

if ($filters->count()) {
$options['filter_by'] = $filters->join(' && ');
}

return $options;
}

public function get(): SearchResults
{
try {
Expand Down Expand Up @@ -205,14 +132,89 @@ public function get(): SearchResults
'per_page' => $paginator->perPage(),
'hits' => $documents,
'facets' => $facets,
'links' => $paginator->appends([
'links' => $paginator->setCollection(
collect($results['hits'])
)->appends([
'facets' => http_build_query($this->facets),
])->links(),
];

return SearchResults::from($data);
}

protected function buildSearchOptions(array $options, string $query, $useFacetFilters = true): array
{
$filters = collect($options['filter_by']);
$facets = $this->getFacetConfig();

$facetQuery = collect();

foreach ($facets as $facetConfig) {
if (empty($facetConfig['facet_query'])) {
continue;
}
$facetQuery->push($facetConfig['facet_query']);
}

$facetQuery = $facetQuery->join(',');

foreach ($this->filters as $key => $value) {
$filters->push($key.':'.collect($value)->join(','));
}

if ($useFacetFilters) {
foreach ($this->facets as $field => $values) {
$values = collect($values)->map(function ($value) {
if ($value == 'false' || $value == 'true') {
return $value;
}
return '`'.$value.'`';
});


if ($values->count() > 1) {
$filters->push($field.':['.collect($values)->join(',').']');

continue;
}

$filters->push($field.':'.collect($values)->join(','));
}
}

$options['q'] = $query;
$options['facet_query'] = $facetQuery;
$facets = $this->getFacetConfig();
$facetBy = array_keys($facets);

foreach ($facets as $field => $config) {
if (!($config['hierarchy'] ?? false)) {
continue;
}
unset(
$facetBy[array_search($field, $facetBy)]
);
$facetBy = [
...$facetBy,
...array_map(
fn ($value) => "{$field}.{$value}",
$config['levels'] ?? []
)
];
}

$options['facet_by'] = implode(',', $facetBy);
$options['max_facet_values'] = 50;

$options['sort_by'] = $this->sortByIsValid() ? $this->sort : '';

if ($filters->count()) {
$options['filter_by'] = $filters->join(' && ');
}

return $options;
}

protected function sortByIsValid(): bool
{
$sort = $this->sort;
Expand Down

0 comments on commit 4ed8895

Please sign in to comment.