diff --git a/.php_cs.dist.php b/.php_cs.dist.php index f537074..3404eb7 100644 --- a/.php_cs.dist.php +++ b/.php_cs.dist.php @@ -1,5 +1,7 @@ in(__DIR__ . '/src') ->exclude(__DIR__ . '/tests') @@ -26,6 +28,7 @@ 'concat_space' => false, 'declare_strict_types' => true, 'native_function_invocation' => ['include' => []], + 'nullable_type_declaration_for_default_null_value' => ['use_nullable_type_declaration' => true], PhpCsFixerCustomFixers\Fixer\DeclareAfterOpeningTagFixer::name() => true, PhpCsFixerCustomFixers\Fixer\NoDoctrineMigrationsGeneratedCommentFixer::name() => true, PhpCsFixerCustomFixers\Fixer\NoImportFromGlobalNamespaceFixer::name() => true, diff --git a/src/Aggregation/Aggregation.php b/src/Aggregation/Aggregation.php index 47cd21a..f68d68d 100644 --- a/src/Aggregation/Aggregation.php +++ b/src/Aggregation/Aggregation.php @@ -107,4 +107,27 @@ public static function topHits(string $name, array $aggregations = []): TopHitsA { return new TopHitsAggregation($name, $aggregations); } + + public static function histogram(string $name, string $field, int $interval): HistogramAggregation + { + return new HistogramAggregation($name, $field, $interval); + } + + /** + * @param array $ranges pass desired ranges that will be converted to linear range + */ + public static function ranges(string $name, string $field, array $ranges): RangesAggregation + { + return new RangesAggregation($name, $field, $ranges); + } + + public static function widthHistogram(string $name, string $field, int $buckets): WidthHistogramAggregation + { + return new WidthHistogramAggregation($name, $field, $buckets); + } + + public static function stats(string $name): StatsAggregation + { + return new StatsAggregation($name); + } } diff --git a/src/Aggregation/RangesAggregation.php b/src/Aggregation/RangesAggregation.php index 692a98c..b671627 100644 --- a/src/Aggregation/RangesAggregation.php +++ b/src/Aggregation/RangesAggregation.php @@ -14,7 +14,7 @@ class RangesAggregation extends AbstractAggregation * @param array $ranges pass desired ranges that will be converted to * linear range * @param array $aggregations - * @param bool $equalConditionOnToRange Se to true if you want to do a histogram with 0 + * @param bool $equalConditionOnToRange Set to true if you want to do a histogram with 0 * - 10, 10 - 15, and correctly count the number * (entry with 10 will be in first and seconds * segment diff --git a/src/Query/Query.php b/src/Query/Query.php index 1610250..bd9bde8 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -49,9 +49,9 @@ public static function range( return new RangeQuery($field, $lt, $gt, $lte, $gte); } - public static function nested(string $field, QueryInterface $query): NestedQuery + public static function nested(?string $path, QueryInterface $query): NestedQuery { - return new NestedQuery($field, $query); + return new NestedQuery($path, $query); } public static function match(string $field, string $query): MatchQuery @@ -84,6 +84,11 @@ public static function functionsQuery(string $field): FunctionsQuery return new FunctionsQuery($field); } + public static function GeoBoundingBoxQuery(string $field): GeoBoundingBoxQuery + { + return new GeoBoundingBoxQuery($field); + } + /** * @param float[]|int[] $position */ @@ -105,7 +110,7 @@ public static function prefix(string $field, string $value): PrefixQuery return new PrefixQuery($field, $value); } - public static function queryString(string $query, string $defaultField = null): QueryStringQuery + public static function queryString(string $query, ?string $defaultField = null): QueryStringQuery { return new QueryStringQuery($query, $defaultField); } @@ -114,4 +119,17 @@ public static function rankFeature(string $field): RankFeatureQuery { return new RankFeatureQuery($field); } + + public static function exists(string $field): ExistsQuery + { + return new ExistsQuery($field); + } + + /** + * @param mixed[]|string[] $fields + */ + public static function simpleQueryString(array $fields, string $query): SimpleQueryStringQuery + { + return new SimpleQueryStringQuery($fields, $query); + } } diff --git a/src/Query/SimpleQueryStringQuery.php b/src/Query/SimpleQueryStringQuery.php index ac84d0a..c832a47 100644 --- a/src/Query/SimpleQueryStringQuery.php +++ b/src/Query/SimpleQueryStringQuery.php @@ -3,10 +3,12 @@ namespace Erichard\ElasticQueryBuilder\Query; use Erichard\ElasticQueryBuilder\Contracts\QueryInterface; +use Erichard\ElasticQueryBuilder\Features\HasBoost; use Erichard\ElasticQueryBuilder\Features\HasMinimumShouldMatch; class SimpleQueryStringQuery implements QueryInterface { + use HasBoost; use HasMinimumShouldMatch; /** @@ -15,7 +17,6 @@ class SimpleQueryStringQuery implements QueryInterface public function __construct( protected array $fields, protected string $query, - private ?string $flags = null, private ?bool $fuzzyTranspositions = null, private ?int $fuzzyMaxExpansions = null, @@ -27,7 +28,6 @@ public function __construct( private ?string $quoteFieldSuffix = null, private ?bool $analyzeWildCard = null, private ?bool $autoGenerateSynonymsPhraseQuery = null, - protected array $params = [], ) { $this->minimumShouldMatch = $minimumShouldMatch; @@ -36,69 +36,70 @@ public function __construct( public function setFlags(string|null $flags): self { $this->flags = $flags; + return $this; } - public function setFuzzyTranspositions(bool|null $fuzzyTranspositions): self { $this->fuzzyTranspositions = $fuzzyTranspositions; + return $this; } - public function setFuzzyMaxExpansions(int|null $fuzzyMaxExpansions): self { $this->fuzzyMaxExpansions = $fuzzyMaxExpansions; + return $this; } - public function setFuzzyPrefixLength(int|null $fuzzyPrefixLength): self { $this->fuzzyPrefixLength = $fuzzyPrefixLength; + return $this; } - public function setDefaultOperator(string|null $defaultOperator): self { $this->defaultOperator = $defaultOperator; + return $this; } - public function setAnalyzer(string|null $analyzer): self { $this->analyzer = $analyzer; + return $this; } - public function setLenient(bool|null $lenient): self { $this->lenient = $lenient; + return $this; } - public function setQuoteFieldSuffix(string|null $quoteFieldSuffix): self { $this->quoteFieldSuffix = $quoteFieldSuffix; + return $this; } - public function setAnalyzeWildCard(bool|null $analyzeWildCard): self { $this->analyzeWildCard = $analyzeWildCard; + return $this; } - public function setAutoGenerateSynonymsPhraseQuery(bool|null $autoGenerateSynonymsPhraseQuery): self { $this->autoGenerateSynonymsPhraseQuery = $autoGenerateSynonymsPhraseQuery; + return $this; } @@ -168,6 +169,7 @@ public function build(): array } $this->buildMinimumShouldMatchTo($data); + $this->buildBoostTo($data); $build = $this->params; $build['simple_query_string'] = $data; diff --git a/tests/Query/GeoBoundingBoxQueryTest.php b/tests/Query/GeoBoundingBoxQueryTest.php index 1e00c1e..4af1013 100644 --- a/tests/Query/GeoBoundingBoxQueryTest.php +++ b/tests/Query/GeoBoundingBoxQueryTest.php @@ -12,13 +12,13 @@ class GeoBoundingBoxQueryTest extends TestCase { public function testBuildFailsOnAllNull(): void { - $this->expectErrorMessage('GeoBoundingBoxQuery needs at least 2 sides set'); + $this->expectExceptionMessage('GeoBoundingBoxQuery needs at least 2 sides set'); (new GeoBoundingBoxQuery('test'))->build(); } public function testBuildFailsOnOneFilter(): void { - $this->expectErrorMessage('GeoBoundingBoxQuery needs at least 2 sides set'); + $this->expectExceptionMessage('GeoBoundingBoxQuery needs at least 2 sides set'); (new GeoBoundingBoxQuery(field: 'test', topLeft: new GpsPointEntity(1.1, 2.1)))->build(); } diff --git a/tests/Query/SimpleQueryStringQueryTest.php b/tests/Query/SimpleQueryStringQueryTest.php index 4727cdf..f7bc6a3 100644 --- a/tests/Query/SimpleQueryStringQueryTest.php +++ b/tests/Query/SimpleQueryStringQueryTest.php @@ -4,7 +4,6 @@ namespace Tests\Erichard\ElasticQueryBuilder\Query; -use Erichard\ElasticQueryBuilder\Query\MultiMatchQuery; use Erichard\ElasticQueryBuilder\Query\SimpleQueryStringQuery; use PHPUnit\Framework\TestCase; @@ -94,4 +93,49 @@ public function testItBuildTheQueryWithAFuzziness(): void ], ], $query->build()); } + + public function testItBuildTheQueryWithBoost(): void + { + $query = new SimpleQueryStringQuery( + ['subject', 'body'], + '~brown fox', + 'ALL', + true, + 50, + 0, + "1%", + "or", + "standard", + false, + "", + false, + true + + ); + $query->setBoost(3); + + $this->assertEquals([ + 'simple_query_string' => + [ + 'query' => '~brown fox', + 'fields' => + [ + 'subject', + 'body', + ], + 'flags' => 'ALL', + 'fuzzy_transpositions' => true, + 'fuzzy_max_expansions' => 50, + 'fuzzy_prefix_length' => 0, + 'default_operator' => 'or', + 'analyzer' => 'standard', + 'lenient' => false, + 'quote_field_suffix' => '', + 'analyze_wildcard' => false, + 'auto_generate_synonyms_phrase_query' => true, + 'minimum_should_match' => '1%', + 'boost' => 3, + ], + ], $query->build()); + } } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 0cb5e77..b7508d9 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -72,7 +72,7 @@ public function testItSetThePitAsString(): void $query = $queryBuilder->build(); - $this->assertEquals(['id' => 'pit-as-string'], $query['pit']); + $this->assertEquals(['id' => 'pit-as-string'], $query['body']['pit']); } public function testItSetThePitAsArray(): void @@ -83,7 +83,7 @@ public function testItSetThePitAsArray(): void $query = $queryBuilder->build(); - $this->assertEquals(['id' => 'pit-as-array', 'keep_alive' => '1m'], $query['pit']); + $this->assertEquals(['id' => 'pit-as-array', 'keep_alive' => '1m'], $query['body']['pit']); } public function testItAllowToSort(): void