Skip to content

Commit

Permalink
Merge pull request #2 from Cyber-Duck/feature/add-array-as-argument-f…
Browse files Browse the repository at this point in the history
…or-where-clause-and-range-method

Add arrays to where clause, ranges method and polish empty string on search term functionality
  • Loading branch information
JaviPedrera authored Mar 10, 2021
2 parents dbad0bc + d8cfcb2 commit bc5b1e5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 17 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"keywords": ["Laravel", "laravel-search"],
"require": {
"elasticsearch/elasticsearch": "^7.8",
"illuminate/support": "~5|~6|~7|~8"
"illuminate/support": "~5|~6|~7|~8",
"laravel/helpers": "^1.4"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
Expand Down
35 changes: 35 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class Builder
*/
public $wheres = [];

/**
* @var array
*/
public $ranges = [];

/**
* The "limit" that should be applied to the search.
*
Expand Down Expand Up @@ -128,6 +133,36 @@ public function where($field, $value)
return $this;
}

/**
* Add a range for a single field with one or more operators
*
* @param string $field
* @param array $ranges
*
* @return $this
*/
public function ranges($field, array $ranges)
{
$validOperators = ['gt', 'gte', 'lt', 'lte'];
$selectedOperators = array_keys($ranges);

foreach ($selectedOperators as $selectedOperator) {
$isValidOperator = in_array($selectedOperator, $validOperators);
if ($isValidOperator) {
continue;
}

throw new \InvalidArgumentException("Invalid operator {$selectedOperator}");
}

$this->ranges[$field] = array_merge(
$this->ranges[$field] ?? [],
$ranges
);

return $this;
}

/**
* Set the "limit" for the search query.
*
Expand Down
56 changes: 40 additions & 16 deletions src/Engines/ElasticSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,16 @@ protected function performSearch(Builder $builder, array $filters = [])
*/
protected function filters(Builder $builder)
{
return collect($builder->wheres)->map(function ($value, $key) {
return [$key => $value];
})->values()->all();
return $builder->wheres;
}

/**
* @param Builder $builder
* @return array
*/
protected function ranges(Builder $builder)
{
return $builder->ranges;
}

/**
Expand All @@ -280,28 +287,45 @@ public function searchAll(AllBuilder $builder)
'from' => $builder->offset,
'body' => [
'query' => [
'query_string' => [
'query' => $builder->query,
'type' => 'phrase',
'default_operator' => 'and',
'bool' => [
'must' => [
[
'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) {
$params['body']['filter']['bool']['must'] = [
'term' => [$field => $value]
$queryParameter = is_array($value) ? 'terms' : 'term';

$params['body']['query']['bool']['filter'] = [
$queryParameter => [$field => $value]
];
}

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 bc5b1e5

Please sign in to comment.