From 1a8d08b4c2f3b5f2f1e3b75ffba14e829f39a03e Mon Sep 17 00:00:00 2001 From: Aleksander Kaim Date: Sat, 5 Oct 2024 12:36:45 +0200 Subject: [PATCH] Fix aggregation and bulk --- src/Aggregation/Aggregation.php | 34 ++++++++------------------------- src/Builder/Builder.php | 10 +++++++++- src/Bulk/Bulk.php | 4 ++-- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/Aggregation/Aggregation.php b/src/Aggregation/Aggregation.php index acbd2f2..8647d40 100644 --- a/src/Aggregation/Aggregation.php +++ b/src/Aggregation/Aggregation.php @@ -39,35 +39,17 @@ public function addSubAggregation(AggregationInterface $subAggregation): self public function toRequestArray(): array { - $request = [ - $this->name => $this->data, + $aggregationArray = [ + $this->name => $this->data ]; - if (!empty($this->subAggregations)) { - foreach ($this->subAggregations as $subAggregation) { - $request[$this->name]['aggs'][$subAggregation->getName()] = $this->handleSubAggregationData($subAggregation); - } + foreach ($this->subAggregations as $subAggregation) { + $aggregationArray[$this->name]['aggs'] = array_merge( + $aggregationArray[$this->name]['aggs'] ?? [], + $subAggregation->toRequestArray() + ); } - return $request; - } - - private function handleSubAggregationData(Aggregation $aggregation): array - { - $request = $aggregation->getData(); - - if (!empty($aggregation->subAggregations)) { - foreach ($aggregation->subAggregations as $subAggregation) { - $subAggregationData = $subAggregation->getData(); - - $request['aggs'][$subAggregation->getName()] = $subAggregationData; - - if (!empty($subAggregation->getSubAggregations())) { - $subAggregationData['aggs'] = $this->handleSubAggregationData($subAggregation); - } - } - } - - return $request; + return $aggregationArray; } } diff --git a/src/Builder/Builder.php b/src/Builder/Builder.php index a39544f..5e4a8d8 100644 --- a/src/Builder/Builder.php +++ b/src/Builder/Builder.php @@ -113,6 +113,14 @@ public function whereIn(string $field, array $values): self return $this; } + public function whereNotIn(string $field, array $values): self + { + $this->query['bool']['must_not'][]['terms'][$field] = $values; + + return $this; + } + + public function orWhereIn(string $field, array $values): self { $this->query['bool']['should'][]['terms'][$field] = $values; @@ -433,7 +441,7 @@ public function getQuery(): array public function getSort(): array { - return $this->sort; + return $this->sort ?? []; } public function getSelect(): array diff --git a/src/Bulk/Bulk.php b/src/Bulk/Bulk.php index d0a2f9a..e1c075a 100644 --- a/src/Bulk/Bulk.php +++ b/src/Bulk/Bulk.php @@ -37,7 +37,7 @@ public function addMany(array $documents): self public function getDocuments(): array { - return $this->documents; + return $this->documents ?? []; } /** @@ -55,7 +55,7 @@ public function toRequestJson(): string $body[] = json_encode($document, JSON_THROW_ON_ERROR); } - return join(PHP_EOL, $body) . PHP_EOL; + return implode(PHP_EOL, $body) . PHP_EOL; } /*