From ead3aad80ac9792a06ed9798b09762aa82604f26 Mon Sep 17 00:00:00 2001 From: Denis Urban <urbandenis0@gmail.com> Date: Thu, 3 Aug 2023 19:34:08 +0200 Subject: [PATCH] reformat code --- .github/workflows/main.yml | 2 +- src/Aggregation/AbstractAggregation.php | 68 ++++++------ .../Bucketing/AdjacencyMatrixAggregation.php | 4 +- .../AutoDateHistogramAggregation.php | 3 +- .../Bucketing/ChildrenAggregation.php | 24 ++-- .../Bucketing/CompositeAggregation.php | 16 +-- .../Bucketing/DateHistogramAggregation.php | 24 ++-- .../Bucketing/DateRangeAggregation.php | 29 +++-- .../DiversifiedSamplerAggregation.php | 36 +++--- .../Bucketing/FilterAggregation.php | 34 +++--- .../Bucketing/FiltersAggregation.php | 4 +- .../Bucketing/GeoDistanceAggregation.php | 96 ++++++++-------- .../Bucketing/GeoHashGridAggregation.php | 62 +++++------ .../Bucketing/HistogramAggregation.php | 104 +++++++++--------- .../Bucketing/NestedAggregation.php | 32 +++--- .../Bucketing/ReverseNestedAggregation.php | 42 +++---- .../Bucketing/SamplerAggregation.php | 42 +++---- .../Matrix/MatrixStatsAggregation.php | 56 +++++----- .../Metric/CardinalityAggregation.php | 32 +++--- .../Metric/ExtendedStatsAggregation.php | 52 ++++----- .../Metric/GeoBoundsAggregation.php | 30 ++--- .../Metric/PercentileRanksAggregation.php | 60 +++++----- .../Metric/PercentilesAggregation.php | 56 +++++----- .../Metric/ScriptedMetricAggregation.php | 32 +++--- src/Aggregation/Metric/TopHitsAggregation.php | 76 ++++++------- .../Pipeline/AbstractPipelineAggregation.php | 16 +-- .../Pipeline/BucketScriptAggregation.php | 30 ++--- .../Pipeline/BucketSortAggregation.php | 41 ++++--- .../Pipeline/PercentilesBucketAggregation.php | 28 ++--- src/BuilderBag.php | 52 ++++----- src/InnerHit/NestedInnerHit.php | 54 ++++----- src/ParametersTrait.php | 26 ++--- src/Query/Compound/BoolQuery.php | 50 ++++----- src/Query/Compound/BoostingQuery.php | 25 +++-- src/Query/Compound/ConstantScoreQuery.php | 16 +-- src/Query/Compound/DisMaxQuery.php | 16 +-- src/Query/Compound/FunctionScoreQuery.php | 46 ++++---- src/Query/FullText/CommonTermsQuery.php | 16 +-- src/Query/FullText/MatchQuery.php | 16 +-- src/Query/FullText/MultiMatchQuery.php | 16 +-- src/Query/FullText/QueryStringQuery.php | 16 +-- src/Query/FullText/SimpleQueryStringQuery.php | 16 +-- src/Query/Geo/GeoBoundingBoxQuery.php | 14 +-- src/Query/Geo/GeoDistanceQuery.php | 28 ++--- src/Query/Geo/GeoPolygonQuery.php | 14 +-- src/Query/Geo/GeoShapeQuery.php | 68 ++++++++---- src/Query/Joining/HasChildQuery.php | 18 +-- src/Query/Joining/HasParentQuery.php | 18 +-- src/Query/Joining/NestedQuery.php | 20 ++-- src/Query/Joining/ParentIdQuery.php | 16 +-- src/Query/MatchAllQuery.php | 10 +- src/Query/Span/FieldMaskingSpanQuery.php | 32 +++--- src/Query/Span/SpanContainingQuery.php | 30 ++--- src/Query/Span/SpanFirstQuery.php | 18 +-- src/Query/Span/SpanMultiTermQuery.php | 16 +-- src/Query/Span/SpanNearQuery.php | 32 +++--- src/Query/Span/SpanNotQuery.php | 23 ++-- src/Query/Span/SpanOrQuery.php | 16 +-- src/Query/Specialized/MoreLikeThisQuery.php | 16 +-- src/Query/Specialized/RankFeatureQuery.php | 39 +++++++ src/Query/Specialized/ScriptQuery.php | 18 +-- src/Query/Specialized/TemplateQuery.php | 50 ++++----- src/Query/TermLevel/ExistsQuery.php | 16 +-- src/Query/TermLevel/FuzzyQuery.php | 16 +-- src/Query/TermLevel/IdsQuery.php | 16 +-- src/Query/TermLevel/PrefixQuery.php | 22 ++-- src/Query/TermLevel/RangeQuery.php | 16 +-- src/Query/TermLevel/RegexpQuery.php | 16 +-- src/Query/TermLevel/TermQuery.php | 16 +-- src/Query/TermLevel/TermsQuery.php | 22 ++-- src/Query/TermLevel/TermsSetQuery.php | 38 ++++--- src/Query/TermLevel/TypeQuery.php | 16 +-- src/Query/TermLevel/WildcardQuery.php | 16 +-- src/Search.php | 61 ++++------ src/SearchEndpoint/AggregationsEndpoint.php | 7 +- src/SearchEndpoint/HighlightEndpoint.php | 7 +- src/SearchEndpoint/InnerHitsEndpoint.php | 7 +- src/SearchEndpoint/PostFilterEndpoint.php | 7 +- src/SearchEndpoint/QueryEndpoint.php | 23 ++-- .../SearchEndpointInterface.php | 8 +- src/SearchEndpoint/SortEndpoint.php | 7 +- src/SearchEndpoint/SuggestEndpoint.php | 7 +- .../Normalizer/CustomReferencedNormalizer.php | 7 +- src/Serializer/OrderedSerializer.php | 36 +++--- src/Sort/FieldSort.php | 6 +- src/Sort/NestedSort.php | 18 +-- src/Suggest/Suggest.php | 48 ++++---- .../Query/FunctionScoreQueryTest.php | 7 +- .../Query/Compound/FunctionScoreQueryTest.php | 10 +- 89 files changed, 1272 insertions(+), 1196 deletions(-) create mode 100644 src/Query/Specialized/RankFeatureQuery.php diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 686b094..b21527b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -38,7 +38,7 @@ jobs: bootstrap: vendor/autoload.php configuration: phpunit.xml.dist php_extensions: xdebug - args: tests --coverage-clover ./coverage.xml + args: --coverage-clover ./coverage.xml - name: Upload to Codecov uses: codecov/codecov-action@v3 diff --git a/src/Aggregation/AbstractAggregation.php b/src/Aggregation/AbstractAggregation.php index d83de82..418606f 100644 --- a/src/Aggregation/AbstractAggregation.php +++ b/src/Aggregation/AbstractAggregation.php @@ -31,18 +31,6 @@ abstract class AbstractAggregation implements NamedBuilderInterface private ?BuilderBag $aggregations = null; - /** - * Abstract supportsNesting method. - * - * @return bool - */ - abstract protected function supportsNesting(): bool; - - /** - * @return array|\stdClass - */ - abstract public function getArray(): array|\stdClass; - /** * Inner aggregations container init. * @@ -53,6 +41,14 @@ abstract class AbstractAggregation implements NamedBuilderInterface $this->setName($name); } + /** + * @return string + */ + public function getField() + { + return $this->field; + } + /** * @param string $field * @@ -65,14 +61,6 @@ abstract class AbstractAggregation implements NamedBuilderInterface return $this; } - /** - * @return string - */ - public function getField() - { - return $this->field; - } - /** * Adds a sub-aggregation. * @@ -86,29 +74,25 @@ abstract class AbstractAggregation implements NamedBuilderInterface } $this->aggregations->add($abstractAggregation); - + return $this; } /** - * Returns all sub aggregations. + * Creates BuilderBag new instance. * - * @return BuilderBag[]|NamedBuilderInterface[] + * @return BuilderBag */ - public function getAggregations() + private function createBuilderBag() { - if ($this->aggregations instanceof BuilderBag) { - return $this->aggregations->all(); - } else { - return []; - } + return new BuilderBag(); } /** * Returns sub aggregation. * @param string $name Aggregation name to return. * - * @return AbstractAggregation|NamedBuilderInterface|null + * @return \ONGR\ElasticsearchDSL\BuilderInterface */ public function getAggregation($name) { @@ -140,6 +124,18 @@ abstract class AbstractAggregation implements NamedBuilderInterface return $result; } + /** + * @return array|\stdClass + */ + abstract public function getArray(): array|\stdClass; + + /** + * Abstract supportsNesting method. + * + * @return bool + */ + abstract protected function supportsNesting(): bool; + /** * Process all nested aggregations. * @@ -157,12 +153,16 @@ abstract class AbstractAggregation implements NamedBuilderInterface } /** - * Creates BuilderBag new instance. + * Returns all sub aggregations. * - * @return BuilderBag + * @return BuilderBag[]|NamedBuilderInterface[] */ - private function createBuilderBag() + public function getAggregations(): array { - return new BuilderBag(); + if ($this->aggregations instanceof BuilderBag) { + return $this->aggregations->all(); + } else { + return []; + } } } diff --git a/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php b/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php index 606c3cc..8ef4907 100644 --- a/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php +++ b/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php @@ -30,13 +30,13 @@ class AdjacencyMatrixAggregation extends AbstractAggregation * @var BuilderInterface[] */ private array $filters = [ - self::FILTERS => [] + self::FILTERS => [], ]; /** * Inner aggregations container init. * - * @param string $name + * @param string $name * @param BuilderInterface[] $filters */ public function __construct(string $name, array $filters = []) diff --git a/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php b/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php index 45a30b6..b68cc78 100644 --- a/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php +++ b/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php @@ -13,7 +13,6 @@ namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait; -use ONGR\ElasticsearchDSL\BuilderInterface; /** * Class representing AutoDateHistogramAggregation. @@ -29,7 +28,7 @@ class AutoDateHistogramAggregation extends AbstractAggregation * * @param string $name * @param string $field - * @param int $buckets + * @param int $buckets * @param string $format */ public function __construct(string $name, $field, $buckets = null, $format = null) diff --git a/src/Aggregation/Bucketing/ChildrenAggregation.php b/src/Aggregation/Bucketing/ChildrenAggregation.php index c36203e..7837029 100644 --- a/src/Aggregation/Bucketing/ChildrenAggregation.php +++ b/src/Aggregation/Bucketing/ChildrenAggregation.php @@ -32,18 +32,6 @@ class ChildrenAggregation extends AbstractAggregation $this->setChildren($children); } - public function setChildren(?string $children): static - { - $this->children = $children; - - return $this; - } - - public function getChildren(): ?string - { - return $this->children; - } - /** * {@inheritdoc} */ @@ -63,4 +51,16 @@ class ChildrenAggregation extends AbstractAggregation return ['type' => $this->getChildren()]; } + + public function getChildren(): ?string + { + return $this->children; + } + + public function setChildren(?string $children): static + { + $this->children = $children; + + return $this; + } } diff --git a/src/Aggregation/Bucketing/CompositeAggregation.php b/src/Aggregation/Bucketing/CompositeAggregation.php index ad4df1a..e24e734 100644 --- a/src/Aggregation/Bucketing/CompositeAggregation.php +++ b/src/Aggregation/Bucketing/CompositeAggregation.php @@ -54,7 +54,7 @@ class CompositeAggregation extends AbstractAggregation $array = is_array($array) ? array_merge($array, $agg->getParameters()) : $array; $this->sources[] = [ - $agg->getName() => [ $agg->getType() => $array ] + $agg->getName() => [$agg->getType() => $array], ]; return $this; @@ -88,6 +88,11 @@ class CompositeAggregation extends AbstractAggregation return 'composite'; } + public function getSize(): ?int + { + return $this->size; + } + public function setSize(int $size): static { $this->size = $size; @@ -95,9 +100,9 @@ class CompositeAggregation extends AbstractAggregation return $this; } - public function getSize(): ?int + public function getAfter(): ?array { - return $this->size; + return $this->after; } public function setAfter(array $after): static @@ -106,9 +111,4 @@ class CompositeAggregation extends AbstractAggregation return $this; } - - public function getAfter(): ?array - { - return $this->after; - } } diff --git a/src/Aggregation/Bucketing/DateHistogramAggregation.php b/src/Aggregation/Bucketing/DateHistogramAggregation.php index f78f3eb..f659c23 100644 --- a/src/Aggregation/Bucketing/DateHistogramAggregation.php +++ b/src/Aggregation/Bucketing/DateHistogramAggregation.php @@ -38,18 +38,6 @@ class DateHistogramAggregation extends AbstractAggregation $this->setFormat($format); } - public function getInterval(): ?string - { - return $this->interval; - } - - public function setInterval(?string $interval): static - { - $this->interval = $interval; - - return $this; - } - public function setFormat(?string $format): static { $this->format = $format; @@ -85,4 +73,16 @@ class DateHistogramAggregation extends AbstractAggregation return $out; } + + public function getInterval(): ?string + { + return $this->interval; + } + + public function setInterval(?string $interval): static + { + $this->interval = $interval; + + return $this; + } } diff --git a/src/Aggregation/Bucketing/DateRangeAggregation.php b/src/Aggregation/Bucketing/DateRangeAggregation.php index 77829f1..c7cf11d 100644 --- a/src/Aggregation/Bucketing/DateRangeAggregation.php +++ b/src/Aggregation/Bucketing/DateRangeAggregation.php @@ -27,8 +27,13 @@ class DateRangeAggregation extends AbstractAggregation private array $ranges = []; private bool $keyed = false; - public function __construct(string $name, ?string $field = null, ?string $format = null, array $ranges = [], bool $keyed = false) - { + public function __construct( + string $name, + ?string $field = null, + ?string $format = null, + array $ranges = [], + bool $keyed = false + ) { parent::__construct($name); $this->setField($field); @@ -54,16 +59,6 @@ class DateRangeAggregation extends AbstractAggregation return $this; } - public function getFormat(): ?string - { - return $this->format; - } - - public function setFormat(?string $format): void - { - $this->format = $format; - } - /** * Add range to aggregation. * @@ -112,6 +107,16 @@ class DateRangeAggregation extends AbstractAggregation throw new \LogicException('Date range aggregation must have field, format set and range added.'); } + public function getFormat(): ?string + { + return $this->format; + } + + public function setFormat(?string $format): void + { + $this->format = $format; + } + /** * {@inheritdoc} */ diff --git a/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php b/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php index 461c165..166faa5 100644 --- a/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php +++ b/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php @@ -44,24 +44,6 @@ class DiversifiedSamplerAggregation extends AbstractAggregation $this->setShardSize($shardSize); } - /** - * @return mixed - */ - public function getShardSize() - { - return $this->shardSize; - } - - /** - * @return $this - */ - public function setShardSize(mixed $shardSize) - { - $this->shardSize = $shardSize; - - return $this; - } - /** * @inheritdoc */ @@ -82,4 +64,22 @@ class DiversifiedSamplerAggregation extends AbstractAggregation ] ); } + + /** + * @return mixed + */ + public function getShardSize() + { + return $this->shardSize; + } + + /** + * @return $this + */ + public function setShardSize(mixed $shardSize) + { + $this->shardSize = $shardSize; + + return $this; + } } diff --git a/src/Aggregation/Bucketing/FilterAggregation.php b/src/Aggregation/Bucketing/FilterAggregation.php index d5f936c..0a751d8 100644 --- a/src/Aggregation/Bucketing/FilterAggregation.php +++ b/src/Aggregation/Bucketing/FilterAggregation.php @@ -39,41 +39,41 @@ class FilterAggregation extends AbstractAggregation } /** - * @return $this + * {@inheritdoc} */ - public function setFilter(BuilderInterface $filter): static + public function setField($field): void { - $this->filter = $filter; - - return $this; + throw new \LogicException("Filter aggregation, doesn't support `field` parameter"); } /** - * Returns a filter. + * {@inheritdoc} */ - public function getFilter(): ?BuilderInterface + public function getArray(): array|\stdClass { - return $this->filter; + if (!$this->filter) { + throw new \LogicException(sprintf('Filter aggregation `%s` has no filter added', $this->getName())); + } + + return $this->getFilter()->toArray(); } /** - * {@inheritdoc} + * Returns a filter. */ - public function setField($field): void + public function getFilter(): ?BuilderInterface { - throw new \LogicException("Filter aggregation, doesn't support `field` parameter"); + return $this->filter; } /** - * {@inheritdoc} + * @return $this */ - public function getArray(): array|\stdClass + public function setFilter(BuilderInterface $filter): static { - if (!$this->filter) { - throw new \LogicException(sprintf('Filter aggregation `%s` has no filter added', $this->getName())); - } + $this->filter = $filter; - return $this->getFilter()->toArray(); + return $this; } /** diff --git a/src/Aggregation/Bucketing/FiltersAggregation.php b/src/Aggregation/Bucketing/FiltersAggregation.php index 72fee22..9809fd6 100644 --- a/src/Aggregation/Bucketing/FiltersAggregation.php +++ b/src/Aggregation/Bucketing/FiltersAggregation.php @@ -34,9 +34,9 @@ class FiltersAggregation extends AbstractAggregation /** * Inner aggregations container init. * - * @param string $name + * @param string $name * @param BuilderInterface[] $filters - * @param bool $anonymous + * @param bool $anonymous */ public function __construct(string $name, array $filters = [], bool $anonymous = false) { diff --git a/src/Aggregation/Bucketing/GeoDistanceAggregation.php b/src/Aggregation/Bucketing/GeoDistanceAggregation.php index 7432a5e..53c0e54 100644 --- a/src/Aggregation/Bucketing/GeoDistanceAggregation.php +++ b/src/Aggregation/Bucketing/GeoDistanceAggregation.php @@ -33,13 +33,19 @@ class GeoDistanceAggregation extends AbstractAggregation * * @param string $name * @param string $field - * @param mixed $origin - * @param array $ranges + * @param mixed $origin + * @param array $ranges * @param string $unit * @param string $distanceType */ - public function __construct(string $name, ?string $field = null, mixed $origin = null, array $ranges = [], ?string $unit = null, ?string $distanceType = null) - { + public function __construct( + string $name, + ?string $field = null, + mixed $origin = null, + array $ranges = [], + ?string $unit = null, + ?string $distanceType = null + ) { parent::__construct($name); $this->setField($field); @@ -54,47 +60,6 @@ class GeoDistanceAggregation extends AbstractAggregation $this->setDistanceType($distanceType); } - public function getOrigin(): ?string - { - return $this->origin; - } - - public function setOrigin(mixed $origin): static - { - $this->origin = $origin; - - return $this; - } - - public function getDistanceType(): ?string - { - return $this->distanceType; - } - - public function setDistanceType(?string $distanceType): static - { - $this->distanceType = $distanceType; - - return $this; - } - - public function getUnit(): ?string - { - return $this->unit; - } - - /** - * @param string $unit - * - * @return $this - */ - public function setUnit(?string $unit): static - { - $this->unit = $unit; - - return $this; - } - /** * Add range to aggregation. * @@ -151,6 +116,47 @@ class GeoDistanceAggregation extends AbstractAggregation return $data; } + public function getOrigin(): ?string + { + return $this->origin; + } + + public function setOrigin(mixed $origin): static + { + $this->origin = $origin; + + return $this; + } + + public function getUnit(): ?string + { + return $this->unit; + } + + /** + * @param string $unit + * + * @return $this + */ + public function setUnit(?string $unit): static + { + $this->unit = $unit; + + return $this; + } + + public function getDistanceType(): ?string + { + return $this->distanceType; + } + + public function setDistanceType(?string $distanceType): static + { + $this->distanceType = $distanceType; + + return $this; + } + /** * {@inheritdoc} */ diff --git a/src/Aggregation/Bucketing/GeoHashGridAggregation.php b/src/Aggregation/Bucketing/GeoHashGridAggregation.php index 675ee62..7b7b253 100644 --- a/src/Aggregation/Bucketing/GeoHashGridAggregation.php +++ b/src/Aggregation/Bucketing/GeoHashGridAggregation.php @@ -43,9 +43,9 @@ class GeoHashGridAggregation extends AbstractAggregation * * @param string $name * @param string $field - * @param int $precision - * @param int $size - * @param int $shardSize + * @param int $precision + * @param int $size + * @param int $shardSize */ public function __construct(string $name, $field = null, $precision = null, $size = null, $shardSize = null) { @@ -57,6 +57,34 @@ class GeoHashGridAggregation extends AbstractAggregation $this->setShardSize($shardSize); } + /** + * {@inheritdoc} + */ + public function getArray(): array|\stdClass + { + $data = []; + + if ($this->getField()) { + $data['field'] = $this->getField(); + } else { + throw new \LogicException('Geo bounds aggregation must have a field set.'); + } + + if ($this->getPrecision()) { + $data['precision'] = $this->getPrecision(); + } + + if ($this->getSize()) { + $data['size'] = $this->getSize(); + } + + if ($this->getShardSize()) { + $data['shard_size'] = $this->getShardSize(); + } + + return $data; + } + /** * @return int */ @@ -117,34 +145,6 @@ class GeoHashGridAggregation extends AbstractAggregation return $this; } - /** - * {@inheritdoc} - */ - public function getArray(): array|\stdClass - { - $data = []; - - if ($this->getField()) { - $data['field'] = $this->getField(); - } else { - throw new \LogicException('Geo bounds aggregation must have a field set.'); - } - - if ($this->getPrecision()) { - $data['precision'] = $this->getPrecision(); - } - - if ($this->getSize()) { - $data['size'] = $this->getSize(); - } - - if ($this->getShardSize()) { - $data['shard_size'] = $this->getShardSize(); - } - - return $data; - } - /** * {@inheritdoc} */ diff --git a/src/Aggregation/Bucketing/HistogramAggregation.php b/src/Aggregation/Bucketing/HistogramAggregation.php index 2f27e39..18bc1a9 100644 --- a/src/Aggregation/Bucketing/HistogramAggregation.php +++ b/src/Aggregation/Bucketing/HistogramAggregation.php @@ -61,13 +61,13 @@ class HistogramAggregation extends AbstractAggregation * * @param string $name * @param string $field - * @param int $interval - * @param int $minDocCount + * @param int $interval + * @param int $minDocCount * @param string $orderMode * @param string $orderDirection - * @param int $extendedBoundsMin - * @param int $extendedBoundsMax - * @param bool $keyed + * @param int $extendedBoundsMin + * @param int $extendedBoundsMax + * @param bool $keyed */ public function __construct( $name, @@ -90,28 +90,6 @@ class HistogramAggregation extends AbstractAggregation $this->setKeyed($keyed); } - /** - * @return bool - */ - public function isKeyed() - { - return $this->keyed; - } - - /** - * Get response as a hash instead keyed by the buckets keys. - * - * @param bool $keyed - * - * @return $this - */ - public function setKeyed($keyed) - { - $this->keyed = $keyed; - - return $this; - } - /** * Sets buckets ordering. * @@ -129,15 +107,32 @@ class HistogramAggregation extends AbstractAggregation } /** - * @return array + * {@inheritdoc} */ - public function getOrder() + public function getType(): string { - if ($this->orderMode && $this->orderDirection) { - return [$this->orderMode => $this->orderDirection]; - } else { - return null; - } + return 'histogram'; + } + + /** + * {@inheritdoc} + */ + public function getArray(): array|\stdClass + { + $out = array_filter( + [ + 'field' => $this->getField(), + 'interval' => $this->getInterval(), + 'min_doc_count' => $this->getMinDocCount(), + 'extended_bounds' => $this->getExtendedBounds(), + 'keyed' => $this->isKeyed(), + 'order' => $this->getOrder(), + ], + static fn($val): bool => $val || is_numeric($val) + ); + $this->checkRequiredParameters($out, ['field', 'interval']); + + return $out; } /** @@ -208,32 +203,37 @@ class HistogramAggregation extends AbstractAggregation } /** - * {@inheritdoc} + * @return bool */ - public function getType(): string + public function isKeyed() { - return 'histogram'; + return $this->keyed; } /** - * {@inheritdoc} + * Get response as a hash instead keyed by the buckets keys. + * + * @param bool $keyed + * + * @return $this */ - public function getArray(): array|\stdClass + public function setKeyed($keyed) { - $out = array_filter( - [ - 'field' => $this->getField(), - 'interval' => $this->getInterval(), - 'min_doc_count' => $this->getMinDocCount(), - 'extended_bounds' => $this->getExtendedBounds(), - 'keyed' => $this->isKeyed(), - 'order' => $this->getOrder(), - ], - static fn($val): bool => $val || is_numeric($val) - ); - $this->checkRequiredParameters($out, ['field', 'interval']); + $this->keyed = $keyed; - return $out; + return $this; + } + + /** + * @return array + */ + public function getOrder() + { + if ($this->orderMode && $this->orderDirection) { + return [$this->orderMode => $this->orderDirection]; + } else { + return null; + } } /** diff --git a/src/Aggregation/Bucketing/NestedAggregation.php b/src/Aggregation/Bucketing/NestedAggregation.php index fc47fe6..e25cf19 100644 --- a/src/Aggregation/Bucketing/NestedAggregation.php +++ b/src/Aggregation/Bucketing/NestedAggregation.php @@ -41,6 +41,22 @@ class NestedAggregation extends AbstractAggregation $this->setPath($path); } + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'nested'; + } + + /** + * {@inheritdoc} + */ + public function getArray(): array|\stdClass + { + return ['path' => $this->getPath()]; + } + /** * Return path. * @@ -62,20 +78,4 @@ class NestedAggregation extends AbstractAggregation return $this; } - - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'nested'; - } - - /** - * {@inheritdoc} - */ - public function getArray(): array|\stdClass - { - return ['path' => $this->getPath()]; - } } diff --git a/src/Aggregation/Bucketing/ReverseNestedAggregation.php b/src/Aggregation/Bucketing/ReverseNestedAggregation.php index bc66b48..6f334af 100644 --- a/src/Aggregation/Bucketing/ReverseNestedAggregation.php +++ b/src/Aggregation/Bucketing/ReverseNestedAggregation.php @@ -36,45 +36,45 @@ class ReverseNestedAggregation extends AbstractAggregation } /** - * Return path. - * - * @return string + * {@inheritdoc} */ - public function getPath(): ?string + public function getType(): string { - return $this->path; + return 'reverse_nested'; } /** - * @param string $path - * - * @return $this + * {@inheritdoc} */ - public function setPath(?string $path): static + public function getArray(): array|\stdClass { - $this->path = $path; + $output = new \stdClass(); + if ($this->getPath()) { + $output = ['path' => $this->getPath()]; + } - return $this; + return $output; } /** - * {@inheritdoc} + * Return path. + * + * @return string */ - public function getType(): string + public function getPath(): ?string { - return 'reverse_nested'; + return $this->path; } /** - * {@inheritdoc} + * @param string $path + * + * @return $this */ - public function getArray(): array|\stdClass + public function setPath(?string $path): static { - $output = new \stdClass(); - if ($this->getPath()) { - $output = ['path' => $this->getPath()]; - } + $this->path = $path; - return $output; + return $this; } } diff --git a/src/Aggregation/Bucketing/SamplerAggregation.php b/src/Aggregation/Bucketing/SamplerAggregation.php index 73bf0c5..8f63d6d 100644 --- a/src/Aggregation/Bucketing/SamplerAggregation.php +++ b/src/Aggregation/Bucketing/SamplerAggregation.php @@ -34,7 +34,7 @@ class SamplerAggregation extends AbstractAggregation * * @param string $name * @param string $field - * @param int $shardSize + * @param int $shardSize */ public function __construct(string $name, $field = null, $shardSize = null) { @@ -44,26 +44,6 @@ class SamplerAggregation extends AbstractAggregation $this->setShardSize($shardSize); } - /** - * @return int - */ - public function getShardSize() - { - return $this->shardSize; - } - - /** - * @param int $shardSize - * - * @return $this - */ - public function setShardSize($shardSize) - { - $this->shardSize = $shardSize; - - return $this; - } - /** * {@inheritdoc} */ @@ -84,4 +64,24 @@ class SamplerAggregation extends AbstractAggregation ] ); } + + /** + * @return int + */ + public function getShardSize() + { + return $this->shardSize; + } + + /** + * @param int $shardSize + * + * @return $this + */ + public function setShardSize($shardSize) + { + $this->shardSize = $shardSize; + + return $this; + } } diff --git a/src/Aggregation/Matrix/MatrixStatsAggregation.php b/src/Aggregation/Matrix/MatrixStatsAggregation.php index 648df48..263ab47 100644 --- a/src/Aggregation/Matrix/MatrixStatsAggregation.php +++ b/src/Aggregation/Matrix/MatrixStatsAggregation.php @@ -44,34 +44,6 @@ class MatrixStatsAggregation extends AbstractAggregation $this->setMode($mode); } - /** - * @return string - */ - public function getMode() - { - return $this->mode; - } - - /** - * @param string $mode - * - * @return $this - */ - public function setMode($mode) - { - $this->mode = $mode; - - return $this; - } - - /** - * @return array - */ - public function getMissing() - { - return $this->missing; - } - /** * @param array $missing * @@ -110,4 +82,32 @@ class MatrixStatsAggregation extends AbstractAggregation return $out; } + + /** + * @return string + */ + public function getMode() + { + return $this->mode; + } + + /** + * @param string $mode + * + * @return $this + */ + public function setMode($mode) + { + $this->mode = $mode; + + return $this; + } + + /** + * @return array + */ + public function getMissing() + { + return $this->missing; + } } diff --git a/src/Aggregation/Metric/CardinalityAggregation.php b/src/Aggregation/Metric/CardinalityAggregation.php index ab6706f..4959bdb 100644 --- a/src/Aggregation/Metric/CardinalityAggregation.php +++ b/src/Aggregation/Metric/CardinalityAggregation.php @@ -55,6 +55,14 @@ class CardinalityAggregation extends AbstractAggregation return $out; } + /** + * @return int + */ + public function getPrecisionThreshold() + { + return $this->precisionThreshold; + } + /** * @param int $precision * @@ -67,14 +75,6 @@ class CardinalityAggregation extends AbstractAggregation return $this; } - /** - * @return int - */ - public function getPrecisionThreshold() - { - return $this->precisionThreshold; - } - /** * @return bool */ @@ -95,14 +95,6 @@ class CardinalityAggregation extends AbstractAggregation return $this; } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'cardinality'; - } - /** * Checks if required fields are set. * @@ -115,4 +107,12 @@ class CardinalityAggregation extends AbstractAggregation throw new \LogicException('Cardinality aggregation must have field or script set.'); } } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'cardinality'; + } } diff --git a/src/Aggregation/Metric/ExtendedStatsAggregation.php b/src/Aggregation/Metric/ExtendedStatsAggregation.php index b807a97..fa7969c 100644 --- a/src/Aggregation/Metric/ExtendedStatsAggregation.php +++ b/src/Aggregation/Metric/ExtendedStatsAggregation.php @@ -25,12 +25,17 @@ class ExtendedStatsAggregation extends AbstractAggregation use MetricTrait; use ScriptAwareTrait; + /** + * @var int + */ + private $sigma; + /** * Inner aggregations container init. * * @param string $name * @param string $field - * @param int $sigma + * @param int $sigma * @param string $script */ public function __construct(string $name, $field = null, $sigma = null, $script = null) @@ -42,31 +47,6 @@ class ExtendedStatsAggregation extends AbstractAggregation $this->setScript($script); } - /** - * @var int - */ - private $sigma; - - /** - * @return int - */ - public function getSigma() - { - return $this->sigma; - } - - /** - * @param int $sigma - * - * @return $this - */ - public function setSigma($sigma) - { - $this->sigma = $sigma; - - return $this; - } - /** * {@inheritdoc} */ @@ -89,4 +69,24 @@ class ExtendedStatsAggregation extends AbstractAggregation static fn($val): bool => $val || is_numeric($val) ); } + + /** + * @return int + */ + public function getSigma() + { + return $this->sigma; + } + + /** + * @param int $sigma + * + * @return $this + */ + public function setSigma($sigma) + { + $this->sigma = $sigma; + + return $this; + } } diff --git a/src/Aggregation/Metric/GeoBoundsAggregation.php b/src/Aggregation/Metric/GeoBoundsAggregation.php index 3d363ae..ef742fa 100644 --- a/src/Aggregation/Metric/GeoBoundsAggregation.php +++ b/src/Aggregation/Metric/GeoBoundsAggregation.php @@ -36,21 +36,6 @@ class GeoBoundsAggregation extends AbstractAggregation $this->setWrapLongitude($wrapLongitude); } - public function isWrapLongitude(): bool - { - return $this->wrapLongitude; - } - - /** - * @return $this - */ - public function setWrapLongitude(bool $wrapLongitude): static - { - $this->wrapLongitude = $wrapLongitude; - - return $this; - } - /** * {@inheritdoc} */ @@ -68,6 +53,21 @@ class GeoBoundsAggregation extends AbstractAggregation return $data; } + public function isWrapLongitude(): bool + { + return $this->wrapLongitude; + } + + /** + * @return $this + */ + public function setWrapLongitude(bool $wrapLongitude): static + { + $this->wrapLongitude = $wrapLongitude; + + return $this; + } + /** * {@inheritdoc} */ diff --git a/src/Aggregation/Metric/PercentileRanksAggregation.php b/src/Aggregation/Metric/PercentileRanksAggregation.php index 0631d67..bdb1448 100644 --- a/src/Aggregation/Metric/PercentileRanksAggregation.php +++ b/src/Aggregation/Metric/PercentileRanksAggregation.php @@ -40,9 +40,9 @@ class PercentileRanksAggregation extends AbstractAggregation * * @param string $name * @param string $field - * @param array $values + * @param array $values * @param string $script - * @param int $compression + * @param int $compression */ public function __construct(string $name, $field = null, $values = null, $script = null, $compression = null) { @@ -54,6 +54,34 @@ class PercentileRanksAggregation extends AbstractAggregation $this->setCompression($compression); } + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'percentile_ranks'; + } + + /** + * {@inheritdoc} + */ + public function getArray(): array|\stdClass + { + $out = array_filter( + [ + 'field' => $this->getField(), + 'script' => $this->getScript(), + 'values' => $this->getValues(), + 'compression' => $this->getCompression(), + ], + static fn($val): bool => $val || is_numeric($val) + ); + + $this->isRequiredParametersSet($out); + + return $out; + } + /** * @return array */ @@ -94,34 +122,6 @@ class PercentileRanksAggregation extends AbstractAggregation return $this; } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'percentile_ranks'; - } - - /** - * {@inheritdoc} - */ - public function getArray(): array|\stdClass - { - $out = array_filter( - [ - 'field' => $this->getField(), - 'script' => $this->getScript(), - 'values' => $this->getValues(), - 'compression' => $this->getCompression(), - ], - static fn($val): bool => $val || is_numeric($val) - ); - - $this->isRequiredParametersSet($out); - - return $out; - } - /** * * @return bool diff --git a/src/Aggregation/Metric/PercentilesAggregation.php b/src/Aggregation/Metric/PercentilesAggregation.php index ede8b5e..3c2b5f1 100644 --- a/src/Aggregation/Metric/PercentilesAggregation.php +++ b/src/Aggregation/Metric/PercentilesAggregation.php @@ -40,9 +40,9 @@ class PercentilesAggregation extends AbstractAggregation * * @param string $name * @param string $field - * @param array $percents + * @param array $percents * @param string $script - * @param int $compression + * @param int $compression */ public function __construct(string $name, $field = null, $percents = null, $script = null, $compression = null) { @@ -55,23 +55,31 @@ class PercentilesAggregation extends AbstractAggregation } /** - * @return array + * {@inheritdoc} */ - public function getPercents() + public function getType(): string { - return $this->percents; + return 'percentiles'; } /** - * @param array $percents - * - * @return $this + * {@inheritdoc} */ - public function setPercents($percents) + public function getArray(): array|\stdClass { - $this->percents = $percents; + $out = array_filter( + [ + 'compression' => $this->getCompression(), + 'percents' => $this->getPercents(), + 'field' => $this->getField(), + 'script' => $this->getScript(), + ], + static fn($val): bool => $val || is_numeric($val) + ); - return $this; + $this->isRequiredParametersSet($out); + + return $out; } /** @@ -95,31 +103,23 @@ class PercentilesAggregation extends AbstractAggregation } /** - * {@inheritdoc} + * @return array */ - public function getType(): string + public function getPercents() { - return 'percentiles'; + return $this->percents; } /** - * {@inheritdoc} + * @param array $percents + * + * @return $this */ - public function getArray(): array|\stdClass + public function setPercents($percents) { - $out = array_filter( - [ - 'compression' => $this->getCompression(), - 'percents' => $this->getPercents(), - 'field' => $this->getField(), - 'script' => $this->getScript(), - ], - static fn($val): bool => $val || is_numeric($val) - ); - - $this->isRequiredParametersSet($out); + $this->percents = $percents; - return $out; + return $this; } /** diff --git a/src/Aggregation/Metric/ScriptedMetricAggregation.php b/src/Aggregation/Metric/ScriptedMetricAggregation.php index 653dfce..712d76e 100644 --- a/src/Aggregation/Metric/ScriptedMetricAggregation.php +++ b/src/Aggregation/Metric/ScriptedMetricAggregation.php @@ -13,7 +13,6 @@ namespace ONGR\ElasticsearchDSL\Aggregation\Metric; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; use ONGR\ElasticsearchDSL\Aggregation\Type\MetricTrait; -use ONGR\ElasticsearchDSL\ScriptAwareTrait; /** * Class representing StatsAggregation. @@ -59,7 +58,6 @@ class ScriptedMetricAggregation extends AbstractAggregation $combineScript = null, $reduceScript = null ) { - parent::__construct($name); $this->setInitScript($initScript); @@ -76,6 +74,21 @@ class ScriptedMetricAggregation extends AbstractAggregation return 'scripted_metric'; } + /** + * {@inheritdoc} + */ + public function getArray(): array|\stdClass + { + return array_filter( + [ + 'init_script' => $this->getInitScript(), + 'map_script' => $this->getMapScript(), + 'combine_script' => $this->getCombineScript(), + 'reduce_script' => $this->getReduceScript(), + ] + ); + } + /** * @return mixed */ @@ -147,19 +160,4 @@ class ScriptedMetricAggregation extends AbstractAggregation return $this; } - - /** - * {@inheritdoc} - */ - public function getArray(): array|\stdClass - { - return array_filter( - [ - 'init_script' => $this->getInitScript(), - 'map_script' => $this->getMapScript(), - 'combine_script' => $this->getCombineScript(), - 'reduce_script' => $this->getReduceScript(), - ] - ); - } } diff --git a/src/Aggregation/Metric/TopHitsAggregation.php b/src/Aggregation/Metric/TopHitsAggregation.php index aed69bc..3c1b8f7 100644 --- a/src/Aggregation/Metric/TopHitsAggregation.php +++ b/src/Aggregation/Metric/TopHitsAggregation.php @@ -57,16 +57,42 @@ class TopHitsAggregation extends AbstractAggregation } } - public function getFrom(): ?int + public function addSort(BuilderInterface $sort): void { - return $this->from; + $this->sorts[] = $sort; } - public function setFrom($from): static + /** + * {@inheritdoc} + */ + public function getType(): string { - $this->from = $from; + return 'top_hits'; + } - return $this; + /** + * {@inheritdoc} + */ + public function getArray(): array|\stdClass + { + $sortsOutput = null; + $addedSorts = array_filter($this->getSorts()); + if ($addedSorts) { + foreach ($addedSorts as $sort) { + $sortsOutput[] = $sort->toArray(); + } + } + + $output = array_filter( + [ + 'sort' => $sortsOutput, + 'size' => $this->getSize(), + 'from' => $this->getFrom(), + ], + static fn($val): bool => $val || is_array($val) || ($val || is_numeric($val)) + ); + + return empty($output) ? new \stdClass() : $output; } /** @@ -89,9 +115,9 @@ class TopHitsAggregation extends AbstractAggregation return $this; } - public function addSort(BuilderInterface $sort): void + public function getSize(): ?int { - $this->sorts[] = $sort; + return $this->size; } public function setSize(int $size): static @@ -101,41 +127,15 @@ class TopHitsAggregation extends AbstractAggregation return $this; } - public function getSize(): ?int - { - return $this->size; - } - - /** - * {@inheritdoc} - */ - public function getType(): string + public function getFrom(): ?int { - return 'top_hits'; + return $this->from; } - /** - * {@inheritdoc} - */ - public function getArray(): array|\stdClass + public function setFrom($from): static { - $sortsOutput = null; - $addedSorts = array_filter($this->getSorts()); - if ($addedSorts) { - foreach ($addedSorts as $sort) { - $sortsOutput[] = $sort->toArray(); - } - } - - $output = array_filter( - [ - 'sort' => $sortsOutput, - 'size' => $this->getSize(), - 'from' => $this->getFrom(), - ], - static fn($val): bool => $val || is_array($val) || ($val || is_numeric($val)) - ); + $this->from = $from; - return empty($output) ? new \stdClass() : $output; + return $this; } } diff --git a/src/Aggregation/Pipeline/AbstractPipelineAggregation.php b/src/Aggregation/Pipeline/AbstractPipelineAggregation.php index c701bac..c483a15 100644 --- a/src/Aggregation/Pipeline/AbstractPipelineAggregation.php +++ b/src/Aggregation/Pipeline/AbstractPipelineAggregation.php @@ -24,6 +24,14 @@ abstract class AbstractPipelineAggregation extends AbstractAggregation $this->setBucketsPath($bucketsPath); } + /** + * {@inheritdoc} + */ + public function getArray(): array|\stdClass + { + return ['buckets_path' => $this->getBucketsPath()]; + } + /** * @return string */ @@ -43,12 +51,4 @@ abstract class AbstractPipelineAggregation extends AbstractAggregation return $this; } - - /** - * {@inheritdoc} - */ - public function getArray(): array|\stdClass - { - return ['buckets_path' => $this->getBucketsPath()]; - } } diff --git a/src/Aggregation/Pipeline/BucketScriptAggregation.php b/src/Aggregation/Pipeline/BucketScriptAggregation.php index 8cc36fa..9a1ebf6 100644 --- a/src/Aggregation/Pipeline/BucketScriptAggregation.php +++ b/src/Aggregation/Pipeline/BucketScriptAggregation.php @@ -26,21 +26,6 @@ class BucketScriptAggregation extends AbstractPipelineAggregation $this->setScript($script); } - /** - * @return string - */ - public function getScript(): mixed - { - return $this->script; - } - - public function setScript(mixed $script): static - { - $this->script = $script; - - return $this; - } - /** * {@inheritdoc} */ @@ -68,4 +53,19 @@ class BucketScriptAggregation extends AbstractPipelineAggregation 'script' => $this->getScript(), ]; } + + /** + * @return string + */ + public function getScript(): mixed + { + return $this->script; + } + + public function setScript(mixed $script): static + { + $this->script = $script; + + return $this; + } } diff --git a/src/Aggregation/Pipeline/BucketSortAggregation.php b/src/Aggregation/Pipeline/BucketSortAggregation.php index f6f06a8..8aae9ff 100644 --- a/src/Aggregation/Pipeline/BucketSortAggregation.php +++ b/src/Aggregation/Pipeline/BucketSortAggregation.php @@ -11,7 +11,6 @@ namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; -use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\Sort\FieldSort; /** @@ -25,18 +24,13 @@ class BucketSortAggregation extends AbstractPipelineAggregation /** * @param string $name - * @param string $bucketsPath + * @param string $bucketsPath */ public function __construct(string $name, $bucketsPath = null) { parent::__construct($name, $bucketsPath); } - public function getSort(): array - { - return $this->sort; - } - /** * @return self */ @@ -45,18 +39,6 @@ class BucketSortAggregation extends AbstractPipelineAggregation $this->sort[] = $sort->toArray(); } - /** - * @param string $sort - * - * @return $this - */ - public function setSort(array $sort) - { - $this->sort = $sort; - - return $this; - } - /** * {@inheritdoc} */ @@ -72,9 +54,26 @@ class BucketSortAggregation extends AbstractPipelineAggregation { return array_filter( [ - 'buckets_path' => $this->getBucketsPath(), - 'sort' => $this->getSort(), + 'buckets_path' => $this->getBucketsPath(), + 'sort' => $this->getSort(), ] ); } + + public function getSort(): array + { + return $this->sort; + } + + /** + * @param string $sort + * + * @return $this + */ + public function setSort(array $sort) + { + $this->sort = $sort; + + return $this; + } } diff --git a/src/Aggregation/Pipeline/PercentilesBucketAggregation.php b/src/Aggregation/Pipeline/PercentilesBucketAggregation.php index f6b4f9b..3ae722c 100644 --- a/src/Aggregation/Pipeline/PercentilesBucketAggregation.php +++ b/src/Aggregation/Pipeline/PercentilesBucketAggregation.php @@ -28,6 +28,20 @@ class PercentilesBucketAggregation extends AbstractPipelineAggregation return 'percentiles_bucket'; } + /** + * {@inheritdoc} + */ + public function getArray(): array|\stdClass + { + $data = ['buckets_path' => $this->getBucketsPath()]; + + if ($this->getPercents()) { + $data['percents'] = $this->getPercents(); + } + + return $data; + } + /** * @return mixed[]|null */ @@ -45,18 +59,4 @@ class PercentilesBucketAggregation extends AbstractPipelineAggregation return $this; } - - /** - * {@inheritdoc} - */ - public function getArray(): array|\stdClass - { - $data = ['buckets_path' => $this->getBucketsPath()]; - - if ($this->getPercents()) { - $data['percents'] = $this->getPercents(); - } - - return $data; - } } diff --git a/src/BuilderBag.php b/src/BuilderBag.php index 3a23a45..f521c8f 100644 --- a/src/BuilderBag.php +++ b/src/BuilderBag.php @@ -33,10 +33,8 @@ class BuilderBag /** * Adds a builder. - * - * @return string */ - public function add(BuilderInterface $builder) + public function add(BuilderInterface $builder): string { $name = method_exists($builder, 'getName') ? $builder->getName() : bin2hex(random_bytes(30)); @@ -47,22 +45,16 @@ class BuilderBag /** * Checks if builder exists by a specific name. - * - * @param string $name Builder name. - * - * @return bool */ - public function has($name) + public function has(string $name): bool { return isset($this->bag[$name]); } /** * Removes a builder by name. - * - * @param string $name Builder name. */ - public function remove($name): void + public function remove(string $name): void { unset($this->bag[$name]); } @@ -77,30 +69,10 @@ class BuilderBag /** * Returns a builder by name. - * - * @param string $name Builder name. - * - * @return BuilderInterface */ - public function get($name) + public function get(string $name): ?BuilderInterface { - return $this->bag[$name]; - } - - /** - * Returns all builders contained. - * - * @param string|null $type Builder type. - * - * @return BuilderInterface[] - */ - public function all($type = null): array - { - return array_filter( - $this->bag, - /** @var BuilderInterface $builder */ - static fn(BuilderInterface $builder): bool => $type === null || $builder->getType() == $type - ); + return $this->bag[$name] ?? null; } /** @@ -115,4 +87,18 @@ class BuilderBag return $output; } + + /** + * Returns all builders contained. + * + * @return BuilderInterface[] + */ + public function all(mixed $type = null): array + { + return array_filter( + $this->bag, + /** @var BuilderInterface $builder */ + static fn(BuilderInterface $builder): bool => $type === null || $builder->getType() == $type + ); + } } diff --git a/src/InnerHit/NestedInnerHit.php b/src/InnerHit/NestedInnerHit.php index 467fdf6..bd1ea56 100644 --- a/src/InnerHit/NestedInnerHit.php +++ b/src/InnerHit/NestedInnerHit.php @@ -42,16 +42,18 @@ class NestedInnerHit implements NamedBuilderInterface } } - public function getPath(): string - { - return $this->path; - } - - public function setPath(string $path): static + /** + * {@inheritdoc} + */ + public function toArray(): array|\stdClass { - $this->path = $path; + $out = $this->getSearch() ? $this->getSearch()->toArray() : new \stdClass(); - return $this; + return [ + $this->getPathType() => [ + $this->getPath() => $out, + ], + ]; } public function getSearch(): ?Search @@ -67,36 +69,34 @@ class NestedInnerHit implements NamedBuilderInterface } /** - * {@inheritdoc} + * Returns 'path' for nested and 'type' for parent inner hits */ - public function getType(): string + private function getPathType(): ?string { - return 'nested'; + return match ($this->getType()) { + 'nested' => 'path', + 'parent' => 'type', + default => null, + }; } /** * {@inheritdoc} */ - public function toArray(): array|\stdClass + public function getType(): string { - $out = $this->getSearch() ? $this->getSearch()->toArray() : new \stdClass(); + return 'nested'; + } - return [ - $this->getPathType() => [ - $this->getPath() => $out, - ], - ]; + public function getPath(): string + { + return $this->path; } - /** - * Returns 'path' for nested and 'type' for parent inner hits - */ - private function getPathType(): ?string + public function setPath(string $path): static { - return match ($this->getType()) { - 'nested' => 'path', - 'parent' => 'type', - default => null, - }; + $this->path = $path; + + return $this; } } diff --git a/src/ParametersTrait.php b/src/ParametersTrait.php index 08b72a4..1e16940 100644 --- a/src/ParametersTrait.php +++ b/src/ParametersTrait.php @@ -18,14 +18,6 @@ trait ParametersTrait { private array $parameters = []; - /** - * Checks if parameter exists. - */ - public function hasParameter(string $name): bool - { - return isset($this->parameters[$name]); - } - /** * Removes parameter. */ @@ -36,6 +28,14 @@ trait ParametersTrait } } + /** + * Checks if parameter exists. + */ + public function hasParameter(string $name): bool + { + return isset($this->parameters[$name]); + } + /** * Returns one parameter by it's name. */ @@ -52,11 +52,6 @@ trait ParametersTrait return $this->parameters; } - public function addParameter(string $name, mixed $value): void - { - $this->parameters[$name] = $value; - } - public function setParameters(array $parameters): static { $this->parameters = $parameters; @@ -64,6 +59,11 @@ trait ParametersTrait return $this; } + public function addParameter(string $name, mixed $value): void + { + $this->parameters[$name] = $value; + } + /** * Returns given array merged with parameters. */ diff --git a/src/Query/Compound/BoolQuery.php b/src/Query/Compound/BoolQuery.php index f4647b9..648a2ec 100644 --- a/src/Query/Compound/BoolQuery.php +++ b/src/Query/Compound/BoolQuery.php @@ -44,34 +44,12 @@ class BoolQuery implements BuilderInterface } } - /** - * Returns the query instances (by bool type). - * - * @param string|null $boolType - * - * @return array - */ - public function getQueries($boolType = null) - { - if ($boolType === null) { - $queries = []; - - foreach ($this->container as $item) { - $queries = array_merge($queries, $item); - } - - return $queries; - } - - return $this->container[$boolType] ?? []; - } - /** * Add BuilderInterface object to bool operator. * * @param BuilderInterface $query Query add to the bool. - * @param string $type Bool type. Example: must, must_not, should. - * @param string $key Key that indicates a builder id. + * @param string $type Bool type. Example: must, must_not, should. + * @param string $key Key that indicates a builder id. * * @return string Key of added builder. * @@ -92,13 +70,35 @@ class BoolQuery implements BuilderInterface return $key; } + /** + * Returns the query instances (by bool type). + * + * @param string|null $boolType + * + * @return array + */ + public function getQueries($boolType = null) + { + if ($boolType === null) { + $queries = []; + + foreach ($this->container as $item) { + $queries = array_merge($queries, $item); + } + + return $queries; + } + + return $this->container[$boolType] ?? []; + } + /** * {@inheritdoc} */ public function toArray(): array|\stdClass { if (count($this->container) === 1 && isset($this->container[self::MUST]) - && count($this->container[self::MUST]) === 1) { + && count($this->container[self::MUST]) === 1) { $query = reset($this->container[self::MUST]); return $query->toArray(); diff --git a/src/Query/Compound/BoostingQuery.php b/src/Query/Compound/BoostingQuery.php index 46830f9..9a3ae69 100644 --- a/src/Query/Compound/BoostingQuery.php +++ b/src/Query/Compound/BoostingQuery.php @@ -21,18 +21,13 @@ use ONGR\ElasticsearchDSL\BuilderInterface; class BoostingQuery implements BuilderInterface { /** - * @param int|float $negativeBoost + * @param int|float $negativeBoost */ - public function __construct(private readonly BuilderInterface $positive, private readonly BuilderInterface $negative, private $negativeBoost) - { - } - - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'boosting'; + public function __construct( + private readonly BuilderInterface $positive, + private readonly BuilderInterface $negative, + private $negativeBoost + ) { } /** @@ -48,4 +43,12 @@ class BoostingQuery implements BuilderInterface return [$this->getType() => $query]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'boosting'; + } } diff --git a/src/Query/Compound/ConstantScoreQuery.php b/src/Query/Compound/ConstantScoreQuery.php index dd1d372..196d0eb 100644 --- a/src/Query/Compound/ConstantScoreQuery.php +++ b/src/Query/Compound/ConstantScoreQuery.php @@ -28,14 +28,6 @@ class ConstantScoreQuery implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'constant_score'; - } - /** * {@inheritdoc} */ @@ -49,4 +41,12 @@ class ConstantScoreQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'constant_score'; + } } diff --git a/src/Query/Compound/DisMaxQuery.php b/src/Query/Compound/DisMaxQuery.php index bc00c14..89de91d 100644 --- a/src/Query/Compound/DisMaxQuery.php +++ b/src/Query/Compound/DisMaxQuery.php @@ -49,14 +49,6 @@ class DisMaxQuery implements BuilderInterface return $this; } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'dis_max'; - } - /** * {@inheritdoc} */ @@ -71,4 +63,12 @@ class DisMaxQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'dis_max'; + } } diff --git a/src/Query/Compound/FunctionScoreQuery.php b/src/Query/Compound/FunctionScoreQuery.php index bdd93c5..7b83876 100644 --- a/src/Query/Compound/FunctionScoreQuery.php +++ b/src/Query/Compound/FunctionScoreQuery.php @@ -86,6 +86,29 @@ class FunctionScoreQuery implements BuilderInterface } } + /** + * {@inheritdoc} + */ + public function toArray(): array|\stdClass + { + $query = [ + 'query' => $this->query->toArray(), + 'functions' => $this->functions, + ]; + + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'function_score'; + } + /** * Add decay function to function score. Weight and query are optional. * @@ -206,27 +229,4 @@ class FunctionScoreQuery implements BuilderInterface return $this; } - - /** - * {@inheritdoc} - */ - public function toArray(): array|\stdClass - { - $query = [ - 'query' => $this->query->toArray(), - 'functions' => $this->functions, - ]; - - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } - - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'function_score'; - } } diff --git a/src/Query/FullText/CommonTermsQuery.php b/src/Query/FullText/CommonTermsQuery.php index 5b365cf..4c35bb0 100644 --- a/src/Query/FullText/CommonTermsQuery.php +++ b/src/Query/FullText/CommonTermsQuery.php @@ -32,14 +32,6 @@ class CommonTermsQuery implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'common'; - } - /** * {@inheritdoc} */ @@ -55,4 +47,12 @@ class CommonTermsQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'common'; + } } diff --git a/src/Query/FullText/MatchQuery.php b/src/Query/FullText/MatchQuery.php index e8dd8ee..1284311 100644 --- a/src/Query/FullText/MatchQuery.php +++ b/src/Query/FullText/MatchQuery.php @@ -32,14 +32,6 @@ class MatchQuery implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'match'; - } - /** * {@inheritdoc} */ @@ -55,4 +47,12 @@ class MatchQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'match'; + } } diff --git a/src/Query/FullText/MultiMatchQuery.php b/src/Query/FullText/MultiMatchQuery.php index c6f538f..cece03c 100644 --- a/src/Query/FullText/MultiMatchQuery.php +++ b/src/Query/FullText/MultiMatchQuery.php @@ -34,14 +34,6 @@ class MultiMatchQuery implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'multi_match'; - } - /** * {@inheritdoc} */ @@ -58,4 +50,12 @@ class MultiMatchQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'multi_match'; + } } diff --git a/src/Query/FullText/QueryStringQuery.php b/src/Query/FullText/QueryStringQuery.php index cfbf323..a4f176e 100644 --- a/src/Query/FullText/QueryStringQuery.php +++ b/src/Query/FullText/QueryStringQuery.php @@ -31,14 +31,6 @@ class QueryStringQuery implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'query_string'; - } - /** * {@inheritdoc} */ @@ -52,4 +44,12 @@ class QueryStringQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'query_string'; + } } diff --git a/src/Query/FullText/SimpleQueryStringQuery.php b/src/Query/FullText/SimpleQueryStringQuery.php index 8d48f35..bc66c73 100644 --- a/src/Query/FullText/SimpleQueryStringQuery.php +++ b/src/Query/FullText/SimpleQueryStringQuery.php @@ -31,14 +31,6 @@ class SimpleQueryStringQuery implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'simple_query_string'; - } - /** * {@inheritdoc} */ @@ -52,4 +44,12 @@ class SimpleQueryStringQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'simple_query_string'; + } } diff --git a/src/Query/Geo/GeoBoundingBoxQuery.php b/src/Query/Geo/GeoBoundingBoxQuery.php index a5f85a1..2ef6321 100644 --- a/src/Query/Geo/GeoBoundingBoxQuery.php +++ b/src/Query/Geo/GeoBoundingBoxQuery.php @@ -25,7 +25,7 @@ class GeoBoundingBoxQuery implements BuilderInterface /** * @param string $field - * @param array $values + * @param array $values */ public function __construct(private $field, private $values, array $parameters = []) { @@ -35,19 +35,19 @@ class GeoBoundingBoxQuery implements BuilderInterface /** * {@inheritdoc} */ - public function getType(): string + public function toArray(): array|\stdClass { - return 'geo_bounding_box'; + return [ + $this->getType() => $this->processArray([$this->field => $this->points()]), + ]; } /** * {@inheritdoc} */ - public function toArray(): array|\stdClass + public function getType(): string { - return [ - $this->getType() => $this->processArray([$this->field => $this->points()]) - ]; + return 'geo_bounding_box'; } /** diff --git a/src/Query/Geo/GeoDistanceQuery.php b/src/Query/Geo/GeoDistanceQuery.php index 8156b54..508397e 100644 --- a/src/Query/Geo/GeoDistanceQuery.php +++ b/src/Query/Geo/GeoDistanceQuery.php @@ -23,23 +23,15 @@ class GeoDistanceQuery implements BuilderInterface { use ParametersTrait; - /** - * @param string $field - * @param string $distance - */ - public function __construct(private $field, private $distance, private readonly mixed $location, array $parameters = []) - { + public function __construct( + private string $field, + private string $distance, + private readonly mixed $location, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'geo_distance'; - } - /** * {@inheritdoc} */ @@ -53,4 +45,12 @@ class GeoDistanceQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'geo_distance'; + } } diff --git a/src/Query/Geo/GeoPolygonQuery.php b/src/Query/Geo/GeoPolygonQuery.php index 27eeab7..9389b33 100644 --- a/src/Query/Geo/GeoPolygonQuery.php +++ b/src/Query/Geo/GeoPolygonQuery.php @@ -34,19 +34,19 @@ class GeoPolygonQuery implements BuilderInterface /** * {@inheritdoc} */ - public function getType(): string + public function toArray(): array|\stdClass { - return 'geo_polygon'; + $query = [$this->field => ['points' => $this->points]]; + $output = $this->processArray($query); + + return [$this->getType() => $output]; } /** * {@inheritdoc} */ - public function toArray(): array|\stdClass + public function getType(): string { - $query = [$this->field => ['points' => $this->points]]; - $output = $this->processArray($query); - - return [$this->getType() => $output]; + return 'geo_polygon'; } } diff --git a/src/Query/Geo/GeoShapeQuery.php b/src/Query/Geo/GeoShapeQuery.php index 212cc4a..356022c 100644 --- a/src/Query/Geo/GeoShapeQuery.php +++ b/src/Query/Geo/GeoShapeQuery.php @@ -24,11 +24,8 @@ class GeoShapeQuery implements BuilderInterface use ParametersTrait; final public const INTERSECTS = 'intersects'; - final public const DISJOINT = 'disjoint'; - final public const WITHIN = 'within'; - final public const CONTAINS = 'contains'; private array $fields = []; @@ -46,17 +43,36 @@ class GeoShapeQuery implements BuilderInterface return 'geo_shape'; } + public function addGeometry( + string $field, + mixed $geometry, + string $relation = self::INTERSECTS, + array $parameters = [] + ): static { + $this->fields[$field] = [ + 'shape' => $geometry, + 'relation' => $relation, + ] + $parameters; + + return $this; + } + /** * Add geo-shape provided filter. * - * @param string $field Field name. - * @param string $type Shape type. - * @param array $coordinates Shape coordinates. - * @param string $relation Spatial relation. - * @param array $parameters Additional parameters. + * @param string $field Field name. + * @param string $type Shape type. + * @param array $coordinates Shape coordinates. + * @param string $relation Spatial relation. + * @param array $parameters Additional parameters. */ - public function addShape($field, $type, array $coordinates, $relation = self::INTERSECTS, array $parameters = []): void - { + public function addShape( + string $field, + string $type, + array $coordinates, + $relation = self::INTERSECTS, + array $parameters = [] + ): static { // TODO: remove this in the next major version if (is_array($relation)) { $parameters = $relation; @@ -76,28 +92,30 @@ class GeoShapeQuery implements BuilderInterface 'shape' => $filter, 'relation' => $relation, ]; + + return $this; } /** * Add geo-shape pre-indexed filter. * - * @param string $field Field name. - * @param string $id The ID of the document that containing the pre-indexed shape. - * @param string $type Name of the index where the pre-indexed shape is. - * @param string $index Index type where the pre-indexed shape is. - * @param string $relation Spatial relation. - * @param string $path The field specified as path containing the pre-indexed shape. - * @param array $parameters Additional parameters. + * @param string $field Field name. + * @param string $id The ID of the document that containing the pre-indexed shape. + * @param string $type Name of the index where the pre-indexed shape is. + * @param string $index Index type where the pre-indexed shape is. + * @param string $relation Spatial relation. + * @param string $path The field specified as path containing the pre-indexed shape. + * @param array $parameters Additional parameters. */ public function addPreIndexedShape( - $field, - $id, - $type, - $index, - $path, + string $field, + string $id, + string $type, + string $index, + string $path, $relation = self::INTERSECTS, array $parameters = [] - ): void { + ): static { // TODO: remove this in the next major version if (is_array($relation)) { $parameters = $relation; @@ -119,12 +137,14 @@ class GeoShapeQuery implements BuilderInterface 'indexed_shape' => $filter, 'relation' => $relation, ]; + + return $this; } /** * {@inheritdoc} */ - public function toArray(): array|\stdClass + public function toArray(): array { $output = $this->processArray($this->fields); diff --git a/src/Query/Joining/HasChildQuery.php b/src/Query/Joining/HasChildQuery.php index c3f364a..1a721bb 100644 --- a/src/Query/Joining/HasChildQuery.php +++ b/src/Query/Joining/HasChildQuery.php @@ -24,21 +24,13 @@ class HasChildQuery implements BuilderInterface use ParametersTrait; /** - * @param string $type + * @param string $type */ public function __construct(private $type, private readonly BuilderInterface $query, array $parameters = []) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'has_child'; - } - /** * {@inheritdoc} */ @@ -53,4 +45,12 @@ class HasChildQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'has_child'; + } } diff --git a/src/Query/Joining/HasParentQuery.php b/src/Query/Joining/HasParentQuery.php index 55efeb8..7ad5f7d 100644 --- a/src/Query/Joining/HasParentQuery.php +++ b/src/Query/Joining/HasParentQuery.php @@ -24,21 +24,13 @@ class HasParentQuery implements BuilderInterface use ParametersTrait; /** - * @param string $parentType + * @param string $parentType */ public function __construct(private $parentType, private readonly BuilderInterface $query, array $parameters = []) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'has_parent'; - } - /** * {@inheritdoc} */ @@ -53,4 +45,12 @@ class HasParentQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'has_parent'; + } } diff --git a/src/Query/Joining/NestedQuery.php b/src/Query/Joining/NestedQuery.php index 3147ec1..02f0dea 100644 --- a/src/Query/Joining/NestedQuery.php +++ b/src/Query/Joining/NestedQuery.php @@ -24,21 +24,13 @@ class NestedQuery implements BuilderInterface use ParametersTrait; /** - * @param string $path + * @param string $path */ public function __construct(private $path, private readonly BuilderInterface $query, array $parameters = []) { $this->parameters = $parameters; } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'nested'; - } - /** * {@inheritdoc} */ @@ -50,10 +42,18 @@ class NestedQuery implements BuilderInterface 'path' => $this->path, 'query' => $this->query->toArray(), ] - ) + ), ]; } + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'nested'; + } + /** * Returns nested query object. */ diff --git a/src/Query/Joining/ParentIdQuery.php b/src/Query/Joining/ParentIdQuery.php index 3b1426e..9a4e5e0 100644 --- a/src/Query/Joining/ParentIdQuery.php +++ b/src/Query/Joining/ParentIdQuery.php @@ -20,14 +20,6 @@ class ParentIdQuery implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'parent_id'; - } - /** * {@inheritdoc} */ @@ -41,4 +33,12 @@ class ParentIdQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'parent_id'; + } } diff --git a/src/Query/MatchAllQuery.php b/src/Query/MatchAllQuery.php index 870c190..b31c613 100644 --- a/src/Query/MatchAllQuery.php +++ b/src/Query/MatchAllQuery.php @@ -34,17 +34,17 @@ class MatchAllQuery implements BuilderInterface /** * {@inheritdoc} */ - public function getType(): string + public function toArray(): array|\stdClass { - return 'match_all'; + $params = $this->getParameters(); + return [$this->getType() => !empty($params) ? $params : new \stdClass()]; } /** * {@inheritdoc} */ - public function toArray(): array|\stdClass + public function getType(): string { - $params = $this->getParameters(); - return [$this->getType() => !empty($params) ? $params : new \stdClass()]; + return 'match_all'; } } diff --git a/src/Query/Span/FieldMaskingSpanQuery.php b/src/Query/Span/FieldMaskingSpanQuery.php index e14b237..e6fea29 100644 --- a/src/Query/Span/FieldMaskingSpanQuery.php +++ b/src/Query/Span/FieldMaskingSpanQuery.php @@ -33,7 +33,7 @@ class FieldMaskingSpanQuery implements SpanQueryInterface private $field; /** - * @param string $field + * @param string $field */ public function __construct($field, SpanQueryInterface $query) { @@ -41,6 +41,21 @@ class FieldMaskingSpanQuery implements SpanQueryInterface $this->setField($field); } + /** + * {@inheritdoc} + */ + public function toArray(): array|\stdClass + { + $output = [ + 'query' => $this->getQuery()->toArray(), + 'field' => $this->getField(), + ]; + + $output = $this->processArray($output); + + return [$this->getType() => $output]; + } + /** * @return mixed */ @@ -79,21 +94,6 @@ class FieldMaskingSpanQuery implements SpanQueryInterface return $this; } - /** - * {@inheritdoc} - */ - public function toArray(): array|\stdClass - { - $output = [ - 'query' => $this->getQuery()->toArray(), - 'field' => $this->getField(), - ]; - - $output = $this->processArray($output); - - return [$this->getType() => $output]; - } - /** * {@inheritdoc} */ diff --git a/src/Query/Span/SpanContainingQuery.php b/src/Query/Span/SpanContainingQuery.php index ec26948..093010e 100644 --- a/src/Query/Span/SpanContainingQuery.php +++ b/src/Query/Span/SpanContainingQuery.php @@ -38,6 +38,21 @@ class SpanContainingQuery implements SpanQueryInterface $this->setBig($big); } + /** + * {@inheritdoc} + */ + public function toArray(): array|\stdClass + { + $output = [ + 'little' => $this->getLittle()->toArray(), + 'big' => $this->getBig()->toArray(), + ]; + + $output = $this->processArray($output); + + return [$this->getType() => $output]; + } + public function getLittle(): SpanQueryInterface { return $this->little; @@ -75,19 +90,4 @@ class SpanContainingQuery implements SpanQueryInterface { return 'span_containing'; } - - /** - * {@inheritdoc} - */ - public function toArray(): array|\stdClass - { - $output = [ - 'little' => $this->getLittle()->toArray(), - 'big' => $this->getBig()->toArray(), - ]; - - $output = $this->processArray($output); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/Span/SpanFirstQuery.php b/src/Query/Span/SpanFirstQuery.php index ca0c896..3fe0f85 100644 --- a/src/Query/Span/SpanFirstQuery.php +++ b/src/Query/Span/SpanFirstQuery.php @@ -23,7 +23,7 @@ class SpanFirstQuery implements SpanQueryInterface use ParametersTrait; /** - * @param int $end + * @param int $end * * @throws \LogicException */ @@ -32,14 +32,6 @@ class SpanFirstQuery implements SpanQueryInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'span_first'; - } - /** * {@inheritdoc} */ @@ -52,4 +44,12 @@ class SpanFirstQuery implements SpanQueryInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'span_first'; + } } diff --git a/src/Query/Span/SpanMultiTermQuery.php b/src/Query/Span/SpanMultiTermQuery.php index 5fbb4b4..f4da785 100644 --- a/src/Query/Span/SpanMultiTermQuery.php +++ b/src/Query/Span/SpanMultiTermQuery.php @@ -31,14 +31,6 @@ class SpanMultiTermQuery implements SpanQueryInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'span_multi'; - } - /** * {@inheritdoc} * @@ -52,4 +44,12 @@ class SpanMultiTermQuery implements SpanQueryInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'span_multi'; + } } diff --git a/src/Query/Span/SpanNearQuery.php b/src/Query/Span/SpanNearQuery.php index 24a6b07..8a311ca 100644 --- a/src/Query/Span/SpanNearQuery.php +++ b/src/Query/Span/SpanNearQuery.php @@ -23,6 +23,22 @@ class SpanNearQuery extends SpanOrQuery implements SpanQueryInterface */ private $slop; + /** + * {@inheritdoc} + */ + public function toArray(): array|\stdClass + { + $query = []; + foreach ($this->getQueries() as $type) { + $query['clauses'][] = $type->toArray(); + } + + $query['slop'] = $this->getSlop(); + $output = $this->processArray($query); + + return [$this->getType() => $output]; + } + /** * @return int */ @@ -50,20 +66,4 @@ class SpanNearQuery extends SpanOrQuery implements SpanQueryInterface { return 'span_near'; } - - /** - * {@inheritdoc} - */ - public function toArray(): array|\stdClass - { - $query = []; - foreach ($this->getQueries() as $type) { - $query['clauses'][] = $type->toArray(); - } - - $query['slop'] = $this->getSlop(); - $output = $this->processArray($query); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/Span/SpanNotQuery.php b/src/Query/Span/SpanNotQuery.php index 80b8474..21d49a1 100644 --- a/src/Query/Span/SpanNotQuery.php +++ b/src/Query/Span/SpanNotQuery.php @@ -22,19 +22,14 @@ class SpanNotQuery implements SpanQueryInterface { use ParametersTrait; - public function __construct(private readonly SpanQueryInterface $include, private readonly SpanQueryInterface $exclude, array $parameters = []) - { + public function __construct( + private readonly SpanQueryInterface $include, + private readonly SpanQueryInterface $exclude, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'span_not'; - } - /** * {@inheritdoc} */ @@ -47,4 +42,12 @@ class SpanNotQuery implements SpanQueryInterface return [$this->getType() => $this->processArray($query)]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'span_not'; + } } diff --git a/src/Query/Span/SpanOrQuery.php b/src/Query/Span/SpanOrQuery.php index 296cab2..f7ef77f 100644 --- a/src/Query/Span/SpanOrQuery.php +++ b/src/Query/Span/SpanOrQuery.php @@ -53,14 +53,6 @@ class SpanOrQuery implements SpanQueryInterface return $this->queries; } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'span_or'; - } - /** * {@inheritdoc} */ @@ -75,4 +67,12 @@ class SpanOrQuery implements SpanQueryInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'span_or'; + } } diff --git a/src/Query/Specialized/MoreLikeThisQuery.php b/src/Query/Specialized/MoreLikeThisQuery.php index 914c862..9d3385e 100644 --- a/src/Query/Specialized/MoreLikeThisQuery.php +++ b/src/Query/Specialized/MoreLikeThisQuery.php @@ -31,14 +31,6 @@ class MoreLikeThisQuery implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'more_like_this'; - } - /** * {@inheritdoc} */ @@ -54,4 +46,12 @@ class MoreLikeThisQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'more_like_this'; + } } diff --git a/src/Query/Specialized/RankFeatureQuery.php b/src/Query/Specialized/RankFeatureQuery.php new file mode 100644 index 0000000..9edb29d --- /dev/null +++ b/src/Query/Specialized/RankFeatureQuery.php @@ -0,0 +1,39 @@ +<?php + +namespace ONGR\ElasticsearchDSL\Query\Specialized; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "rank_feature" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-rank-feature-query.html + */ +class RankFeatureQuery implements BuilderInterface +{ + use ParametersTrait; + + public function __construct(private readonly string $field, array $parameters) + { + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'rank_feature'; + } + + /** + * {@inheritdoc} + */ + public function toArray(): array + { + $output = $this->processArray(['field' => $this->field]); + + return [$this->getType() => $output]; + } +} diff --git a/src/Query/Specialized/ScriptQuery.php b/src/Query/Specialized/ScriptQuery.php index 3dd1a3f..c9d4637 100644 --- a/src/Query/Specialized/ScriptQuery.php +++ b/src/Query/Specialized/ScriptQuery.php @@ -24,8 +24,8 @@ class ScriptQuery implements BuilderInterface use ParametersTrait; /** - * @param string $script Script - * @param array $parameters Optional parameters + * @param string $script Script + * @param array $parameters Optional parameters */ public function __construct(private $script, array $parameters = []) { @@ -35,19 +35,19 @@ class ScriptQuery implements BuilderInterface /** * {@inheritdoc} */ - public function getType(): string + public function toArray(): array|\stdClass { - return 'script'; + $query = ['inline' => $this->script]; + $output = $this->processArray($query); + + return [$this->getType() => ['script' => $output]]; } /** * {@inheritdoc} */ - public function toArray(): array|\stdClass + public function getType(): string { - $query = ['inline' => $this->script]; - $output = $this->processArray($query); - - return [$this->getType() => ['script' => $output]]; + return 'script'; } } diff --git a/src/Query/Specialized/TemplateQuery.php b/src/Query/Specialized/TemplateQuery.php index c5ffba2..362f10c 100644 --- a/src/Query/Specialized/TemplateQuery.php +++ b/src/Query/Specialized/TemplateQuery.php @@ -41,7 +41,7 @@ class TemplateQuery implements BuilderInterface /** * @param string $file A template of the query * @param string $inline A template of the query - * @param array $params Parameters to insert into template + * @param array $params Parameters to insert into template */ public function __construct($file = null, $inline = null, array $params = []) { @@ -50,6 +50,30 @@ class TemplateQuery implements BuilderInterface $this->setParams($params); } + /** + * {@inheritdoc} + */ + public function toArray(): array|\stdClass + { + $output = array_filter( + [ + 'file' => $this->getFile(), + 'inline' => $this->getInline(), + 'params' => $this->getParams(), + ] + ); + + if (!isset($output['file']) && !isset($output['inline'])) { + throw new \InvalidArgumentException( + 'Template query requires that either `inline` or `file` parameters are set' + ); + } + + $output = $this->processArray($output); + + return [$this->getType() => $output]; + } + /** * @return string */ @@ -117,28 +141,4 @@ class TemplateQuery implements BuilderInterface { return 'template'; } - - /** - * {@inheritdoc} - */ - public function toArray(): array|\stdClass - { - $output = array_filter( - [ - 'file' => $this->getFile(), - 'inline' => $this->getInline(), - 'params' => $this->getParams(), - ] - ); - - if (!isset($output['file']) && !isset($output['inline'])) { - throw new \InvalidArgumentException( - 'Template query requires that either `inline` or `file` parameters are set' - ); - } - - $output = $this->processArray($output); - - return [$this->getType() => $output]; - } } diff --git a/src/Query/TermLevel/ExistsQuery.php b/src/Query/TermLevel/ExistsQuery.php index 74953ce..a32a915 100644 --- a/src/Query/TermLevel/ExistsQuery.php +++ b/src/Query/TermLevel/ExistsQuery.php @@ -27,14 +27,6 @@ class ExistsQuery implements BuilderInterface { } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'exists'; - } - /** * {@inheritdoc} */ @@ -46,4 +38,12 @@ class ExistsQuery implements BuilderInterface ], ]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'exists'; + } } diff --git a/src/Query/TermLevel/FuzzyQuery.php b/src/Query/TermLevel/FuzzyQuery.php index 2638a0d..88af76b 100644 --- a/src/Query/TermLevel/FuzzyQuery.php +++ b/src/Query/TermLevel/FuzzyQuery.php @@ -32,14 +32,6 @@ class FuzzyQuery implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'fuzzy'; - } - /** * {@inheritdoc} */ @@ -55,4 +47,12 @@ class FuzzyQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'fuzzy'; + } } diff --git a/src/Query/TermLevel/IdsQuery.php b/src/Query/TermLevel/IdsQuery.php index 4a173c8..dc94df5 100644 --- a/src/Query/TermLevel/IdsQuery.php +++ b/src/Query/TermLevel/IdsQuery.php @@ -28,14 +28,6 @@ class IdsQuery implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'ids'; - } - /** * {@inheritdoc} */ @@ -49,4 +41,12 @@ class IdsQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'ids'; + } } diff --git a/src/Query/TermLevel/PrefixQuery.php b/src/Query/TermLevel/PrefixQuery.php index f1648d8..fe2c17c 100644 --- a/src/Query/TermLevel/PrefixQuery.php +++ b/src/Query/TermLevel/PrefixQuery.php @@ -24,23 +24,15 @@ class PrefixQuery implements BuilderInterface use ParametersTrait; /** - * @param string $field Field name. - * @param string $value Value. - * @param array $parameters Optional parameters. + * @param string $field Field name. + * @param string $value Value. + * @param array $parameters Optional parameters. */ public function __construct(protected $field, protected $value, array $parameters = []) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'prefix'; - } - /** * {@inheritdoc} */ @@ -56,4 +48,12 @@ class PrefixQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'prefix'; + } } diff --git a/src/Query/TermLevel/RangeQuery.php b/src/Query/TermLevel/RangeQuery.php index 4cca3f1..d0d6893 100644 --- a/src/Query/TermLevel/RangeQuery.php +++ b/src/Query/TermLevel/RangeQuery.php @@ -50,14 +50,6 @@ class RangeQuery implements BuilderInterface } } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'range'; - } - /** * {@inheritdoc} */ @@ -69,4 +61,12 @@ class RangeQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'range'; + } } diff --git a/src/Query/TermLevel/RegexpQuery.php b/src/Query/TermLevel/RegexpQuery.php index a4aa518..08665e9 100644 --- a/src/Query/TermLevel/RegexpQuery.php +++ b/src/Query/TermLevel/RegexpQuery.php @@ -32,14 +32,6 @@ class RegexpQuery implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'regexp'; - } - /** * {@inheritdoc} */ @@ -55,4 +47,12 @@ class RegexpQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'regexp'; + } } diff --git a/src/Query/TermLevel/TermQuery.php b/src/Query/TermLevel/TermQuery.php index 3b5766d..14578fe 100644 --- a/src/Query/TermLevel/TermQuery.php +++ b/src/Query/TermLevel/TermQuery.php @@ -28,14 +28,6 @@ class TermQuery implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'term'; - } - /** * {@inheritdoc} */ @@ -55,4 +47,12 @@ class TermQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'term'; + } } diff --git a/src/Query/TermLevel/TermsQuery.php b/src/Query/TermLevel/TermsQuery.php index be2da6d..002eba8 100644 --- a/src/Query/TermLevel/TermsQuery.php +++ b/src/Query/TermLevel/TermsQuery.php @@ -26,23 +26,15 @@ class TermsQuery implements BuilderInterface /** * Constructor. * - * @param string $field Field name - * @param array $terms An array of terms - * @param array $parameters Optional parameters + * @param string $field Field name + * @param array $terms An array of terms + * @param array $parameters Optional parameters */ public function __construct(private $field, private $terms, array $parameters = []) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'terms'; - } - /** * {@inheritdoc} */ @@ -56,4 +48,12 @@ class TermsQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'terms'; + } } diff --git a/src/Query/TermLevel/TermsSetQuery.php b/src/Query/TermLevel/TermsSetQuery.php index a644f13..f042c6d 100644 --- a/src/Query/TermLevel/TermsSetQuery.php +++ b/src/Query/TermLevel/TermsSetQuery.php @@ -30,9 +30,9 @@ class TermsSetQuery implements BuilderInterface /** * Constructor. * - * @param string $field Field name - * @param array $terms An array of terms - * @param array $parameters Parameters + * @param string $field Field name + * @param array $terms An array of terms + * @param array $parameters Parameters */ public function __construct(private $field, private $terms, array $parameters) { @@ -40,12 +40,14 @@ class TermsSetQuery implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string + private function validateParameters(array $parameters) { - return 'terms_set'; + if (!isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_FIELD]) && + !isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_SCRIPT]) + ) { + $message = "Either minimum_should_match_field or minimum_should_match_script must be set."; + throw new \InvalidArgumentException($message); + } } /** @@ -57,18 +59,18 @@ class TermsSetQuery implements BuilderInterface 'terms' => $this->terms, ]; - return [$this->getType() => [ - $this->field => $this->processArray($query), - ]]; + return [ + $this->getType() => [ + $this->field => $this->processArray($query), + ], + ]; } - private function validateParameters(array $parameters) + /** + * {@inheritdoc} + */ + public function getType(): string { - if (!isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_FIELD]) && - !isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_SCRIPT]) - ) { - $message = "Either minimum_should_match_field or minimum_should_match_script must be set."; - throw new \InvalidArgumentException($message); - } + return 'terms_set'; } } diff --git a/src/Query/TermLevel/TypeQuery.php b/src/Query/TermLevel/TypeQuery.php index 5f42816..7306fe1 100644 --- a/src/Query/TermLevel/TypeQuery.php +++ b/src/Query/TermLevel/TypeQuery.php @@ -29,14 +29,6 @@ class TypeQuery implements BuilderInterface { } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'type'; - } - /** * {@inheritdoc} */ @@ -48,4 +40,12 @@ class TypeQuery implements BuilderInterface ], ]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'type'; + } } diff --git a/src/Query/TermLevel/WildcardQuery.php b/src/Query/TermLevel/WildcardQuery.php index 19dbbcc..90fed62 100644 --- a/src/Query/TermLevel/WildcardQuery.php +++ b/src/Query/TermLevel/WildcardQuery.php @@ -32,14 +32,6 @@ class WildcardQuery implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'wildcard'; - } - /** * {@inheritdoc} */ @@ -55,4 +47,12 @@ class WildcardQuery implements BuilderInterface return [$this->getType() => $output]; } + + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'wildcard'; + } } diff --git a/src/Search.php b/src/Search.php index e9e99a4..1dba4c5 100644 --- a/src/Search.php +++ b/src/Search.php @@ -24,34 +24,32 @@ use ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointFactory; use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointInterface; use ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint; +use ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint; use ONGR\ElasticsearchDSL\Serializer\Normalizer\CustomReferencedNormalizer; use ONGR\ElasticsearchDSL\Serializer\OrderedSerializer; use Symfony\Component\Serializer\Normalizer\CustomNormalizer; -use ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint; /** * Search object that can be executed by a manager. */ class Search { + private static ?OrderedSerializer $serializer = null; /** * If you don’t need to track the total number of hits at all you can improve * query times by setting this option to false. Defaults to true. */ private ?bool $trackTotalHits = null; - /** * To retrieve hits from a certain offset. Defaults to 0. */ private ?int $from = null; - /** * The number of hits to return. Defaults to 10. If you do not care about getting some * hits back but only about the number of matches and/or aggregations, setting the value * to 0 will help performance. */ private ?int $size = null; - /** * Allows to control how the _source field is returned with every hit. By default * operations return the contents of the _source field unless you have used the @@ -60,12 +58,10 @@ class Search * @var bool */ private $source; - /** * Allows to selectively load specific stored fields for each document represented by a search hit. */ private ?array $storedFields = null; - /** * Allows to return a script evaluation (based on different fields) for each hit. * Script fields can work on fields that are not stored, and allow to return custom @@ -74,7 +70,6 @@ class Search * to be returned from it (can be an "object" type). */ private ?array $scriptFields = null; - /** * Allows to return the doc value representation of a field for each hit. Doc value * fields can work on fields that are not stored. Note that if the fields parameter @@ -83,35 +78,30 @@ class Search * result in more memory consumption. */ private ?array $docValueFields = null; - /** * Enables explanation for each hit on how its score was computed. * * @var bool */ private ?bool $explain = null; - /** * Returns a version for each search hit. * * @var bool */ private ?bool $version = null; - /** * Allows to configure different boost level per index when searching across more * than one indices. This is very handy when hits coming from one index matter more * than hits coming from another index (think social graph where each user has an index). */ private ?array $indicesBoost = null; - /** * Exclude documents which have a _score less than the minimum specified in min_score. * * @var int */ private ?int $minScore = null; - /** * Pagination of results can be done by using the from and size but the cost becomes * prohibitive when the deep pagination is reached. The index.max_result_window which @@ -123,14 +113,12 @@ class Search * help the retrieval of the next page. */ private ?array $searchAfter = null; - /** * URI parameters alongside Request body search. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html */ private ?array $uriParams = null; - /** * While a search request returns a single “page†of results, the scroll API can be used to retrieve * large numbers of results (or even all results) from a single search request, in much the same way @@ -139,9 +127,6 @@ class Search * of one index into a new index with a different configuration. */ private ?string $scroll = null; - - private static ?OrderedSerializer $serializer = null; - /** * @var SearchEndpointInterface[] */ @@ -155,14 +140,6 @@ class Search $this->initializeSerializer(); } - /** - * Wakeup method to initialize static properties - */ - public function __wakeup() - { - $this->initializeSerializer(); - } - /** * Initializes the serializer */ @@ -178,6 +155,14 @@ class Search } } + /** + * Wakeup method to initialize static properties + */ + public function __wakeup() + { + $this->initializeSerializer(); + } + /** * Destroys search endpoint. * @@ -259,12 +244,12 @@ class Search /** * Adds a post filter to search. * - * @param BuilderInterface $filter Filter. - * @param string $boolType Example boolType values: + * @param BuilderInterface $filter Filter. + * @param string $boolType Example boolType values: * - must * - must_not * - should. - * @param string $key + * @param string $key * * @return $this. */ @@ -399,12 +384,12 @@ class Search } /** - * Adds suggest into search. - * - * @param BuilderInterface $suggest - * - * @return $this - */ + * Adds suggest into search. + * + * @param BuilderInterface $suggest + * + * @return $this + */ public function addSuggest(NamedBuilderInterface $suggest): static { $this->getEndpoint(SuggestEndpoint::NAME)->add($suggest, $suggest->getName()); @@ -413,10 +398,10 @@ class Search } /** - * Returns all suggests. - * - * @return BuilderInterface[] - */ + * Returns all suggests. + * + * @return BuilderInterface[] + */ public function getSuggests() { return $this->getEndpoint(SuggestEndpoint::NAME)->getAll(); diff --git a/src/SearchEndpoint/AggregationsEndpoint.php b/src/SearchEndpoint/AggregationsEndpoint.php index 8eddd6b..94975e1 100644 --- a/src/SearchEndpoint/AggregationsEndpoint.php +++ b/src/SearchEndpoint/AggregationsEndpoint.php @@ -27,8 +27,11 @@ class AggregationsEndpoint extends AbstractSearchEndpoint /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null - { + public function normalize( + NormalizerInterface $normalizer, + $format = null, + array $context = [] + ): array|string|int|float|bool|\ArrayObject|null { $output = []; /** @var AbstractAggregation $aggregation */ foreach ($this->getAll() as $aggregation) { diff --git a/src/SearchEndpoint/HighlightEndpoint.php b/src/SearchEndpoint/HighlightEndpoint.php index b8380ba..1607e05 100644 --- a/src/SearchEndpoint/HighlightEndpoint.php +++ b/src/SearchEndpoint/HighlightEndpoint.php @@ -37,8 +37,11 @@ class HighlightEndpoint extends AbstractSearchEndpoint /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null - { + public function normalize( + NormalizerInterface $normalizer, + $format = null, + array $context = [] + ): array|string|int|float|bool|\ArrayObject|null { if ($this->highlight) { return $this->highlight->toArray(); } diff --git a/src/SearchEndpoint/InnerHitsEndpoint.php b/src/SearchEndpoint/InnerHitsEndpoint.php index bcf78c8..173e8b7 100644 --- a/src/SearchEndpoint/InnerHitsEndpoint.php +++ b/src/SearchEndpoint/InnerHitsEndpoint.php @@ -27,8 +27,11 @@ class InnerHitsEndpoint extends AbstractSearchEndpoint /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null - { + public function normalize( + NormalizerInterface $normalizer, + $format = null, + array $context = [] + ): array|string|int|float|bool|\ArrayObject|null { $output = []; /** @var NestedInnerHit $innerHit */ foreach ($this->getAll() as $innerHit) { diff --git a/src/SearchEndpoint/PostFilterEndpoint.php b/src/SearchEndpoint/PostFilterEndpoint.php index b35869d..f71c30d 100644 --- a/src/SearchEndpoint/PostFilterEndpoint.php +++ b/src/SearchEndpoint/PostFilterEndpoint.php @@ -26,8 +26,11 @@ class PostFilterEndpoint extends QueryEndpoint /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null - { + public function normalize( + NormalizerInterface $normalizer, + $format = null, + array $context = [] + ): array|string|int|float|bool|\ArrayObject|null { if (!$this->getBool()) { return null; } diff --git a/src/SearchEndpoint/QueryEndpoint.php b/src/SearchEndpoint/QueryEndpoint.php index 24c0477..8e3511b 100644 --- a/src/SearchEndpoint/QueryEndpoint.php +++ b/src/SearchEndpoint/QueryEndpoint.php @@ -33,8 +33,11 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null - { + public function normalize( + NormalizerInterface $normalizer, + $format = null, + array $context = [] + ): array|string|int|float|bool|\ArrayObject|null { if (!$this->filtersSet && $this->hasReference('filter_query')) { /** @var BuilderInterface $filter */ $filter = $this->getReference('filter_query'); @@ -49,14 +52,6 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI return $this->bool->toArray(); } - /** - * {@inheritdoc} - */ - public function add(BuilderInterface $builder, $key = null) - { - return $this->addToBool($builder, BoolQuery::MUST, $key); - } - /** * {@inheritdoc} */ @@ -69,6 +64,14 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI return $this->bool->add($builder, $boolType, $key); } + /** + * {@inheritdoc} + */ + public function add(BuilderInterface $builder, $key = null) + { + return $this->addToBool($builder, BoolQuery::MUST, $key); + } + /** * {@inheritdoc} */ diff --git a/src/SearchEndpoint/SearchEndpointInterface.php b/src/SearchEndpoint/SearchEndpointInterface.php index 0464cba..d0f4a0c 100644 --- a/src/SearchEndpoint/SearchEndpointInterface.php +++ b/src/SearchEndpoint/SearchEndpointInterface.php @@ -24,7 +24,7 @@ interface SearchEndpointInterface extends NormalizableInterface * Adds builder to search endpoint. * * @param BuilderInterface $builder Builder to add. - * @param array $key Additional parameters relevant to builder. + * @param array $key Additional parameters relevant to builder. * * @return string Key of added builder. */ @@ -33,10 +33,10 @@ interface SearchEndpointInterface extends NormalizableInterface /** * Adds builder to search endpoint's specific bool type container. * - * @param BuilderInterface $builder Builder to add. - * @param array $boolType Bool type for query or filter. If bool type is left null + * @param BuilderInterface $builder Builder to add. + * @param array $boolType Bool type for query or filter. If bool type is left null * it will be treated as MUST. - * @param array $key Additional parameters relevant to builder. + * @param array $key Additional parameters relevant to builder. * * @return string Key of added builder. */ diff --git a/src/SearchEndpoint/SortEndpoint.php b/src/SearchEndpoint/SortEndpoint.php index ba2f7d1..e692743 100644 --- a/src/SearchEndpoint/SortEndpoint.php +++ b/src/SearchEndpoint/SortEndpoint.php @@ -26,8 +26,11 @@ class SortEndpoint extends AbstractSearchEndpoint /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null - { + public function normalize( + NormalizerInterface $normalizer, + $format = null, + array $context = [] + ): array|string|int|float|bool|\ArrayObject|null { $output = []; foreach ($this->getAll() as $sort) { diff --git a/src/SearchEndpoint/SuggestEndpoint.php b/src/SearchEndpoint/SuggestEndpoint.php index 56a684b..934aafb 100644 --- a/src/SearchEndpoint/SuggestEndpoint.php +++ b/src/SearchEndpoint/SuggestEndpoint.php @@ -27,8 +27,11 @@ class SuggestEndpoint extends AbstractSearchEndpoint /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null - { + public function normalize( + NormalizerInterface $normalizer, + $format = null, + array $context = [] + ): array|string|int|float|bool|\ArrayObject|null { $output = []; /** @var TermSuggest $suggest */ foreach ($this->getAll() as $suggest) { diff --git a/src/Serializer/Normalizer/CustomReferencedNormalizer.php b/src/Serializer/Normalizer/CustomReferencedNormalizer.php index bd5501b..abdf95d 100644 --- a/src/Serializer/Normalizer/CustomReferencedNormalizer.php +++ b/src/Serializer/Normalizer/CustomReferencedNormalizer.php @@ -23,8 +23,11 @@ class CustomReferencedNormalizer extends CustomNormalizer /** * {@inheritdoc} */ - public function normalize(mixed $object, string $format = null, array $context = []): array|bool|string|int|float|null|\ArrayObject - { + public function normalize( + mixed $object, + string $format = null, + array $context = [] + ): array|bool|string|int|float|null|\ArrayObject { $object->setReferences($this->references); $data = parent::normalize($object, $format, $context); $this->references = array_merge($this->references, $object->getReferences()); diff --git a/src/Serializer/OrderedSerializer.php b/src/Serializer/OrderedSerializer.php index 90e3329..b76c0a5 100644 --- a/src/Serializer/OrderedSerializer.php +++ b/src/Serializer/OrderedSerializer.php @@ -22,8 +22,11 @@ class OrderedSerializer extends Serializer /** * {@inheritdoc} */ - public function normalize($data, string $format = null, array $context = []): array|bool|string|int|float|null|\ArrayObject - { + public function normalize( + $data, + string $format = null, + array $context = [] + ): array|bool|string|int|float|null|\ArrayObject { return parent::normalize( is_array($data) ? $this->order($data) : $data, $format, @@ -31,19 +34,6 @@ class OrderedSerializer extends Serializer ); } - /** - * {@inheritdoc} - */ - public function denormalize($data, string $type, string $format = null, array $context = []): mixed - { - return parent::denormalize( - is_array($data) ? $this->order($data) : $data, - $type, - $format, - $context - ); - } - /** * Orders objects if can be done. */ @@ -54,7 +44,8 @@ class OrderedSerializer extends Serializer if (!empty($filteredData)) { uasort( $filteredData, - static fn(OrderedNormalizerInterface $a, OrderedNormalizerInterface $b): bool => $a->getOrder() > $b->getOrder() + static fn(OrderedNormalizerInterface $a, OrderedNormalizerInterface $b): + bool => $a->getOrder() > $b->getOrder() ); return array_merge($filteredData, array_diff_key($data, $filteredData)); @@ -75,4 +66,17 @@ class OrderedSerializer extends Serializer static fn($value): bool => $value instanceof OrderedNormalizerInterface ); } + + /** + * {@inheritdoc} + */ + public function denormalize($data, string $type, string $format = null, array $context = []): mixed + { + return parent::denormalize( + is_array($data) ? $this->order($data) : $data, + $type, + $format, + $context + ); + } } diff --git a/src/Sort/FieldSort.php b/src/Sort/FieldSort.php index 6d343c7..67f16a1 100644 --- a/src/Sort/FieldSort.php +++ b/src/Sort/FieldSort.php @@ -27,9 +27,9 @@ class FieldSort implements BuilderInterface private ?BuilderInterface $nestedFilter = null; /** - * @param string $field Field name. - * @param string $order Order direction. - * @param array $params Params that can be set to field sort. + * @param string $field Field name. + * @param string $order Order direction. + * @param array $params Params that can be set to field sort. */ public function __construct(private string $field, private ?string $order = null, array $params = []) { diff --git a/src/Sort/NestedSort.php b/src/Sort/NestedSort.php index d61c640..67daeca 100644 --- a/src/Sort/NestedSort.php +++ b/src/Sort/NestedSort.php @@ -37,21 +37,13 @@ class NestedSort implements BuilderInterface $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType(): string - { - return 'nested'; - } - /** * {@inheritdoc} */ public function toArray(): array|\stdClass { $output = [ - 'path' => $this->path, + 'path' => $this->path, ]; if ($this->filter instanceof BuilderInterface) { @@ -65,6 +57,14 @@ class NestedSort implements BuilderInterface return $this->processArray($output); } + /** + * {@inheritdoc} + */ + public function getType(): string + { + return 'nested'; + } + /** * Returns nested filter object. */ diff --git a/src/Suggest/Suggest.php b/src/Suggest/Suggest.php index 2054496..70d6571 100644 --- a/src/Suggest/Suggest.php +++ b/src/Suggest/Suggest.php @@ -35,11 +35,17 @@ class Suggest implements NamedBuilderInterface $this->setParameters($parameters); } - public function setName(string $name): static + /** + * {@inheritdoc} + */ + public function toArray(): array|\stdClass { - $this->name = $name; - - return $this; + return [ + $this->getName() => [ + 'text' => $this->getText(), + $this->getType() => $this->processArray(['field' => $this->getField()]), + ], + ]; } public function getName(): string @@ -47,14 +53,9 @@ class Suggest implements NamedBuilderInterface return $this->name; } - public function getType(): string - { - return $this->type; - } - - public function setType(string $type): static + public function setName(string $name): static { - $this->type = $type; + $this->name = $name; return $this; } @@ -71,6 +72,18 @@ class Suggest implements NamedBuilderInterface return $this; } + public function getType(): string + { + return $this->type; + } + + public function setType(string $type): static + { + $this->type = $type; + + return $this; + } + public function getField(): string { return $this->field; @@ -82,17 +95,4 @@ class Suggest implements NamedBuilderInterface return $this; } - - /** - * {@inheritdoc} - */ - public function toArray(): array|\stdClass - { - return [ - $this->getName() => [ - 'text' => $this->getText(), - $this->getType() => $this->processArray(['field' => $this->getField()]), - ] - ]; - } } diff --git a/tests/Functional/Query/FunctionScoreQueryTest.php b/tests/Functional/Query/FunctionScoreQueryTest.php index 60e1055..7f04771 100644 --- a/tests/Functional/Query/FunctionScoreQueryTest.php +++ b/tests/Functional/Query/FunctionScoreQueryTest.php @@ -37,7 +37,7 @@ class FunctionScoreQueryTest extends AbstractElasticsearchTestCase 'title' => 'bar', 'price' => 10, ], - ] + ], ]; } @@ -55,7 +55,10 @@ class FunctionScoreQueryTest extends AbstractElasticsearchTestCase $results = $this->executeSearch($search); - $this->assertEquals(is_countable($this->getDataArray()['product']) ? count($this->getDataArray()['product']) : 0, count($results)); + $this->assertEquals( + is_countable($this->getDataArray()['product']) ? count($this->getDataArray()['product']) : 0, + count($results) + ); } public function testScriptScore(): void diff --git a/tests/Unit/Query/Compound/FunctionScoreQueryTest.php b/tests/Unit/Query/Compound/FunctionScoreQueryTest.php index e95b196..c44830c 100644 --- a/tests/Unit/Query/Compound/FunctionScoreQueryTest.php +++ b/tests/Unit/Query/Compound/FunctionScoreQueryTest.php @@ -11,12 +11,12 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Compound; -use PHPUnit\Framework\MockObject\MockObject; -use PHPUnit\Framework\TestCase; -use PHPUnit\Framework\Attributes\DataProvider; use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\Query\Compound\FunctionScoreQuery; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; /** * Tests for FunctionScoreQuery. @@ -48,7 +48,7 @@ class FunctionScoreQueryTest extends TestCase 'query' => [], 'functions' => [ [ - 'random_score' => [ 'seed' => 'someSeed'], + 'random_score' => ['seed' => 'someSeed'], ], ], ], @@ -72,7 +72,7 @@ class FunctionScoreQueryTest extends TestCase $this->assertEquals(['function_score' => $expectedArray], $functionScoreQuery->toArray()); } - + /** * Tests default argument values. */ -- GitLab