Skip to content

Commit c28157f

Browse files
committed
clean up Query from mandatory parameters, add HasFuzziness trait
1 parent 38b5c72 commit c28157f

File tree

5 files changed

+76
-120
lines changed

5 files changed

+76
-120
lines changed

src/Features/HasFuzziness.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 HasFuzziness
8+
{
9+
protected ?string $fuzziness = null;
10+
11+
public function setFuzziness(?string $fuzziness): self
12+
{
13+
$this->fuzziness = $fuzziness;
14+
15+
return $this;
16+
}
17+
18+
public function buildFuzzinessTo(array &$array): self
19+
{
20+
if (null === $this->fuzziness) {
21+
return $this;
22+
}
23+
24+
$array['fuzziness'] = $this->fuzziness;
25+
26+
return $this;
27+
}
28+
29+
public function getFuzziness(): ?string
30+
{
31+
return $this->fuzziness;
32+
}
33+
}

src/Query/MatchQuery.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,30 @@
44

55
namespace Erichard\ElasticQueryBuilder\Query;
66

7+
use Erichard\ElasticQueryBuilder\Features\HasFuzziness;
78
use Erichard\ElasticQueryBuilder\Features\HasMinimumShouldMatch;
89
use Erichard\ElasticQueryBuilder\Features\HasOperator;
910

1011
class MatchQuery extends AbstractMatchQuery
1112
{
1213
use HasOperator;
1314
use HasMinimumShouldMatch;
15+
use HasFuzziness;
1416

1517
public function __construct(
1618
string $field,
1719
string $query,
1820
?string $analyzer = null,
1921
?string $operator = null,
2022
?string $minimumShouldMatch = null,
23+
?string $fuzziness = null,
2124
array $params = [],
2225
) {
2326
parent::__construct($field, $query, $analyzer, $params);
2427

2528
$this->operator = $operator;
2629
$this->minimumShouldMatch = $minimumShouldMatch;
30+
$this->fuzziness = $fuzziness;
2731
}
2832

2933
public function getQueryName(): string
@@ -44,6 +48,7 @@ public function build(): array
4448

4549
$this->buildOperatorTo($build[$this->getQueryName()][$this->field]);
4650
$this->buildMinimumShouldMatchTo($build[$this->getQueryName()][$this->field]);
51+
$this->buildFuzzinessTo($build[$this->getQueryName()][$this->field]);
4752

4853
return $build;
4954
}

src/Query/MultiMatchQuery.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Erichard\ElasticQueryBuilder\Contracts\QueryInterface;
88
use Erichard\ElasticQueryBuilder\Features\HasBoost;
9+
use Erichard\ElasticQueryBuilder\Features\HasFuzziness;
910
use Erichard\ElasticQueryBuilder\Features\HasMinimumShouldMatch;
1011
use Erichard\ElasticQueryBuilder\Features\HasOperator;
1112

@@ -14,6 +15,7 @@ class MultiMatchQuery implements QueryInterface
1415
use HasOperator;
1516
use HasBoost;
1617
use HasMinimumShouldMatch;
18+
use HasFuzziness;
1719

1820
/**
1921
* @param mixed[]|string[] $fields
@@ -22,7 +24,7 @@ public function __construct(
2224
protected array $fields,
2325
protected string $query,
2426
protected ?string $type = null,
25-
protected ?string $fuzziness = null,
27+
?string $fuzziness = null,
2628
?string $operator = null,
2729
?float $boost = null,
2830
?string $minimumShouldMatch = null,
@@ -31,6 +33,7 @@ public function __construct(
3133
$this->operator = $operator;
3234
$this->boost = $boost;
3335
$this->minimumShouldMatch = $minimumShouldMatch;
36+
$this->fuzziness = $fuzziness;
3437
}
3538

3639
public function setFields(array $fields): self
@@ -54,13 +57,6 @@ public function setType(string $type): self
5457
return $this;
5558
}
5659

57-
public function setFuzziness(string $fuzziness): self
58-
{
59-
$this->fuzziness = $fuzziness;
60-
61-
return $this;
62-
}
63-
6460
public function setParams(array $params): self
6561
{
6662
$this->params = $params;
@@ -79,13 +75,10 @@ public function build(): array
7975
$data['type'] = $this->type;
8076
}
8177

82-
if (null !== $this->fuzziness) {
83-
$data['fuzziness'] = $this->fuzziness;
84-
}
85-
8678
$this->buildOperatorTo($data);
8779
$this->buildBoostTo($data);
8880
$this->buildMinimumShouldMatchTo($data);
81+
$this->buildFuzzinessTo($data);
8982

9083
$build = $this->params;
9184
$build['multi_match'] = $data;

src/Query/Query.php

Lines changed: 28 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,19 @@ class Query
1111
/**
1212
* @param array<int, string|int|float|bool> $values
1313
*/
14-
public static function terms(string $field, array $values, array $params = []): TermsQuery
14+
public static function terms(string $field, array $values): TermsQuery
1515
{
16-
return new TermsQuery(
17-
field: $field,
18-
values: $values,
19-
params: $params,
20-
);
16+
return new TermsQuery($field, $values);
2117
}
2218

23-
public static function term(string $field, string|int|float|bool $value, array $params = []): TermQuery
19+
public static function term(string $field, string|int|float|bool $value): TermQuery
2420
{
25-
return new TermQuery(
26-
field: $field,
27-
value: $value,
28-
params: $params,
29-
);
21+
return new TermQuery($field, $value);
3022
}
3123

32-
public static function wildcard(string $field, string $value, array $params = []): WildcardQuery
24+
public static function wildcard(string $field, string $value): WildcardQuery
3325
{
34-
return new WildcardQuery(
35-
field: $field,
36-
value: $value,
37-
params: $params,
38-
);
26+
return new WildcardQuery($field, $value);
3927
}
4028

4129
/**
@@ -49,15 +37,8 @@ public static function bool(
4937
array $mustNot = [],
5038
array $should = [],
5139
array $filter = [],
52-
array $params = [],
5340
): BoolQuery {
54-
return new BoolQuery(
55-
must: $must,
56-
mustNot: $mustNot,
57-
should: $should,
58-
filter: $filter,
59-
params: $params,
60-
);
41+
return new BoolQuery($must, $mustNot, $should, $filter);
6142
}
6243

6344
public static function range(
@@ -66,112 +47,63 @@ public static function range(
6647
int|float|string|null $gt = null,
6748
int|float|string|null $lte = null,
6849
int|float|string|null $gte = null,
69-
array $params = [],
7050
): RangeQuery {
71-
return new RangeQuery(
72-
field: $field,
73-
lt: $lt,
74-
gt: $gt,
75-
lte: $lte,
76-
gte: $gte,
77-
params: $params,
78-
);
51+
return new RangeQuery($field, $lt, $gt, $lte, $gte);
7952
}
8053

81-
public static function nested(string $field, QueryInterface $query, array $params = []): NestedQuery
54+
public static function nested(string $field, QueryInterface $query): NestedQuery
8255
{
83-
return new NestedQuery(
84-
path: $field,
85-
query: $query,
86-
params: $params,
87-
);
56+
return new NestedQuery($field, $query);
8857
}
8958

90-
public static function match(string $field, string $query, array $params = []): MatchQuery
59+
public static function match(string $field, string $query): MatchQuery
9160
{
92-
return new MatchQuery(
93-
field: $field,
94-
query: $query,
95-
params: $params,
96-
);
61+
return new MatchQuery($field, $query);
9762
}
9863

99-
public static function matchPhrase(string $field, string $query, array $params = []): MatchPhraseQuery
64+
public static function matchPhrase(string $field, string $query): MatchPhraseQuery
10065
{
101-
return new MatchPhraseQuery(
102-
field: $field,
103-
query: $query,
104-
params: $params,
105-
);
66+
return new MatchPhraseQuery($field, $query);
10667
}
10768

108-
public static function matchPhrasePrefix(string $field, string $query, array $params = []): MatchPhrasePrefixQuery
69+
public static function matchPhrasePrefix(string $field, string $query): MatchPhrasePrefixQuery
10970
{
110-
return new MatchPhrasePrefixQuery(
111-
field: $field,
112-
query: $query,
113-
params: $params,
114-
);
71+
return new MatchPhrasePrefixQuery($field, $query);
11572
}
11673

117-
public static function multiMatch(array $fields, string $query, array $params = []): MultiMatchQuery
74+
public static function multiMatch(array $fields, string $query): MultiMatchQuery
11875
{
119-
return new MultiMatchQuery(
120-
fields: $fields,
121-
query: $query,
122-
params: $params,
123-
);
76+
return new MultiMatchQuery($fields, $query);
12477
}
12578

12679
/**
12780
* @param float[]|int[] $position
12881
*/
129-
public static function geoDistance(string $field, string $distance, array $position, array $params = []): GeoDistanceQuery
82+
public static function geoDistance(string $field, string $distance, array $position): GeoDistanceQuery
13083
{
131-
return new GeoDistanceQuery(
132-
distance: $distance,
133-
field: $field,
134-
position: $position,
135-
params: $params,
136-
);
84+
return new GeoDistanceQuery($distance, $field, $position);
13785
}
13886

13987
/**
14088
* @param mixed[]|float[]|int[] $coordinates
14189
*/
142-
public static function geoShape(string $field, string $type, array $coordinates, array $params = []): GeoShapeQuery
90+
public static function geoShape(string $field, string $type, array $coordinates): GeoShapeQuery
14391
{
144-
return new GeoShapeQuery(
145-
field: $field,
146-
type: $type,
147-
coordinates: $coordinates,
148-
params: $params,
149-
);
92+
return new GeoShapeQuery($field, $type, $coordinates);
15093
}
15194

152-
public static function prefix(string $field, string $value, array $params = []): PrefixQuery
95+
public static function prefix(string $field, string $value): PrefixQuery
15396
{
154-
return new PrefixQuery(
155-
field: $field,
156-
value: $value,
157-
params: $params,
158-
);
97+
return new PrefixQuery($field, $value);
15998
}
16099

161-
public static function queryString(string $query, string $defaultField = null, array $params = []): QueryStringQuery
100+
public static function queryString(string $query, string $defaultField = null): QueryStringQuery
162101
{
163-
return new QueryStringQuery(
164-
query: $query,
165-
defaultField: $defaultField,
166-
params: $params,
167-
);
102+
return new QueryStringQuery($query, $defaultField);
168103
}
169104

170-
public static function rankFeature(string $field, array $params = []): RankFeatureQuery
105+
public static function rankFeature(string $field): RankFeatureQuery
171106
{
172-
return new RankFeatureQuery(
173-
field: $field,
174-
params: $params,
175-
);
107+
return new RankFeatureQuery($field);
176108
}
177109
}

src/Query/QueryStringQuery.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Erichard\ElasticQueryBuilder\Contracts\QueryInterface;
88
use Erichard\ElasticQueryBuilder\Features\HasBoost;
9+
use Erichard\ElasticQueryBuilder\Features\HasFuzziness;
910
use Erichard\ElasticQueryBuilder\Features\HasMinimumShouldMatch;
1011
use Erichard\ElasticQueryBuilder\Features\HasRewrite;
1112

@@ -14,6 +15,7 @@ class QueryStringQuery implements QueryInterface
1415
use HasBoost;
1516
use HasMinimumShouldMatch;
1617
use HasRewrite;
18+
use HasFuzziness;
1719

1820
public function __construct(
1921
protected string $query,
@@ -23,12 +25,13 @@ public function __construct(
2325
?float $boost = null,
2426
?string $minimumShouldMatch = null,
2527
?string $rewrite = null,
26-
protected ?string $fuzziness = null,
28+
?string $fuzziness = null,
2729
protected array $params = [],
2830
) {
2931
$this->boost = $boost;
3032
$this->minimumShouldMatch = $minimumShouldMatch;
3133
$this->rewrite = $rewrite;
34+
$this->fuzziness = $fuzziness;
3235
}
3336

3437
public function setQuery(string $query): self
@@ -59,13 +62,6 @@ public function setFields(?array $fields): self
5962
return $this;
6063
}
6164

62-
public function setFuzziness(?string $fuzziness): self
63-
{
64-
$this->fuzziness = $fuzziness;
65-
66-
return $this;
67-
}
68-
6965
public function setParams(array $params): self
7066
{
7167
$this->params = $params;
@@ -90,13 +86,10 @@ public function build(): array
9086
$build['fields'] = $this->fields;
9187
}
9288

93-
if (null !== $this->fuzziness) {
94-
$build['fuzziness'] = $this->fuzziness;
95-
}
96-
9789
$this->buildBoostTo($build);
9890
$this->buildMinimumShouldMatchTo($build);
9991
$this->buildRewriteTo($build);
92+
$this->buildFuzzinessTo($build);
10093

10194
return [
10295
'query_string' => $build,

0 commit comments

Comments
 (0)