Skip to content

Commit 7972f85

Browse files
authored
Merge pull request #47 from ensi-platform/v8-ecs-521
V8 add orWhereMultiMatch
2 parents bc03853 + e25c475 commit 7972f85

File tree

7 files changed

+66
-18
lines changed

7 files changed

+66
-18
lines changed

composer.json

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

src/Concerns/DecoratesBoolQuery.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ public function whereMultiMatch(array $fields, string $query, string|MultiMatchO
9393
return $this;
9494
}
9595

96+
public function orWhereMultiMatch(array $fields, string $query, string|MultiMatchOptions|null $type = null): static
97+
{
98+
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
99+
100+
return $this;
101+
}
102+
96103
public function whereWildcard(string $field, string $query, ?WildcardOptions $options = null): static
97104
{
98105
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());

src/Contracts/BoolQuery.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function orWhereMatch(string $field, string $query, string|MatchOptions $
2929

3030
public function whereMultiMatch(array $fields, string $query, string|MultiMatchOptions|null $type = null): static;
3131

32+
public function orWhereMultiMatch(array $fields, string $query, string|MultiMatchOptions|null $type = null): static;
33+
3234
public function whereWildcard(string $field, string $query, ?WildcardOptions $options = null): static;
3335

3436
public function orWhereWildcard(string $field, string $query, ?WildcardOptions $options = null): static;

src/Filtering/BoolQueryBuilder.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,20 @@ protected function makeMatch(string $field, string $query, string|MatchOptions $
170170
}
171171

172172
public function whereMultiMatch(array $fields, string $query, string|MultiMatchOptions|null $type = null): static
173+
{
174+
$this->must->add($this->makeMultiMatch($fields, $query, $type));
175+
176+
return $this;
177+
}
178+
179+
public function orWhereMultiMatch(array $fields, string $query, string|MultiMatchOptions|null $type = null): static
180+
{
181+
$this->should->add($this->makeMultiMatch($fields, $query, $type));
182+
183+
return $this;
184+
}
185+
186+
protected function makeMultiMatch(array $fields, string $query, string|MultiMatchOptions|null $type = null): MultiMatch
173187
{
174188
$options = is_string($type) ? MultiMatchOptions::make($type) : $type;
175189

@@ -178,9 +192,7 @@ public function whereMultiMatch(array $fields, string $query, string|MultiMatchO
178192
$fields
179193
);
180194

181-
$this->must->add(new MultiMatch($fields, $query, $options ?? new MultiMatchOptions()));
182-
183-
return $this;
195+
return new MultiMatch($fields, $query, $options ?? new MultiMatchOptions());
184196
}
185197

186198
public function whereWildcard(string $field, string $query, ?WildcardOptions $options = null): static
@@ -197,7 +209,7 @@ public function orWhereWildcard(string $field, string $query, ?WildcardOptions $
197209
return $this;
198210
}
199211

200-
public function makeWildcard(string $field, string $query, ?WildcardOptions $options = null): Wildcard
212+
protected function makeWildcard(string $field, string $query, ?WildcardOptions $options = null): Wildcard
201213
{
202214
return new Wildcard($this->absolutePath($field), $query, $options ?: new WildcardOptions());
203215
}

tests/Unit/Aggregating/AggregationCollectionTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Ensi\LaravelElasticQuery\Tests\Unit\UnitTestCase;
99
use InvalidArgumentException;
1010
use Mockery;
11+
use Mockery\MockInterface;
1112

1213
class AggregationCollectionTest extends UnitTestCase
1314
{
@@ -55,13 +56,13 @@ public function testGenerateUniqueName(AggregationCollection $target, string $na
5556
$this->assertEquals($expected, $target->generateUniqueName($name));
5657
}
5758

58-
public function provideGenerateUniqueName(): array
59+
public static function provideGenerateUniqueName(): array
5960
{
6061
return [
6162
'no items no name' => [new AggregationCollection(), ' ', 'agg_1'],
6263
'no items with name' => [new AggregationCollection(), 'agg_2', 'agg_2_1'],
6364
'has items' => [
64-
AggregationCollection::fromAggregation($this->mockAggregation('agg_1')),
65+
AggregationCollection::fromAggregation(static::mockAggregation('agg_1')),
6566
'',
6667
'agg_2',
6768
],
@@ -96,7 +97,7 @@ public function testParseResults(): void
9697
$this->assertEquals(['foo' => 'result', 'bar' => [20]], $testing->parseResults([])->all());
9798
}
9899

99-
private function mockAggregation(string $name, ?array $dsl = null, ?array $results = null): Aggregation
100+
private static function mockAggregation(string $name, ?array $dsl = null, ?array $results = null): Aggregation|MockInterface
100101
{
101102
$agg = Mockery::mock(Aggregation::class);
102103
$agg->allows('name')->andReturn($name);

tests/Unit/ElasticClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testResolveBasicAuthData(array $config, array $expected): void
1414
$this->assertEquals($expected, ElasticClient::resolveBasicAuthData($config));
1515
}
1616

17-
public function provideBasicAuthData(): array
17+
public static function provideBasicAuthData(): array
1818
{
1919
return [
2020
'separate username and password' => [

tests/Unit/Filtering/BoolQueryTest.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testFilter(BoolQueryBuilder $query, array $expected): void
2424
$this->assertArrayStructure(['bool' => ['filter' => [$expected]]], $query->toDSL());
2525
}
2626

27-
public function provideFilter(): array
27+
public static function provideFilter(): array
2828
{
2929
return [
3030
'term' => [
@@ -53,7 +53,7 @@ public function testMustNot(BoolQueryBuilder $query, array $expected): void
5353
$this->assertArrayStructure(['bool' => ['must_not' => [$expected]]], $query->toDSL());
5454
}
5555

56-
public function provideMustNot(): array
56+
public static function provideMustNot(): array
5757
{
5858
return [
5959
'term' => [BoolQueryBuilder::make()->whereNot('name', 'Product'), ['term']],
@@ -76,7 +76,7 @@ public function testEmptyMatchAll(BoolQueryBuilder $query): void
7676
$this->assertArrayFragment(['match_all' => new stdClass()], $query->toDSL());
7777
}
7878

79-
public function provideEmpty(): array
79+
public static function provideEmpty(): array
8080
{
8181
return [
8282
'empty' => [BoolQueryBuilder::make()],
@@ -122,7 +122,7 @@ public function testWhereOperators(string $operator, array $expected): void
122122
$this->assertArrayFragment($expected, $dsl);
123123
}
124124

125-
public function provideWhereOperators(): array
125+
public static function provideWhereOperators(): array
126126
{
127127
return [
128128
'=' => ['=', ['term' => ['rating' => 5]]],
@@ -144,7 +144,7 @@ public function testMatch(string|MatchOptions $options, array $expected): void
144144
$this->assertArrayFragment(['must' => [["match" => ['name' => array_merge(['query' => 'foo'], $expected)]]]], $dsl);
145145
}
146146

147-
public function provideMatch(): array
147+
public static function provideMatch(): array
148148
{
149149
return [
150150
'operator' => ['and', ['operator' => 'and']],
@@ -170,7 +170,7 @@ public function testOrMatch(string|MatchOptions $options, array $expected): void
170170
$this->assertArrayFragment(['should' => [["match" => ['name' => array_merge(['query' => 'foo'], $expected)]]]], $dsl);
171171
}
172172

173-
public function provideOrMatch(): array
173+
public static function provideOrMatch(): array
174174
{
175175
return [
176176
'operator' => ['and', ['operator' => 'and']],
@@ -196,7 +196,30 @@ public function testMultiMatch(string|MultiMatchOptions|null $options, array $ex
196196
$this->assertArrayFragment(array_merge(['query' => 'baz', 'fields' => ['foo', 'bar']], $expected), $dsl);
197197
}
198198

199-
public function provideMultiMatch(): array
199+
public static function provideMultiMatch(): array
200+
{
201+
return [
202+
'type as string' => [MatchType::CROSS_FIELDS, ['type' => MatchType::CROSS_FIELDS]],
203+
'type in options' => [MultiMatchOptions::make(MatchType::PHRASE), ['type' => MatchType::PHRASE]],
204+
'fuzziness' => [MultiMatchOptions::make(fuzziness: 'AUTO'), ['fuzziness' => 'AUTO']],
205+
'multiple options' => [
206+
MultiMatchOptions::make(type: MatchType::MOST_FIELDS, fuzziness: '3', minimumShouldMatch: '30%'),
207+
['minimum_should_match' => '30%', 'fuzziness' => '3', 'type' => MatchType::MOST_FIELDS],
208+
],
209+
];
210+
}
211+
212+
/**
213+
* @dataProvider provideOrMultiMatch
214+
*/
215+
public function testOrMultiMatch(string|MultiMatchOptions|null $options, array $expected): void
216+
{
217+
$dsl = BoolQueryBuilder::make()->OrWhereMultiMatch(['foo', 'bar'], 'baz', $options)->toDSL();
218+
219+
$this->assertArrayFragment(array_merge(['query' => 'baz', 'fields' => ['foo', 'bar']], $expected), $dsl);
220+
}
221+
222+
public static function provideOrMultiMatch(): array
200223
{
201224
return [
202225
'type as string' => [MatchType::CROSS_FIELDS, ['type' => MatchType::CROSS_FIELDS]],
@@ -219,7 +242,7 @@ public function testWildcard(?WildcardOptions $options, array $expected): void
219242
$this->assertArrayFragment(['must' => [['wildcard' => ['foo' => array_merge(['value' => '%value%'], $expected)]]]], $dsl);
220243
}
221244

222-
public function provideWildcard(): array
245+
public static function provideWildcard(): array
223246
{
224247
return [
225248
'empty options' => [WildcardOptions::make(0, false), ['boost' => 0, 'case_insensitive' => false]],
@@ -238,7 +261,7 @@ public function testOrWildcard(?WildcardOptions $options, array $expected): void
238261
$this->assertArrayFragment(['should' => [['wildcard' => ['foo' => array_merge(['value' => '%value%'], $expected)]]]], $dsl);
239262
}
240263

241-
public function provideOrWildcard(): array
264+
public static function provideOrWildcard(): array
242265
{
243266
return [
244267
'empty options' => [WildcardOptions::make(0, false), ['boost' => 0, 'case_insensitive' => false]],

0 commit comments

Comments
 (0)