From 77340341689e8c1af3b9698f2935835f4360e3f3 Mon Sep 17 00:00:00 2001 From: Javier Date: Wed, 10 Mar 2021 20:41:23 +0100 Subject: [PATCH 1/3] Fix clauses and filters not working combined --- src/Engines/ElasticSearchEngine.php | 42 +++++++++++------------------ 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/src/Engines/ElasticSearchEngine.php b/src/Engines/ElasticSearchEngine.php index d1ab501..9c6cd4e 100644 --- a/src/Engines/ElasticSearchEngine.php +++ b/src/Engines/ElasticSearchEngine.php @@ -286,26 +286,10 @@ public function searchAll(AllBuilder $builder) 'size' => $builder->limit, 'from' => $builder->offset, 'body' => [ - 'query' => [ - 'bool' => [ - 'must' => [ - [ - 'query_string' => [ - 'query' => $builder->query, - 'type' => 'phrase', - 'default_operator' => 'and', - ], - ] - ], - ], - ], + 'query' => [], ], ]; - foreach ($this->ranges($builder) as $field => $ranges) { - $params['body']['query']['bool']['must'][]['range'][$field] = $ranges; - } - foreach ($this->filters($builder) as $field => $value) { $queryParameter = is_array($value) ? 'terms' : 'term'; @@ -314,16 +298,22 @@ public function searchAll(AllBuilder $builder) ]; } - if (empty($builder->query)) { - $queryContainsFilters = $this->filters($builder); + foreach ($this->ranges($builder) as $field => $ranges) { + $params['body']['query']['bool']['must'][]['range'][$field] = $ranges; + } - if (! $queryContainsFilters) { - $params['body']['query'] = [ - 'match_all' => (object)null - ]; - } else { - $params['body']['query']['bool']['must']['match_all'] = (object)null; - } + if (! empty($builder->query)) { + $params['body']['query']['bool']['must'][]['query_string'] = [ + 'query' => $builder->query, + 'type' => 'phrase', + 'default_operator' => 'and', + ]; + } + + if (! $builder->query && ! $this->ranges($builder) && ! $this->filters($builder)) { + $params['body']['query'] = [ + 'match_all' => (object)null + ]; } if ($sort = $builder->sort) { From b97281a8400e6f810f1b0316f9b3349e21aba09a Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 29 Mar 2021 16:22:01 +0100 Subject: [PATCH 2/3] Update to make is possible to use multiple where clauses when searching all --- src/Engines/ElasticSearchEngine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Engines/ElasticSearchEngine.php b/src/Engines/ElasticSearchEngine.php index 9c6cd4e..34f3be1 100644 --- a/src/Engines/ElasticSearchEngine.php +++ b/src/Engines/ElasticSearchEngine.php @@ -293,7 +293,7 @@ public function searchAll(AllBuilder $builder) foreach ($this->filters($builder) as $field => $value) { $queryParameter = is_array($value) ? 'terms' : 'term'; - $params['body']['query']['bool']['filter'] = [ + $params['body']['query']['bool']['filter'][] = [ $queryParameter => [$field => $value] ]; } From 98058f941bea57edcb1f1e219b7127a20b83f401 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 29 Mar 2021 19:29:19 +0100 Subject: [PATCH 3/3] Make it possible to make elasticsearch only respond when update and bulk tasks are finished which helps with running tests related to elasticsearch --- src/Engines/ElasticSearchEngine.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Engines/ElasticSearchEngine.php b/src/Engines/ElasticSearchEngine.php index 34f3be1..2701d7b 100644 --- a/src/Engines/ElasticSearchEngine.php +++ b/src/Engines/ElasticSearchEngine.php @@ -79,7 +79,9 @@ public function update($models) $objects['body'][] = array_merge(['model' => get_class($model)], $searchableData); } + if (!empty($objects)) { + $objects['refresh'] = config('laravel-search.wait_for', false); $this->client->bulk((array)$objects); } }