diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 686b094bd19ae5d8eb6e05d07a31d6539d001604..b21527b5dcb96e006ab7c5fac6fe2350e8680475 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 d83de8255fe153eca4d4523a238be6e8f0eaa810..418606ff23fa76019717a60ceaca9266e30df823 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 606c3cce6c03851d3cb248f8ddc495af73644261..8ef4907e29e4b490a9dfe08301d86e6a95946b2d 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 45a30b69b34cd9806df855a1d5c079e2754a5de5..b68cc7878f6c78541c3994a05974ad82747c9040 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 c36203ec1d51bca46b7c5a352ee04b45b59c9749..7837029047369471ae28ff92ef7636041b756f3d 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 ad4df1aff8c8d21964393cb780ebd5755bcc0627..e24e73483fa54246d22a470d0983609c2c392b34 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 f78f3ebd01adda1a93cf57a9d8b074e8b031782d..f659c236518aa121d057ae49eb42199374be297c 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 77829f17a209cd745b854d0db0fe2092b839d19b..c7cf11d8bf15ce02dfdd13d43cb7a73a77e780cd 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 461c16556f7eb0a1beefbefd53e0c184bdb63100..166faa54034e3209a8a1a5c5fbeeaf5c24058718 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 d5f936c2cb1a11ef6a48b611e26692108455eef2..0a751d8b2854210dcdd38ec61b8a7a854ae42e3b 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 72fee2279c0b4c7e049b7453c539c78633e7f21c..9809fd64984610505ee0d8d566bdb9c8b5f5a692 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 7432a5e0600694abaac009757af655c35fc93315..53c0e54aa11e667ee0090a3d48e96dce3208ed8c 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 675ee62a73183ccb77e55148643e0b93f1a02a1f..7b7b253f6908c849e1ddf1c54a5eb75fffe63670 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 2f27e3955b2000ddb5d2c8c31990dd8e09318fe8..18bc1a99e4073b03259f71119cf008a5aaee0626 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 fc47fe66c7dd4e4fbd4ed3b9f6280261b7869fac..e25cf19bde152fcf70de443c96676f0989c60464 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 bc66b4848ab4f632b6197dddb32cb08667c499bc..6f334afa462bcb902022e24d40c6f9aac10cbe99 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 73bf0c5902567632fb097fa2e150121fe9072d4b..8f63d6d6da2c18f8b6e36d23b03d8cb63a5600a0 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 648df482fc8c9063d22a063188dedaf74576d187..263ab47ec926be9f7259f9057faf9f243f8462d9 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 ab6706f83b92d6627723e86b955d275def0a06b3..4959bdb1e46dcaeffe11ee0a72491590edc4218f 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 b807a97f9ce848d90cdc29ac24fce0440bed0a05..fa7969c7a139889ae2c217af3eb7519438498a9e 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 3d363aea5f519bb9710cde20f785da8db75b70d4..ef742fa1ffe4e1cecb106dca8ea27a8ccb2a880e 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 0631d67ed49e7e9db004d3925019873766c85f06..bdb1448b01c7e11b74feeacb7a0b23d82aaee6c4 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 ede8b5e2fbe4e2e9ad842cbad51a96f0dfa3a693..3c2b5f17b473d66cff0703ee71fe5a17f2c81026 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 653dfce187587797c71ff0eaa3d810d406707258..712d76e4133f1f4ee55edb714dd2118591ed44c8 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 aed69bce0adcae6eac92f42f2de5868dec959165..3c1b8f73e542c995152883110cf4b89a3c21883c 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 c701bac27cfedfb08ba2585d1614ea447632ca07..c483a1516e8bc03d58f4302900c80c9240f61a0f 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 8cc36fad5ec37bb8711425182b7b86aad2a1a621..9a1ebf6c4aa3e842d7a2ec23ff414b0c2fd45fdc 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 f6f06a801b17a893422bf197c432a0ed55aa9f3d..8aae9ff28516e736d310853e03246542b33bf181 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 f6b4f9bf610b175606d8744846fd94c2acf079a9..3ae722ce58c37abdb9a25aa9e714e2143e79afaf 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 3a23a45379a53e9217b1eec2318dfe4c3953311d..f521c8f1454e09bd8a1a561fd4fc000602ffc7a0 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 467fdf63c67c3601ea981962d968eb7b967fa3fa..bd1ea569c0127ac28e3c1e5bbe8e4558c20f691e 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 08b72a4dcc362fcfd8734a4f88067b68d372cbd4..1e16940ecc505b4b579c90d78df1ce8529af115f 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 f4647b9d78212e42d5fca4ae8205681f4f49ffd2..648a2ec3e972e7496cc70c837543ca8ac43cf1af 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 46830f9e02ad3aa491b6d3d87735ad39d0b6d69b..9a3ae694c93c764c3fc3182939080e33aa85aa6b 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 dd1d3728dd18e203fc9c0ef719569c6108563972..196d0ebe8f8430791266a1ea9430b3967df8ebc9 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 bc00c14e841531b84a354de7db6f341f69ead589..89de91dfad0f10de465273809985e6d7723420a9 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 bdd93c55da54fa1a8df39010c64065fc58fc0cd6..7b8387607f393d732747a6b189bc30498fbadd2b 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 5b365cfd145ac673b26f30b21e1f57cc21644d37..4c35bb07525850cf695df851b34eb976b5989c84 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 e8dd8ee73c26e24a0c03ec33612f795e18f0b413..128431144a0187c43456bc6e7e63ccd23bbeea06 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 c6f538f17b816ebbe390e93ebfd5d988d9942134..cece03c1da2b737f18acf20c3d80abdacbad4522 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 cfbf323780d0b04443f520091fc430543ebb3a58..a4f176ef13e31c0adf70a77b20e816907719163d 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 8d48f35efb61ba2678be18fb0357abef5f5760e6..bc66c7366dd8f16d25b1aca751d3ef6ddb8b691c 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 a5f85a13517c7d1b5b52cb0918a52e52232ba1c2..2ef63214784882b3b790fc0a400faf5ba2cb6d46 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 8156b54ee023440285179c91a53db0871299c251..508397e43ca35dfef9cc4eef956960b67c4ab0f7 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 27eeab7414ea205afdf92026e04468b16692ca05..9389b3304f84f4f85eaaf6c2ce2d77b6c6d9904f 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 212cc4aeb80b00f2ce9dd7ee0db864336f54367e..356022c59509eca2b372f04638505925ffd51d79 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 c3f364aa55abfbd2f657bc8407f44fc6168e0a64..1a721bbccb07971b1e2aa886f17cc3aae5b56b29 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 55efeb81621e8fd6d96cd6a9c73cacaef3f61967..7ad5f7d0c3eae6958638ee9448d231c834a1f3fd 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 3147ec1a7008851976efa54dd18ea8ed8e9d1ddc..02f0dea316eabe844d5df62fd40819ecac5b2cda 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 3b1426ebdea8e3327163cda5c9b46c4628bd3f8b..9a4e5e0ac41f3823fb476d238437da949a10bcee 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 870c190695d75c86061b54d6d000fda233274236..b31c6134853d85bc6c22bce06a604684a76309f8 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 e14b237249405d48dc97cddd00f60f257ff7da32..e6fea2937425d76e3973a475d857509dde7d420d 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 ec269481f5777877b6189be37922049d5125ef36..093010e7b00a98f9d78aa6128b96d5585c01bf4a 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 ca0c896e268ce37ee6a31c3e5fbb82c1d10f008a..3fe0f851159d02bfa291b2fa5832629c3063c693 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 5fbb4b44ce4621f044e02d9e3076d9237dd5a53f..f4da7857b6cab345d285e94b9ccc60713871ba2f 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 24a6b079f30b6c6673103f8ebb24536be67089df..8a311cadbb23d8fefc1a939cc5cdcd98ed3e7868 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 80b847491ab6acfb2d7fdc342b29166f6084275c..21d49a179d2debfd1f6394f3a675744456735e16 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 296cab23a9adfc0d138245f803bcaa485ddf987f..f7ef77fd385ab8edaff125d62a3f781d91a24ed4 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 914c862e873d39483778622bb707835f2b31cfad..9d3385ec31db9f5d5e815f935632508dbc2f50c7 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 0000000000000000000000000000000000000000..9edb29d8c17cbf18a3fa8d082bcbe3420b4732a4 --- /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 3dd1a3ff5c3a7bdf25a99b09f27ba1f102f88f32..c9d4637f97523f11e694af5fc19ab5fa0237dcd3 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 c5ffba28f69b66053d0de19e029d73697a56cf39..362f10c4299f47e88305cb0c85781d647c211ede 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 74953ce1da02ee4fe8549fa38382b443563cd51e..a32a9157f3980feecebae5195fa7954851a6e51d 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 2638a0dd81560c8d1636867cdcefd242e7513c9c..88af76b9de95919c14ed59b0ecc19e5da23e8c7d 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 4a173c86eefe08b3333bc6e9dca5329d080409b0..dc94df5f73f2b72269ea157b53b28d30ffd14038 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 f1648d85678d8bec9bc1a683983a16ada379721f..fe2c17c9ed98b55126899bd65d2d050bb1ceafd9 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 4cca3f1db7546db7e3175fe4b6aca83bd1eec232..d0d6893988145e1c798fbc97acc5abac596c190a 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 a4aa518a6ec975e3a76f84c8c69ce3867123b2aa..08665e959e363d24729ecfd176bd09f430234e96 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 3b5766d713142b8bfd864b34b4c1d779be9c140a..14578fe2990283cea30f0922561f70d51a4bac76 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 be2da6d3cdfd9207d845e258c263eb71c0829f50..002eba8d588e4c1a56ddae8961bc92c64f749004 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 a644f1395f78e800015e5fcd288a00637534855f..f042c6d689b90dcfd9dd67205356c5f68815fc07 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 5f42816eee8d407d5b83f0dd21573e0b5895e87c..7306fe1d23290733ea499a19fe71210449b33637 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 19dbbccb21f515f2cb1dff4801d29d78a13fe7e7..90fed625f59ae5e0f600a108e4095c3f4d5bcda2 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 e9e99a4a10d33bfb4775e7d097a58f4121b3a5c0..1dba4c527d4d851cbc652ca10c6e43aa9ee25871 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 8eddd6b5ac0e5a31235d95e43d52d403e3142135..94975e16cc0513d9b2079bdab24cf885de024bae 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 b8380ba117ec58091f3688f316be0b450800f762..1607e0502750eba9a26b1c14f723287aa9262e80 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 bcf78c80df4f06772e076ed0d77db9a3ee87bf0c..173e8b70c7182a77e5fa12e5fdb0a86e8b1a7404 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 b35869dfe845cdad92fc16aa39483b2a4a4f6dc9..f71c30d2b952fd41d0c6736b4ff49fb9bfabf0b3 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 24c047701db15c52af430a36e031a7b0ae048bc7..8e3511b5f55f0992e74cc5ae6eaa72f42a94d5ad 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 0464cba1f112f2a942944fb8d36656715ff39705..d0f4a0c7aa65bf4d3d84a21a1240594e6063ee0c 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 ba2f7d1bb6953b8f9a155bf514354313f32ed500..e69274318a57ad610cdd6956347ef11fb16c1f54 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 56a684b0d3dc625c3c1e88e28c0fa306278539c7..934aafba7654efb3b6536327233f4afb1aad22d8 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 bd5501b18e859d8320cbe5c2bcedb8bac781100a..abdf95d46a38ac9c11c34057a2e024b5cabfcc59 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 90e332974f0761f86059b04af1785e8927ff468f..b76c0a5da665b67ed038a0cc091c305c141ef6e5 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 6d343c7b43b9ed3dc4e1f9e60ac97851fc7b777c..67f16a18b5f23d13f05c62a014430d6eceb47a67 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 d61c6405a53d393891b51beea715a9bd1ee0de00..67daecabff236fe3ba8666a581a50e7792235cbf 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 2054496e98d2b3f361a4c597c65db3df9e8cdd9a..70d6571d5e216af5c77bbdfd1de7a5d6f652a959 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 60e1055c4a53d1ad571ce065e1c2cd3459d95e45..7f047711ece8a1ef81a8cc593a33a67c97bb8385 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 e95b196860ea86174ceaedfe0b726ea728426e6b..c44830c3048aa237e4c27720e3e563a17ab323de 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. */