Skip to content

Commit cdd6a4b

Browse files
authored
Added whereMatch and whereNotMatch (babenkoivan#355)
* Updated @pk1z pull request babenkoivan#154 to remove conflicts and added whereNotMatch filter and test * Fixed CI style issues
1 parent 6844679 commit cdd6a4b

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,8 @@ whereBetween($field, $value) | whereBetween('price', [100, 200]) | Checks if a v
444444
whereNotBetween($field, $value) | whereNotBetween('price', [100, 200]) | Checks if a value isn't in a range.
445445
whereExists($field) | whereExists('unemployed') | Checks if a value is defined.
446446
whereNotExists($field) | whereNotExists('unemployed') | Checks if a value isn't defined.
447+
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.
448+
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.
447449
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.
448450
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.
449451
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.

src/Builders/FilterBuilder.php

+36
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,42 @@ public function whereNotExists($field)
282282
return $this;
283283
}
284284

285+
/**
286+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html Match query
287+
*
288+
* @param string $field
289+
* @param string $value
290+
* @return $this
291+
*/
292+
public function whereMatch($field, $value)
293+
{
294+
$this->wheres['must'][] = [
295+
'match' => [
296+
$field => $value,
297+
],
298+
];
299+
300+
return $this;
301+
}
302+
303+
/**
304+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html Match query
305+
*
306+
* @param string $field
307+
* @param string $value
308+
* @return $this
309+
*/
310+
public function whereNotMatch($field, $value)
311+
{
312+
$this->wheres['must_not'][] = [
313+
'match' => [
314+
$field => $value,
315+
],
316+
];
317+
318+
return $this;
319+
}
320+
285321
/**
286322
* Add a whereRegexp condition.
287323
*

tests/Builders/FilterBuilderTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,42 @@ public function testWhereNotExists()
236236
);
237237
}
238238

239+
public function testWhereMatch()
240+
{
241+
$builder = (new FilterBuilder($this->mockModel()))
242+
->whereMatch('tags', 'travel')
243+
->whereMatch('tags', 'spain');
244+
245+
$this->assertEquals(
246+
[
247+
'must' => [
248+
['match' => ['tags' => 'travel']],
249+
['match' => ['tags' => 'spain']],
250+
],
251+
'must_not' => [],
252+
],
253+
$builder->wheres
254+
);
255+
}
256+
257+
public function testWhereNotMatch()
258+
{
259+
$builder = (new FilterBuilder($this->mockModel()))
260+
->whereNotMatch('tags', 'travel')
261+
->whereNotMatch('tags', 'spain');
262+
263+
$this->assertEquals(
264+
[
265+
'must' => [],
266+
'must_not' => [
267+
['match' => ['tags' => 'travel']],
268+
['match' => ['tags' => 'spain']],
269+
],
270+
],
271+
$builder->wheres
272+
);
273+
}
274+
239275
public function testWhereRegexp()
240276
{
241277
$builder = (new FilterBuilder($this->mockModel()))

0 commit comments

Comments
 (0)