Skip to content

Commit

Permalink
Make ranges and empty search term to work in conjunction with other f…
Browse files Browse the repository at this point in the history
…ilters applied
  • Loading branch information
Javier committed Mar 10, 2021
1 parent e8f702b commit d8cfcb2
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/Engines/ElasticSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,7 @@ protected function filters(Builder $builder)
*/
protected function ranges(Builder $builder)
{
$ranges = $builder->ranges;

$field = key($ranges);
return [
$field => $ranges
];
return $builder->ranges;
}

/**
Expand All @@ -294,22 +289,21 @@ public function searchAll(AllBuilder $builder)
'query' => [
'bool' => [
'must' => [
'query_string' => [
'query' => $builder->query,
'type' => 'phrase',
'default_operator' => 'and',
],
[
'query_string' => [
'query' => $builder->query,
'type' => 'phrase',
'default_operator' => 'and',
],
]
],
],
],
],
];

if (empty($builder->query)) {
$params['body']['query'] = [
'match_all' => (object)null
];

foreach ($this->ranges($builder) as $field => $ranges) {
$params['body']['query']['bool']['must'][]['range'][$field] = $ranges;
}

foreach ($this->filters($builder) as $field => $value) {
Expand All @@ -320,6 +314,18 @@ public function searchAll(AllBuilder $builder)
];
}

if (empty($builder->query)) {
$queryContainsFilters = $this->filters($builder);

if (! $queryContainsFilters) {
$params['body']['query'] = [
'match_all' => (object)null
];
} else {
$params['body']['query']['bool']['must']['match_all'] = (object)null;
}
}

if ($sort = $builder->sort) {
$params['body']['sort'] = $sort;
}
Expand Down

0 comments on commit d8cfcb2

Please sign in to comment.