Skip to content

Commit bb13a45

Browse files
authored
Merge pull request #32 from ensi-platform/v8-task-110876
#110876 v8
2 parents fb63ee4 + 6588633 commit bb13a45

File tree

5 files changed

+133
-10
lines changed

5 files changed

+133
-10
lines changed

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@
4242
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html coverage"
4343
},
4444
"config": {
45-
"sort-packages": true,
46-
"allow-plugins": {
47-
"php-http/discovery": true
48-
}
45+
"sort-packages": true
4946
},
5047
"extra": {
5148
"laravel": {

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(callable $fn): static
111+
{
112+
return $this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
113+
}
95114
}

src/Contracts/BoolQuery.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,13 @@ public function whereNotNull(string $field): static;
2525

2626
public function whereMatch(string $field, string $query, string|MatchOptions $operator = 'or'): static;
2727

28+
public function orWhereMatch(string $field, string $query, string|MatchOptions $operator = 'or'): static;
29+
2830
public function whereMultiMatch(array $fields, string $query, string|MultiMatchOptions|null $type = null): static;
31+
32+
public function whereWildcard(string $field, string $query, ?WildcardOptions $options = null): static;
33+
34+
public function orWhereWildcard(string $field, string $query, ?WildcardOptions $options = null): static;
35+
36+
public function addMustBool(callable $fn): static;
2937
}

src/Filtering/BoolQueryBuilder.php

Lines changed: 41 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,7 +185,28 @@ 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));
189+
190+
return $this;
191+
}
192+
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(callable $fn): static
206+
{
207+
$boolCriteria = static::make();
208+
$fn($boolCriteria);
209+
$this->must->add($boolCriteria);
173210

174211
return $this;
175212
}

tests/Unit/Filtering/BoolQueryTest.php

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function testMatch(string|MatchOptions $options, array $expected): void
141141
{
142142
$dsl = BoolQueryBuilder::make()->whereMatch('name', 'foo', $options)->toDSL();
143143

144-
$this->assertArrayFragment(array_merge(['query' => 'foo'], $expected), $dsl);
144+
$this->assertArrayFragment(['must' => [["match" => ['name' => array_merge(['query' => 'foo'], $expected)]]]], $dsl);
145145
}
146146

147147
public function provideMatch(): array
@@ -160,6 +160,32 @@ public function provideMatch(): array
160160
];
161161
}
162162

163+
/**
164+
* @dataProvider provideOrMatch
165+
*/
166+
public function testOrMatch(string|MatchOptions $options, array $expected): void
167+
{
168+
$dsl = BoolQueryBuilder::make()->orWhereMatch('name', 'foo', $options)->toDSL();
169+
170+
$this->assertArrayFragment(['should' => [["match" => ['name' => array_merge(['query' => 'foo'], $expected)]]]], $dsl);
171+
}
172+
173+
public function provideOrMatch(): array
174+
{
175+
return [
176+
'operator' => ['and', ['operator' => 'and']],
177+
'fuzziness' => [MatchOptions::make(fuzziness: 'AUTO'), ['fuzziness' => 'AUTO']],
178+
'minimum_should_match' => [
179+
MatchOptions::make(minimumShouldMatch: '50%'),
180+
['minimum_should_match' => '50%'],
181+
],
182+
'many options' => [
183+
MatchOptions::make(operator: 'or', fuzziness: '2', minimumShouldMatch: '30%'),
184+
['minimum_should_match' => '30%', 'fuzziness' => '2', 'operator' => 'or'],
185+
],
186+
];
187+
}
188+
163189
/**
164190
* @dataProvider provideMultiMatch
165191
*/
@@ -190,7 +216,7 @@ public function testWildcard(?WildcardOptions $options, array $expected): void
190216
{
191217
$dsl = BoolQueryBuilder::make()->whereWildcard('foo', '%value%', $options)->toDSL();
192218

193-
$this->assertArrayFragment(['wildcard' => ['foo' => array_merge(['value' => '%value%'], $expected)]], $dsl);
219+
$this->assertArrayFragment(['must' => [['wildcard' => ['foo' => array_merge(['value' => '%value%'], $expected)]]]], $dsl);
194220
}
195221

196222
public function provideWildcard(): array
@@ -201,4 +227,40 @@ public function provideWildcard(): array
201227
'rewrite options' => [WildcardOptions::make(rewrite: 'constant_score'), ['rewrite' => 'constant_score']],
202228
];
203229
}
230+
231+
/**
232+
* @dataProvider provideOrWildcard
233+
*/
234+
public function testOrWildcard(?WildcardOptions $options, array $expected): void
235+
{
236+
$dsl = BoolQueryBuilder::make()->orWhereWildcard('foo', '%value%', $options)->toDSL();
237+
238+
$this->assertArrayFragment(['should' => [['wildcard' => ['foo' => array_merge(['value' => '%value%'], $expected)]]]], $dsl);
239+
}
240+
241+
public function provideOrWildcard(): array
242+
{
243+
return [
244+
'empty options' => [WildcardOptions::make(0, false), ['boost' => 0, 'case_insensitive' => false]],
245+
'full options' => [WildcardOptions::make(0.5, true), ['boost' => 0.5, 'case_insensitive' => true]],
246+
'rewrite options' => [WildcardOptions::make(rewrite: 'constant_score'), ['rewrite' => 'constant_score']],
247+
];
248+
}
249+
250+
public function testAddMustBool(): void
251+
{
252+
$builder = BoolQueryBuilder::make();
253+
$builder->where('mustName', 'value');
254+
$builder->addMustBool(fn (BoolQueryBuilder $builder) => $builder->orWhereWildcard('wildcardName', 'wildcardValue')->orWhereMatch('matchName', 'matchValue'));
255+
256+
$dsl = $builder->toDSL();
257+
258+
$this->assertArrayFragment([
259+
'filter' => [["term" => ['mustName' => 'value']]],
260+
'must' => [["bool" => ['should' => [
261+
['wildcard' => ['wildcardName' => ['value' => 'wildcardValue']]],
262+
["match" => ['matchName' => ['query' => 'matchValue', 'operator' => 'or']]],
263+
]]]],
264+
], $dsl);
265+
}
204266
}

0 commit comments

Comments
 (0)