Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing factories #28

Merged
merged 7 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .php_cs.dist.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php declare(strict_types=1);

use PhpCsFixer\Fixer\FunctionNotation\NullableTypeDeclarationForDefaultNullValueFixer;

$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->exclude(__DIR__ . '/tests')
Expand All @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions src/Aggregation/Aggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> $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);
}
}
2 changes: 1 addition & 1 deletion src/Aggregation/RangesAggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RangesAggregation extends AbstractAggregation
* @param array<int> $ranges pass desired ranges that will be converted to
* linear range
* @param array<AbstractAggregation> $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
Expand Down
24 changes: 21 additions & 3 deletions src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand All @@ -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);
}
Expand All @@ -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);
}
}
24 changes: 13 additions & 11 deletions src/Query/SimpleQueryStringQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -168,6 +169,7 @@ public function build(): array
}

$this->buildMinimumShouldMatchTo($data);
$this->buildBoostTo($data);

$build = $this->params;
$build['simple_query_string'] = $data;
Expand Down
4 changes: 2 additions & 2 deletions tests/Query/GeoBoundingBoxQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
46 changes: 45 additions & 1 deletion tests/Query/SimpleQueryStringQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Tests\Erichard\ElasticQueryBuilder\Query;

use Erichard\ElasticQueryBuilder\Query\MultiMatchQuery;
use Erichard\ElasticQueryBuilder\Query\SimpleQueryStringQuery;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -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());
}
}
4 changes: 2 additions & 2 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down