Skip to content

Commit eff61d1

Browse files
committed
#110876
1 parent 7f21112 commit eff61d1

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/Concerns/DecoratesBoolQuery.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ public function whereMatch(string $field, string $query, string|MatchOptions $op
7979
return $this;
8080
}
8181

82+
public function orWhereMatch(string $field, string $query, string|MatchOptions $operator = 'or'): static
83+
{
84+
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
85+
86+
return $this;
87+
}
88+
8289
public function whereMultiMatch(array $fields, string $query, string|MultiMatchOptions|null $type = null): static
8390
{
8491
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
@@ -92,4 +99,16 @@ public function whereWildcard(string $field, string $query, ?WildcardOptions $op
9299

93100
return $this;
94101
}
102+
103+
public function orWhereWildcard(string $field, string $query, ?WildcardOptions $options = null): static
104+
{
105+
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
106+
107+
return $this;
108+
}
109+
110+
public function addMustBool(): BoolQueryBuilder
111+
{
112+
return $this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
113+
}
95114
}

src/Filtering/BoolQueryBuilder.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ class BoolQueryBuilder implements BoolQuery, Criteria
2525
use SupportsPath;
2626

2727
protected CriteriaCollection $must;
28+
protected CriteriaCollection $should;
2829
protected CriteriaCollection $filter;
2930
protected CriteriaCollection $mustNot;
3031

3132
public function __construct(protected string $path = '', protected bool $emptyMatchAll = true)
3233
{
3334
$this->must = new CriteriaCollection();
35+
$this->should = new CriteriaCollection();
3436
$this->filter = new CriteriaCollection();
3537
$this->mustNot = new CriteriaCollection();
3638
}
@@ -65,6 +67,7 @@ public function toDSL(): array
6567

6668
$body = array_merge(
6769
$this->criteriasToDSL('must', $this->must),
70+
$this->criteriasToDSL('should', $this->should),
6871
$this->criteriasToDSL('filter', $this->filter),
6972
$this->criteriasToDSL('must_not', $this->mustNot),
7073
);
@@ -79,7 +82,7 @@ protected function criteriasToDSL(string $key, CriteriaCollection $criterias): a
7982

8083
protected function criteriasCount(): int
8184
{
82-
return $this->must->count() + $this->filter->count() + $this->mustNot->count();
85+
return $this->must->count() + $this->should->count() + $this->filter->count() + $this->mustNot->count();
8386
}
8487

8588
//endregion
@@ -147,12 +150,25 @@ public function whereNotNull(string $field): static
147150

148151
public function whereMatch(string $field, string $query, string|MatchOptions $operator = 'or'): static
149152
{
150-
$options = is_string($operator) ? MatchOptions::make($operator) : $operator;
151-
$this->must->add(new OneMatch($this->absolutePath($field), $query, $options));
153+
$this->must->add($this->makeMatch($field, $query, $operator));
154+
155+
return $this;
156+
}
157+
158+
public function orWhereMatch(string $field, string $query, string|MatchOptions $operator = 'or'): static
159+
{
160+
$this->should->add($this->makeMatch($field, $query, $operator));
152161

153162
return $this;
154163
}
155164

165+
protected function makeMatch(string $field, string $query, string|MatchOptions $operator = 'or'): OneMatch
166+
{
167+
$options = is_string($operator) ? MatchOptions::make($operator) : $operator;
168+
169+
return new OneMatch($this->absolutePath($field), $query, $options);
170+
}
171+
156172
public function whereMultiMatch(array $fields, string $query, string|MultiMatchOptions|null $type = null): static
157173
{
158174
$options = is_string($type) ? MultiMatchOptions::make($type) : $type;
@@ -169,11 +185,31 @@ public function whereMultiMatch(array $fields, string $query, string|MultiMatchO
169185

170186
public function whereWildcard(string $field, string $query, ?WildcardOptions $options = null): static
171187
{
172-
$this->must->add(new Wildcard($this->absolutePath($field), $query, $options ?: new WildcardOptions()));
188+
$this->must->add($this->makeWildcard($field, $query, $options));
173189

174190
return $this;
175191
}
176192

193+
public function orWhereWildcard(string $field, string $query, ?WildcardOptions $options = null): static
194+
{
195+
$this->should->add($this->makeWildcard($field, $query, $options));
196+
197+
return $this;
198+
}
199+
200+
public function makeWildcard(string $field, string $query, ?WildcardOptions $options = null): Wildcard
201+
{
202+
return new Wildcard($this->absolutePath($field), $query, $options ?: new WildcardOptions());
203+
}
204+
205+
public function addMustBool(): static
206+
{
207+
$boolCriteria = static::make();
208+
$this->must->add($boolCriteria);
209+
210+
return $boolCriteria;
211+
}
212+
177213
protected function addNestedCriteria(string $nested, Closure $filter, CriteriaCollection $target): static
178214
{
179215
$path = $this->absolutePath($nested);

0 commit comments

Comments
 (0)