From cdd6a4b55951d088ad5dde39e516d5cb3c7af73d Mon Sep 17 00:00:00 2001 From: hoersten <2041942+hoersten@users.noreply.github.com> Date: Wed, 1 Apr 2020 15:21:42 -0500 Subject: [PATCH] Added whereMatch and whereNotMatch (#355) * Updated @pk1z pull request https://github.com/babenkoivan/scout-elasticsearch-driver/pull/154 to remove conflicts and added whereNotMatch filter and test * Fixed CI style issues --- README.md | 2 ++ src/Builders/FilterBuilder.php | 36 ++++++++++++++++++++++++++++ tests/Builders/FilterBuilderTest.php | 36 ++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/README.md b/README.md index cdb00fb..75eef2b 100644 --- a/README.md +++ b/README.md @@ -444,6 +444,8 @@ whereBetween($field, $value) | whereBetween('price', [100, 200]) | Checks if a v whereNotBetween($field, $value) | whereNotBetween('price', [100, 200]) | Checks if a value isn't in a range. whereExists($field) | whereExists('unemployed') | Checks if a value is defined. whereNotExists($field) | whereNotExists('unemployed') | Checks if a value isn't defined. +whereMatch($field, $value) | whereMatch('tags', 'travel') | Filters records matching exact value. [Here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html) you can find more about syntax. +whereNotMatch($field, $value) | whereNotMatch('tags', 'travel') | Filters records not matching exact value. [Here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html) you can find more about syntax. whereRegexp($field, $value, $flags = 'ALL') | whereRegexp('name.raw', 'A.+') | Filters records according to a given regular expression. [Here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#regexp-syntax) you can find more about syntax. whereGeoDistance($field, $value, $distance) | whereGeoDistance('location', [-70, 40], '1000m') | Filters records according to given point and distance from it. [Here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html) you can find more about syntax. whereGeoBoundingBox($field, array $value) | whereGeoBoundingBox('location', ['top_left' => [-74.1, 40.73], 'bottom_right' => [-71.12, 40.01]]) | Filters records within given boundings. [Here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html) you can find more about syntax. diff --git a/src/Builders/FilterBuilder.php b/src/Builders/FilterBuilder.php index 78f3e93..26ce1f7 100644 --- a/src/Builders/FilterBuilder.php +++ b/src/Builders/FilterBuilder.php @@ -282,6 +282,42 @@ public function whereNotExists($field) return $this; } + /** + * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html Match query + * + * @param string $field + * @param string $value + * @return $this + */ + public function whereMatch($field, $value) + { + $this->wheres['must'][] = [ + 'match' => [ + $field => $value, + ], + ]; + + return $this; + } + + /** + * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html Match query + * + * @param string $field + * @param string $value + * @return $this + */ + public function whereNotMatch($field, $value) + { + $this->wheres['must_not'][] = [ + 'match' => [ + $field => $value, + ], + ]; + + return $this; + } + /** * Add a whereRegexp condition. * diff --git a/tests/Builders/FilterBuilderTest.php b/tests/Builders/FilterBuilderTest.php index 6feb6cf..515348a 100644 --- a/tests/Builders/FilterBuilderTest.php +++ b/tests/Builders/FilterBuilderTest.php @@ -236,6 +236,42 @@ public function testWhereNotExists() ); } + public function testWhereMatch() + { + $builder = (new FilterBuilder($this->mockModel())) + ->whereMatch('tags', 'travel') + ->whereMatch('tags', 'spain'); + + $this->assertEquals( + [ + 'must' => [ + ['match' => ['tags' => 'travel']], + ['match' => ['tags' => 'spain']], + ], + 'must_not' => [], + ], + $builder->wheres + ); + } + + public function testWhereNotMatch() + { + $builder = (new FilterBuilder($this->mockModel())) + ->whereNotMatch('tags', 'travel') + ->whereNotMatch('tags', 'spain'); + + $this->assertEquals( + [ + 'must' => [], + 'must_not' => [ + ['match' => ['tags' => 'travel']], + ['match' => ['tags' => 'spain']], + ], + ], + $builder->wheres + ); + } + public function testWhereRegexp() { $builder = (new FilterBuilder($this->mockModel()))