Skip to content

Commit 38eb119

Browse files
authored
Merge pull request #14 from Spameri/v2
fix(query): allow string type for minimumShouldMatch parameter
2 parents 5bcdc83 + be95306 commit 38eb119

File tree

8 files changed

+109
-8
lines changed

8 files changed

+109
-8
lines changed

src/Query/ElasticMatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct(
1616
private bool|int|string|null $query,
1717
private float $boost = 1.0,
1818
private \Spameri\ElasticQuery\Query\Match\Fuzziness|null $fuzziness = null,
19-
private int|null $minimumShouldMatch = null,
19+
private int|string|null $minimumShouldMatch = null,
2020
private string $operator = \Spameri\ElasticQuery\Query\Match\Operator::OR,
2121
private string|null $analyzer = null,
2222
)

src/Query/MultiMatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct(
1717
private float $boost = 1.0,
1818
private \Spameri\ElasticQuery\Query\Match\Fuzziness|null $fuzziness = null,
1919
private string $type = \Spameri\ElasticQuery\Query\Match\MultiMatchType::BEST_FIELDS,
20-
private int|null $minimumShouldMatch = null,
20+
private int|string|null $minimumShouldMatch = null,
2121
private string $operator = \Spameri\ElasticQuery\Query\Match\Operator::OR,
2222
private string|null $analyzer = null,
2323
)

src/Query/Nested.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function toArray(): array
3434
{
3535
$queryArray = $this->query->toArray();
3636

37-
if (count($queryArray) === 0) {
37+
if (\count($queryArray) === 0) {
3838
$queryArray = [
3939
'bool' => [],
4040
];

tests/SpameriTests/ElasticQuery/Aggregation/AggregationCollection.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class AggregationCollection extends \Tester\TestCase
149149
null,
150150
null,
151151
null,
152+
null,
152153
$collection,
153154
);
154155

tests/SpameriTests/ElasticQuery/ElasticQuery.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class ElasticQuery extends \Tester\TestCase
7979
$term
8080
)
8181
),
82+
NULL,
8283
new \Spameri\ElasticQuery\Options\SortCollection(
8384
new \Spameri\ElasticQuery\Options\Sort(
8485
'year',

tests/SpameriTests/ElasticQuery/Query/ElasticMatch.phpt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,54 @@ class ElasticMatch extends \Tester\TestCase
8383
}
8484

8585

86+
public function testMinimumShouldMatchString() : void
87+
{
88+
$match = new \Spameri\ElasticQuery\Query\ElasticMatch(
89+
'name',
90+
'Avengers Endgame',
91+
1.0,
92+
null,
93+
'75%',
94+
);
95+
96+
$array = $match->toArray();
97+
98+
\Tester\Assert::same('75%', $array['match']['name']['minimum_should_match']);
99+
}
100+
101+
102+
public function testMinimumShouldMatchCombinationString() : void
103+
{
104+
$match = new \Spameri\ElasticQuery\Query\ElasticMatch(
105+
'name',
106+
'Avengers Endgame Infinity War',
107+
1.0,
108+
null,
109+
'2<90%',
110+
);
111+
112+
$array = $match->toArray();
113+
114+
\Tester\Assert::same('2<90%', $array['match']['name']['minimum_should_match']);
115+
}
116+
117+
118+
public function testMinimumShouldMatchInt() : void
119+
{
120+
$match = new \Spameri\ElasticQuery\Query\ElasticMatch(
121+
'name',
122+
'Avengers Endgame',
123+
1.0,
124+
null,
125+
2,
126+
);
127+
128+
$array = $match->toArray();
129+
130+
\Tester\Assert::same(2, $array['match']['name']['minimum_should_match']);
131+
}
132+
133+
86134
public function tearDown() : void
87135
{
88136
$ch = \curl_init();

tests/SpameriTests/ElasticQuery/Query/MultiMatch.phpt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,57 @@ class MultiMatch extends \Tester\TestCase
154154
}
155155

156156

157+
public function testMinimumShouldMatchString(): void
158+
{
159+
$multiMatch = new \Spameri\ElasticQuery\Query\MultiMatch(
160+
['title', 'description'],
161+
'search term',
162+
1.0,
163+
null,
164+
\Spameri\ElasticQuery\Query\Match\MultiMatchType::BEST_FIELDS,
165+
'75%',
166+
);
167+
168+
$array = $multiMatch->toArray();
169+
170+
\Tester\Assert::same('75%', $array['multi_match']['minimum_should_match']);
171+
}
172+
173+
174+
public function testMinimumShouldMatchCombinationString(): void
175+
{
176+
$multiMatch = new \Spameri\ElasticQuery\Query\MultiMatch(
177+
['title', 'description'],
178+
'search term query',
179+
1.0,
180+
null,
181+
\Spameri\ElasticQuery\Query\Match\MultiMatchType::BEST_FIELDS,
182+
'2<90%',
183+
);
184+
185+
$array = $multiMatch->toArray();
186+
187+
\Tester\Assert::same('2<90%', $array['multi_match']['minimum_should_match']);
188+
}
189+
190+
191+
public function testMinimumShouldMatchInt(): void
192+
{
193+
$multiMatch = new \Spameri\ElasticQuery\Query\MultiMatch(
194+
['title', 'description'],
195+
'search term',
196+
1.0,
197+
null,
198+
\Spameri\ElasticQuery\Query\Match\MultiMatchType::BEST_FIELDS,
199+
2,
200+
);
201+
202+
$array = $multiMatch->toArray();
203+
204+
\Tester\Assert::same(2, $array['multi_match']['minimum_should_match']);
205+
}
206+
207+
157208
public function testCreate(): void
158209
{
159210
$multiMatch = new \Spameri\ElasticQuery\Query\MultiMatch(

tests/SpameriTests/ElasticQuery/Query/Nested.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Nested extends \Tester\TestCase
3131

3232
\Tester\Assert::true(isset($array['nested']));
3333
\Tester\Assert::same('comments', $array['nested']['path']);
34-
\Tester\Assert::true(isset($array['nested']['query']['bool']));
34+
\Tester\Assert::true(isset($array['nested']['query'][0]['bool']));
3535
}
3636

3737

@@ -47,8 +47,8 @@ class Nested extends \Tester\TestCase
4747
$array = $nested->toArray();
4848

4949
\Tester\Assert::same('comments', $array['nested']['path']);
50-
\Tester\Assert::true(isset($array['nested']['query']['bool']['bool']['must']));
51-
\Tester\Assert::count(1, $array['nested']['query']['bool']['bool']['must']);
50+
\Tester\Assert::true(isset($array['nested']['query'][0]['bool']['must']));
51+
\Tester\Assert::count(1, $array['nested']['query'][0]['bool']['must']);
5252
}
5353

5454

@@ -74,8 +74,8 @@ class Nested extends \Tester\TestCase
7474

7575
$array = $nested->toArray();
7676

77-
\Tester\Assert::true(isset($array['nested']['query']['bool']['bool']['must']));
78-
\Tester\Assert::true(isset($array['nested']['query']['bool']['bool']['should']));
77+
\Tester\Assert::true(isset($array['nested']['query'][0]['bool']['must']));
78+
\Tester\Assert::true(isset($array['nested']['query'][0]['bool']['should']));
7979
}
8080

8181

0 commit comments

Comments
 (0)