Skip to content

Commit 773e4cf

Browse files
authored
Merge pull request #28 from Ilyes512/add-missing-factories
Add missing factories
2 parents 71ab3e8 + 9529f40 commit 773e4cf

8 files changed

+110
-20
lines changed

Diff for: .php_cs.dist.php

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php declare(strict_types=1);
22

3+
use PhpCsFixer\Fixer\FunctionNotation\NullableTypeDeclarationForDefaultNullValueFixer;
4+
35
$finder = PhpCsFixer\Finder::create()
46
->in(__DIR__ . '/src')
57
->exclude(__DIR__ . '/tests')
@@ -26,6 +28,7 @@
2628
'concat_space' => false,
2729
'declare_strict_types' => true,
2830
'native_function_invocation' => ['include' => []],
31+
'nullable_type_declaration_for_default_null_value' => ['use_nullable_type_declaration' => true],
2932
PhpCsFixerCustomFixers\Fixer\DeclareAfterOpeningTagFixer::name() => true,
3033
PhpCsFixerCustomFixers\Fixer\NoDoctrineMigrationsGeneratedCommentFixer::name() => true,
3134
PhpCsFixerCustomFixers\Fixer\NoImportFromGlobalNamespaceFixer::name() => true,

Diff for: src/Aggregation/Aggregation.php

+23
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,27 @@ public static function topHits(string $name, array $aggregations = []): TopHitsA
107107
{
108108
return new TopHitsAggregation($name, $aggregations);
109109
}
110+
111+
public static function histogram(string $name, string $field, int $interval): HistogramAggregation
112+
{
113+
return new HistogramAggregation($name, $field, $interval);
114+
}
115+
116+
/**
117+
* @param array<int> $ranges pass desired ranges that will be converted to linear range
118+
*/
119+
public static function ranges(string $name, string $field, array $ranges): RangesAggregation
120+
{
121+
return new RangesAggregation($name, $field, $ranges);
122+
}
123+
124+
public static function widthHistogram(string $name, string $field, int $buckets): WidthHistogramAggregation
125+
{
126+
return new WidthHistogramAggregation($name, $field, $buckets);
127+
}
128+
129+
public static function stats(string $name): StatsAggregation
130+
{
131+
return new StatsAggregation($name);
132+
}
110133
}

Diff for: src/Aggregation/RangesAggregation.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class RangesAggregation extends AbstractAggregation
1414
* @param array<int> $ranges pass desired ranges that will be converted to
1515
* linear range
1616
* @param array<AbstractAggregation> $aggregations
17-
* @param bool $equalConditionOnToRange Se to true if you want to do a histogram with 0
17+
* @param bool $equalConditionOnToRange Set to true if you want to do a histogram with 0
1818
* - 10, 10 - 15, and correctly count the number
1919
* (entry with 10 will be in first and seconds
2020
* segment

Diff for: src/Query/Query.php

+21-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public static function range(
4949
return new RangeQuery($field, $lt, $gt, $lte, $gte);
5050
}
5151

52-
public static function nested(string $field, QueryInterface $query): NestedQuery
52+
public static function nested(?string $path, QueryInterface $query): NestedQuery
5353
{
54-
return new NestedQuery($field, $query);
54+
return new NestedQuery($path, $query);
5555
}
5656

5757
public static function match(string $field, string $query): MatchQuery
@@ -84,6 +84,11 @@ public static function functionsQuery(string $field): FunctionsQuery
8484
return new FunctionsQuery($field);
8585
}
8686

87+
public static function GeoBoundingBoxQuery(string $field): GeoBoundingBoxQuery
88+
{
89+
return new GeoBoundingBoxQuery($field);
90+
}
91+
8792
/**
8893
* @param float[]|int[] $position
8994
*/
@@ -105,7 +110,7 @@ public static function prefix(string $field, string $value): PrefixQuery
105110
return new PrefixQuery($field, $value);
106111
}
107112

108-
public static function queryString(string $query, string $defaultField = null): QueryStringQuery
113+
public static function queryString(string $query, ?string $defaultField = null): QueryStringQuery
109114
{
110115
return new QueryStringQuery($query, $defaultField);
111116
}
@@ -114,4 +119,17 @@ public static function rankFeature(string $field): RankFeatureQuery
114119
{
115120
return new RankFeatureQuery($field);
116121
}
122+
123+
public static function exists(string $field): ExistsQuery
124+
{
125+
return new ExistsQuery($field);
126+
}
127+
128+
/**
129+
* @param mixed[]|string[] $fields
130+
*/
131+
public static function simpleQueryString(array $fields, string $query): SimpleQueryStringQuery
132+
{
133+
return new SimpleQueryStringQuery($fields, $query);
134+
}
117135
}

Diff for: src/Query/SimpleQueryStringQuery.php

+13-11
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
namespace Erichard\ElasticQueryBuilder\Query;
44

55
use Erichard\ElasticQueryBuilder\Contracts\QueryInterface;
6+
use Erichard\ElasticQueryBuilder\Features\HasBoost;
67
use Erichard\ElasticQueryBuilder\Features\HasMinimumShouldMatch;
78

89
class SimpleQueryStringQuery implements QueryInterface
910
{
11+
use HasBoost;
1012
use HasMinimumShouldMatch;
1113

1214
/**
@@ -15,7 +17,6 @@ class SimpleQueryStringQuery implements QueryInterface
1517
public function __construct(
1618
protected array $fields,
1719
protected string $query,
18-
1920
private ?string $flags = null,
2021
private ?bool $fuzzyTranspositions = null,
2122
private ?int $fuzzyMaxExpansions = null,
@@ -27,7 +28,6 @@ public function __construct(
2728
private ?string $quoteFieldSuffix = null,
2829
private ?bool $analyzeWildCard = null,
2930
private ?bool $autoGenerateSynonymsPhraseQuery = null,
30-
3131
protected array $params = [],
3232
) {
3333
$this->minimumShouldMatch = $minimumShouldMatch;
@@ -36,69 +36,70 @@ public function __construct(
3636
public function setFlags(string|null $flags): self
3737
{
3838
$this->flags = $flags;
39+
3940
return $this;
4041
}
4142

42-
4343
public function setFuzzyTranspositions(bool|null $fuzzyTranspositions): self
4444
{
4545
$this->fuzzyTranspositions = $fuzzyTranspositions;
46+
4647
return $this;
4748
}
4849

49-
5050
public function setFuzzyMaxExpansions(int|null $fuzzyMaxExpansions): self
5151
{
5252
$this->fuzzyMaxExpansions = $fuzzyMaxExpansions;
53+
5354
return $this;
5455
}
5556

56-
5757
public function setFuzzyPrefixLength(int|null $fuzzyPrefixLength): self
5858
{
5959
$this->fuzzyPrefixLength = $fuzzyPrefixLength;
60+
6061
return $this;
6162
}
6263

63-
6464
public function setDefaultOperator(string|null $defaultOperator): self
6565
{
6666
$this->defaultOperator = $defaultOperator;
67+
6768
return $this;
6869
}
6970

70-
7171
public function setAnalyzer(string|null $analyzer): self
7272
{
7373
$this->analyzer = $analyzer;
74+
7475
return $this;
7576
}
7677

77-
7878
public function setLenient(bool|null $lenient): self
7979
{
8080
$this->lenient = $lenient;
81+
8182
return $this;
8283
}
8384

84-
8585
public function setQuoteFieldSuffix(string|null $quoteFieldSuffix): self
8686
{
8787
$this->quoteFieldSuffix = $quoteFieldSuffix;
88+
8889
return $this;
8990
}
9091

91-
9292
public function setAnalyzeWildCard(bool|null $analyzeWildCard): self
9393
{
9494
$this->analyzeWildCard = $analyzeWildCard;
95+
9596
return $this;
9697
}
9798

98-
9999
public function setAutoGenerateSynonymsPhraseQuery(bool|null $autoGenerateSynonymsPhraseQuery): self
100100
{
101101
$this->autoGenerateSynonymsPhraseQuery = $autoGenerateSynonymsPhraseQuery;
102+
102103
return $this;
103104
}
104105

@@ -168,6 +169,7 @@ public function build(): array
168169
}
169170

170171
$this->buildMinimumShouldMatchTo($data);
172+
$this->buildBoostTo($data);
171173

172174
$build = $this->params;
173175
$build['simple_query_string'] = $data;

Diff for: tests/Query/GeoBoundingBoxQueryTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ class GeoBoundingBoxQueryTest extends TestCase
1212
{
1313
public function testBuildFailsOnAllNull(): void
1414
{
15-
$this->expectErrorMessage('GeoBoundingBoxQuery needs at least 2 sides set');
15+
$this->expectExceptionMessage('GeoBoundingBoxQuery needs at least 2 sides set');
1616
(new GeoBoundingBoxQuery('test'))->build();
1717
}
1818

1919
public function testBuildFailsOnOneFilter(): void
2020
{
21-
$this->expectErrorMessage('GeoBoundingBoxQuery needs at least 2 sides set');
21+
$this->expectExceptionMessage('GeoBoundingBoxQuery needs at least 2 sides set');
2222
(new GeoBoundingBoxQuery(field: 'test', topLeft: new GpsPointEntity(1.1, 2.1)))->build();
2323
}
2424

Diff for: tests/Query/SimpleQueryStringQueryTest.php

+45-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Tests\Erichard\ElasticQueryBuilder\Query;
66

7-
use Erichard\ElasticQueryBuilder\Query\MultiMatchQuery;
87
use Erichard\ElasticQueryBuilder\Query\SimpleQueryStringQuery;
98
use PHPUnit\Framework\TestCase;
109

@@ -94,4 +93,49 @@ public function testItBuildTheQueryWithAFuzziness(): void
9493
],
9594
], $query->build());
9695
}
96+
97+
public function testItBuildTheQueryWithBoost(): void
98+
{
99+
$query = new SimpleQueryStringQuery(
100+
['subject', 'body'],
101+
'~brown fox',
102+
'ALL',
103+
true,
104+
50,
105+
0,
106+
"1%",
107+
"or",
108+
"standard",
109+
false,
110+
"",
111+
false,
112+
true
113+
114+
);
115+
$query->setBoost(3);
116+
117+
$this->assertEquals([
118+
'simple_query_string' =>
119+
[
120+
'query' => '~brown fox',
121+
'fields' =>
122+
[
123+
'subject',
124+
'body',
125+
],
126+
'flags' => 'ALL',
127+
'fuzzy_transpositions' => true,
128+
'fuzzy_max_expansions' => 50,
129+
'fuzzy_prefix_length' => 0,
130+
'default_operator' => 'or',
131+
'analyzer' => 'standard',
132+
'lenient' => false,
133+
'quote_field_suffix' => '',
134+
'analyze_wildcard' => false,
135+
'auto_generate_synonyms_phrase_query' => true,
136+
'minimum_should_match' => '1%',
137+
'boost' => 3,
138+
],
139+
], $query->build());
140+
}
97141
}

Diff for: tests/QueryBuilderTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function testItSetThePitAsString(): void
7272

7373
$query = $queryBuilder->build();
7474

75-
$this->assertEquals(['id' => 'pit-as-string'], $query['pit']);
75+
$this->assertEquals(['id' => 'pit-as-string'], $query['body']['pit']);
7676
}
7777

7878
public function testItSetThePitAsArray(): void
@@ -83,7 +83,7 @@ public function testItSetThePitAsArray(): void
8383

8484
$query = $queryBuilder->build();
8585

86-
$this->assertEquals(['id' => 'pit-as-array', 'keep_alive' => '1m'], $query['pit']);
86+
$this->assertEquals(['id' => 'pit-as-array', 'keep_alive' => '1m'], $query['body']['pit']);
8787
}
8888

8989
public function testItAllowToSort(): void

0 commit comments

Comments
 (0)