Skip to content

Commit 100a28e

Browse files
authored
Merge pull request #17 from igoooor/main
Allow full Elastic API using a params argument
2 parents 07f55c5 + c28157f commit 100a28e

17 files changed

+261
-77
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/AbstractMatchQuery.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ abstract class AbstractMatchQuery implements QueryInterface
1414
public function __construct(
1515
string $field,
1616
protected string $query,
17-
protected ?string $analyzer = null
17+
protected ?string $analyzer = null,
18+
protected array $params = [],
1819
) {
1920
$this->field = $field;
2021
}
@@ -33,15 +34,21 @@ public function setAnalyzer(?string $analyzer): self
3334
return $this;
3435
}
3536

37+
public function setParams(array $params): self
38+
{
39+
$this->params = $params;
40+
41+
return $this;
42+
}
43+
3644
public function build(): array
3745
{
3846
$queryName = $this->getQueryName();
3947

40-
$query = [
41-
$queryName => [
42-
$this->field => [
43-
'query' => $this->query,
44-
],
48+
$query = $this->params;
49+
$query[$queryName] = [
50+
$this->field => [
51+
'query' => $this->query,
4552
],
4653
];
4754

src/Query/BoolQuery.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function __construct(
2020
private array $mustNot = [],
2121
private array $should = [],
2222
private array $filter = [],
23+
private array $params = [],
2324
) {
2425
}
2526

@@ -75,9 +76,16 @@ public function isEmpty(): bool
7576
&& empty($this->filter);
7677
}
7778

79+
public function setParams(array $params): self
80+
{
81+
$this->params = $params;
82+
83+
return $this;
84+
}
85+
7886
public function build(): array
7987
{
80-
$query = [];
88+
$query = $this->params;
8189

8290
$this
8391
->buildQueries($query, 'should', $this->should)

src/Query/GeoDistanceQuery.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class GeoDistanceQuery implements QueryInterface
1717
public function __construct(
1818
private string $distance,
1919
string $field,
20-
private array $position
20+
private array $position,
21+
private array $params = [],
2122
) {
2223
$this->field = $field;
2324
}
@@ -36,16 +37,24 @@ public function setPosition(array $position): self
3637
return $this;
3738
}
3839

40+
public function setParams(array $params): self
41+
{
42+
$this->params = $params;
43+
44+
return $this;
45+
}
46+
3947
public function build(): array
4048
{
41-
return [
42-
'geo_distance' => [
43-
'distance' => $this->distance,
44-
$this->field => [
45-
'lat' => $this->position[0],
46-
'lon' => $this->position[1],
47-
],
49+
$build = $this->params;
50+
$build['geo_distance'] = [
51+
'distance' => $this->distance,
52+
$this->field => [
53+
'lat' => $this->position[0],
54+
'lon' => $this->position[1],
4855
],
4956
];
57+
58+
return $build;
5059
}
5160
}

src/Query/GeoShapeQuery.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class GeoShapeQuery implements QueryInterface
1616
public function __construct(
1717
private string $field,
1818
private string $type,
19-
private array $coordinates
19+
private array $coordinates,
20+
private array $params = [],
2021
) {
2122
}
2223

@@ -48,18 +49,26 @@ public function setRelation(string $relation): self
4849
return $this;
4950
}
5051

52+
public function setParams(array $params): self
53+
{
54+
$this->params = $params;
55+
56+
return $this;
57+
}
58+
5159
public function build(): array
5260
{
53-
return [
54-
'geo_shape' => [
55-
$this->field => [
56-
'shape' => [
57-
'type' => $this->type,
58-
'coordinates' => $this->coordinates,
59-
],
60-
'relation' => $this->relation,
61+
$build = $this->params;
62+
$build['geo_shape'] = [
63+
$this->field => [
64+
'shape' => [
65+
'type' => $this->type,
66+
'coordinates' => $this->coordinates,
6167
],
68+
'relation' => $this->relation,
6269
],
6370
];
71+
72+
return $build;
6473
}
6574
}

src/Query/MatchQuery.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,51 @@
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,
20-
?string $minimumShouldMatch = null
22+
?string $minimumShouldMatch = null,
23+
?string $fuzziness = null,
24+
array $params = [],
2125
) {
22-
parent::__construct($field, $query, $analyzer);
26+
parent::__construct($field, $query, $analyzer, $params);
2327

2428
$this->operator = $operator;
2529
$this->minimumShouldMatch = $minimumShouldMatch;
30+
$this->fuzziness = $fuzziness;
2631
}
2732

2833
public function getQueryName(): string
2934
{
3035
return 'match';
3136
}
3237

38+
public function setParams(array $params): self
39+
{
40+
$this->params = $params;
41+
42+
return $this;
43+
}
44+
3345
public function build(): array
3446
{
3547
$build = parent::build();
3648

3749
$this->buildOperatorTo($build[$this->getQueryName()][$this->field]);
3850
$this->buildMinimumShouldMatchTo($build[$this->getQueryName()][$this->field]);
51+
$this->buildFuzzinessTo($build[$this->getQueryName()][$this->field]);
3952

4053
return $build;
4154
}

src/Query/MultiMatchQuery.php

Lines changed: 13 additions & 11 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,14 +24,16 @@ 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,
28-
?string $minimumShouldMatch = null
30+
?string $minimumShouldMatch = null,
31+
protected array $params = [],
2932
) {
3033
$this->operator = $operator;
3134
$this->boost = $boost;
3235
$this->minimumShouldMatch = $minimumShouldMatch;
36+
$this->fuzziness = $fuzziness;
3337
}
3438

3539
public function setFields(array $fields): self
@@ -53,9 +57,9 @@ public function setType(string $type): self
5357
return $this;
5458
}
5559

56-
public function setFuzziness(string $fuzziness): self
60+
public function setParams(array $params): self
5761
{
58-
$this->fuzziness = $fuzziness;
62+
$this->params = $params;
5963

6064
return $this;
6165
}
@@ -71,16 +75,14 @@ public function build(): array
7175
$data['type'] = $this->type;
7276
}
7377

74-
if (null !== $this->fuzziness) {
75-
$data['fuzziness'] = $this->fuzziness;
76-
}
77-
7878
$this->buildOperatorTo($data);
7979
$this->buildBoostTo($data);
8080
$this->buildMinimumShouldMatchTo($data);
81+
$this->buildFuzzinessTo($data);
8182

82-
return [
83-
'multi_match' => $data,
84-
];
83+
$build = $this->params;
84+
$build['multi_match'] = $data;
85+
86+
return $build;
8587
}
8688
}

src/Query/NestedQuery.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class NestedQuery implements QueryInterface
1010
{
1111
public function __construct(
1212
protected ?string $path,
13-
protected QueryInterface $query
13+
protected QueryInterface $query,
14+
protected array $params = [],
1415
) {
1516
}
1617

@@ -28,13 +29,21 @@ public function setQuery(QueryInterface $query): self
2829
return $this;
2930
}
3031

32+
public function setParams(array $params): self
33+
{
34+
$this->params = $params;
35+
36+
return $this;
37+
}
38+
3139
public function build(): array
3240
{
33-
return [
34-
'nested' => [
35-
'path' => $this->path,
36-
'query' => $this->query->build(),
37-
],
41+
$build = $this->params;
42+
$build['nested'] = [
43+
'path' => $this->path,
44+
'query' => $this->query->build(),
3845
];
46+
47+
return $build;
3948
}
4049
}

src/Query/PrefixQuery.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public function __construct(
2222
string $field,
2323
protected string $value,
2424
?string $rewrite = null,
25-
?bool $caseInsensitive = null
25+
?bool $caseInsensitive = null,
26+
protected array $params = [],
2627
) {
2728
$this->field = $field;
2829
$this->rewrite = $rewrite;
@@ -36,11 +37,17 @@ public function setValue(string $value): self
3637
return $this;
3738
}
3839

40+
public function setParams(array $params): self
41+
{
42+
$this->params = $params;
43+
44+
return $this;
45+
}
46+
3947
public function build(): array
4048
{
41-
$build = [
42-
'value' => $this->value,
43-
];
49+
$build = $this->params;
50+
$build['value'] = $this->value;
4451

4552
$this->buildRewriteTo($build);
4653
$this->buildCaseInsensitiveTo($build);

src/Query/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static function range(
4646
int|float|string|null $lt = null,
4747
int|float|string|null $gt = null,
4848
int|float|string|null $lte = null,
49-
int|float|string|null $gte = null
49+
int|float|string|null $gte = null,
5050
): RangeQuery {
5151
return new RangeQuery($field, $lt, $gt, $lte, $gte);
5252
}

0 commit comments

Comments
 (0)