Skip to content

Commit d2a26a4

Browse files
committed
*add - new traits / parameters for queries (boost, case_insensitive, format, minimum_should_match, rewrite)
*edit - queries - new parameters *edit - tests including new parameters *fix - PrefixQuery has no boost (according to documentation) *fix - QueryStringTest renamed to QueryStringQueryTest, RankFeatureTest renamed to RankFeatureQueryTest *add - missing tests *edit - phpstan - highest level is 9 now *edit - .gitignore - .editorconfig added (but I can recommend to add it to the repository - but with yours settings)
1 parent 74580e1 commit d2a26a4

24 files changed

+454
-52
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor
22
composer.lock
3-
.phpunit.result.cache
3+
.phpunit.result.cache
4+
.editorconfig

phpstan.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ parameters:
1212
- src
1313
- tests
1414

15-
# The level 8 is the highest level
16-
level: 8
15+
# The level 9 is the highest level
16+
level: 9
1717

1818
# it is impossible to map build()
1919
checkMissingIterableValueType: false

src/Features/HasBoost.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Erichard\ElasticQueryBuilder\Features;
6+
7+
trait HasBoost
8+
{
9+
protected ?float $boost = null;
10+
11+
public function setBoost(?float $boost): self
12+
{
13+
$this->boost = $boost;
14+
15+
return $this;
16+
}
17+
18+
public function buildBoostTo(array &$array): self
19+
{
20+
if (null === $this->boost) {
21+
return $this;
22+
}
23+
24+
$array['boost'] = $this->boost;
25+
26+
return $this;
27+
}
28+
29+
public function getBoost(): ?float
30+
{
31+
return $this->boost;
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Erichard\ElasticQueryBuilder\Features;
6+
7+
trait HasCaseInsensitive
8+
{
9+
protected ?bool $caseInsensitive = null;
10+
11+
public function setCaseInsensitive(?bool $caseInsensitive): self
12+
{
13+
$this->caseInsensitive = $caseInsensitive;
14+
15+
return $this;
16+
}
17+
18+
public function buildCaseInsensitiveTo(array &$array): self
19+
{
20+
if (null === $this->caseInsensitive) {
21+
return $this;
22+
}
23+
24+
$array['case_insensitive'] = $this->caseInsensitive;
25+
26+
return $this;
27+
}
28+
29+
public function getCaseInsensitive(): ?bool
30+
{
31+
return $this->caseInsensitive;
32+
}
33+
}

src/Features/HasFormat.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Erichard\ElasticQueryBuilder\Features;
6+
7+
trait HasFormat
8+
{
9+
protected ?string $format = null;
10+
11+
public function setFormat(?string $format): self
12+
{
13+
$this->format = $format;
14+
15+
return $this;
16+
}
17+
18+
public function buildFormatTo(array &$array): self
19+
{
20+
if (null === $this->format) {
21+
return $this;
22+
}
23+
24+
$array['format'] = $this->format;
25+
26+
return $this;
27+
}
28+
29+
public function getFormat(): ?string
30+
{
31+
return $this->format;
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Erichard\ElasticQueryBuilder\Features;
6+
7+
trait HasMinimumShouldMatch
8+
{
9+
protected ?string $minimumShouldMatch = null;
10+
11+
public function setMinimumShouldMatch(?string $minimumShouldMatch): self
12+
{
13+
$this->minimumShouldMatch = $minimumShouldMatch;
14+
15+
return $this;
16+
}
17+
18+
public function buildMinimumShouldMatchTo(array &$array): self
19+
{
20+
if (null === $this->minimumShouldMatch) {
21+
return $this;
22+
}
23+
24+
$array['minimum_should_match'] = $this->minimumShouldMatch;
25+
26+
return $this;
27+
}
28+
29+
public function getMinimumShouldMatch(): ?string
30+
{
31+
return $this->minimumShouldMatch;
32+
}
33+
}

src/Features/HasRewrite.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Erichard\ElasticQueryBuilder\Features;
6+
7+
trait HasRewrite
8+
{
9+
protected ?string $rewrite = null;
10+
11+
public function setRewrite(?string $rewrite): self
12+
{
13+
$this->rewrite = $rewrite;
14+
15+
return $this;
16+
}
17+
18+
public function buildRewriteTo(array &$array): self
19+
{
20+
if (null === $this->rewrite) {
21+
return $this;
22+
}
23+
24+
$array['rewrite'] = $this->rewrite;
25+
26+
return $this;
27+
}
28+
29+
public function getRewrite(): ?string
30+
{
31+
return $this->rewrite;
32+
}
33+
}

src/Query/MatchQuery.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,39 @@
44

55
namespace Erichard\ElasticQueryBuilder\Query;
66

7+
use Erichard\ElasticQueryBuilder\Features\HasMinimumShouldMatch;
8+
use Erichard\ElasticQueryBuilder\Features\HasOperator;
9+
710
class MatchQuery extends AbstractMatchQuery
811
{
12+
use HasOperator;
13+
use HasMinimumShouldMatch;
14+
15+
public function __construct(string $field, string $query, ?string $analyzer = null, ?string $operator = null, ?string $minimumShouldMatch = null)
16+
{
17+
parent::__construct($field, $query, $analyzer);
18+
19+
$this->operator = $operator;
20+
$this->minimumShouldMatch = $minimumShouldMatch;
21+
}
22+
923
public function getQueryName(): string
1024
{
1125
return 'match';
1226
}
27+
28+
public function build(): array
29+
{
30+
$build = parent::build();
31+
32+
if (null !== $this->operator) {
33+
$this->buildOperatorTo($build[$this->getQueryName()][$this->field]);
34+
}
35+
36+
if (null !== $this->minimumShouldMatch) {
37+
$this->buildMinimumShouldMatchTo($build[$this->getQueryName()][$this->field]);
38+
}
39+
40+
return $build;
41+
}
1342
}

src/Query/MultiMatchQuery.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
namespace Erichard\ElasticQueryBuilder\Query;
66

77
use Erichard\ElasticQueryBuilder\Contracts\QueryInterface;
8+
use Erichard\ElasticQueryBuilder\Features\HasBoost;
9+
use Erichard\ElasticQueryBuilder\Features\HasMinimumShouldMatch;
810
use Erichard\ElasticQueryBuilder\Features\HasOperator;
911

1012
class MultiMatchQuery implements QueryInterface
1113
{
1214
use HasOperator;
15+
use HasBoost;
16+
use HasMinimumShouldMatch;
1317

1418
/**
1519
* @param mixed[]|string[] $fields
@@ -19,9 +23,13 @@ public function __construct(
1923
protected string $query,
2024
protected ?string $type = null,
2125
protected ?string $fuzziness = null,
22-
?string $operator = null
26+
?string $operator = null,
27+
?float $boost = null,
28+
?string $minimumShouldMatch = null
2329
) {
2430
$this->operator = $operator;
31+
$this->boost = $boost;
32+
$this->minimumShouldMatch = $minimumShouldMatch;
2533
}
2634

2735
public function setFields(array $fields): self
@@ -68,6 +76,8 @@ public function build(): array
6876
}
6977

7078
$this->buildOperatorTo($data);
79+
$this->buildBoostTo($data);
80+
$this->buildMinimumShouldMatchTo($data);
7181

7282
return [
7383
'multi_match' => $data,

src/Query/PrefixQuery.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,28 @@
55
namespace Erichard\ElasticQueryBuilder\Query;
66

77
use Erichard\ElasticQueryBuilder\Contracts\QueryInterface;
8+
use Erichard\ElasticQueryBuilder\Features\HasCaseInsensitive;
89
use Erichard\ElasticQueryBuilder\Features\HasField;
10+
use Erichard\ElasticQueryBuilder\Features\HasRewrite;
911

1012
/**
1113
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html
1214
*/
1315
class PrefixQuery implements QueryInterface
1416
{
1517
use HasField;
18+
use HasRewrite;
19+
use HasCaseInsensitive;
1620

1721
public function __construct(
1822
string $field,
1923
protected string $value,
20-
protected ?float $boost = null
24+
?string $rewrite = null,
25+
?bool $caseInsensitive = null
2126
) {
2227
$this->field = $field;
28+
$this->rewrite = $rewrite;
29+
$this->caseInsensitive = $caseInsensitive;
2330
}
2431

2532
public function setValue(string $value): self
@@ -29,28 +36,19 @@ public function setValue(string $value): self
2936
return $this;
3037
}
3138

32-
public function setBoost(?float $boost): self
33-
{
34-
$this->boost = $boost;
35-
36-
return $this;
37-
}
38-
3939
public function build(): array
4040
{
41-
$value = $this->value;
41+
$build = [
42+
'value' => $this->value,
43+
];
4244

43-
if (null !== $this->boost) {
44-
$value = [
45-
'value' => $this->value,
46-
'boost' => $this->boost,
47-
];
48-
}
45+
$this->buildRewriteTo($build);
46+
$this->buildCaseInsensitiveTo($build);
4947

5048
return [
5149
'prefix' => [
52-
$this->field => $value,
53-
],
50+
$this->field => $build,
51+
]
5452
];
5553
}
5654
}

0 commit comments

Comments
 (0)