Skip to content

Commit 29be3ab

Browse files
committed
whereMatch operator.
1 parent 7ddff7b commit 29be3ab

File tree

5 files changed

+17
-6
lines changed

5 files changed

+17
-6
lines changed

src/Concerns/DecoratesBoolQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function whereNotNull(string $field): static
6969
return $this;
7070
}
7171

72-
public function whereMatch(string $field, string $query): static
72+
public function whereMatch(string $field, string $query, string $operator = 'or'): static
7373
{
7474
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
7575

src/Contracts/BoolQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function whereNull(string $field): static;
2323

2424
public function whereNotNull(string $field): static;
2525

26-
public function whereMatch(string $field, string $query): static;
26+
public function whereMatch(string $field, string $query, string $operator = 'or'): static;
2727

2828
public function whereMultiMatch(array $fields, string $query, ?string $type = null): static;
2929
}

src/Filtering/BoolQueryBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ public function whereNotNull(string $field): static
141141
return $this;
142142
}
143143

144-
public function whereMatch(string $field, string $query): static
144+
public function whereMatch(string $field, string $query, string $operator = 'or'): static
145145
{
146-
$this->must->add(new OneMatch($this->absolutePath($field), $query));
146+
$this->must->add(new OneMatch($this->absolutePath($field), $query, $operator));
147147

148148
return $this;
149149
}

src/Filtering/Criterias/OneMatch.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77

88
class OneMatch implements Criteria
99
{
10-
public function __construct(private string $field, private string $query)
10+
public function __construct(private string $field, private string $query, private string $operator)
1111
{
1212
Assert::stringNotEmpty(trim($field));
13+
Assert::oneOf($operator, ['and', 'or']);
1314
}
1415

1516
public function toDSL(): array
1617
{
17-
$body = ['query' => $this->query];
18+
$body = [
19+
'query' => $this->query,
20+
'operator' => $this->operator,
21+
];
1822

1923
return [
2024
'match' => [

tests/Functional/Search/FilteringTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ public function testWhereMatch(): void
6161
$this->assertDocumentIds([319, 471]);
6262
}
6363

64+
public function whereMatchOperatorAnd(): void
65+
{
66+
$this->testing->whereMatch('search_name', 'leather gloves', 'and');
67+
68+
$this->assertDocumentIds([319]);
69+
}
70+
6471
public function testWhereMultiMatch(): void
6572
{
6673
$this->testing->whereMultiMatch(['search_name', 'description'], 'nice gloves');

0 commit comments

Comments
 (0)