Skip to content

Commit

Permalink
Fix aggregation and bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
olekjs committed Oct 5, 2024
1 parent 1dd6736 commit 1a8d08b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
34 changes: 8 additions & 26 deletions src/Aggregation/Aggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
10 changes: 9 additions & 1 deletion src/Builder/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -433,7 +441,7 @@ public function getQuery(): array

public function getSort(): array
{
return $this->sort;
return $this->sort ?? [];
}

public function getSelect(): array
Expand Down
4 changes: 2 additions & 2 deletions src/Bulk/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function addMany(array $documents): self

public function getDocuments(): array
{
return $this->documents;
return $this->documents ?? [];
}

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

/*
Expand Down

0 comments on commit 1a8d08b

Please sign in to comment.