Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

Commit

Permalink
Added whereMatch and whereNotMatch (#355)
Browse files Browse the repository at this point in the history
* Updated @pk1z pull request #154 to remove conflicts and added whereNotMatch filter and test

* Fixed CI style issues
  • Loading branch information
hoersten committed Apr 1, 2020
1 parent 6844679 commit cdd6a4b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions src/Builders/FilterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/Builders/FilterBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down

0 comments on commit cdd6a4b

Please sign in to comment.