Skip to content

Commit 5af6f0b

Browse files
authored
Merge pull request #16 from olekjs/fix-aggregation
Fix aggregation and bulk
2 parents 1dd6736 + 1a8d08b commit 5af6f0b

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
lines changed

src/Aggregation/Aggregation.php

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,17 @@ public function addSubAggregation(AggregationInterface $subAggregation): self
3939

4040
public function toRequestArray(): array
4141
{
42-
$request = [
43-
$this->name => $this->data,
42+
$aggregationArray = [
43+
$this->name => $this->data
4444
];
4545

46-
if (!empty($this->subAggregations)) {
47-
foreach ($this->subAggregations as $subAggregation) {
48-
$request[$this->name]['aggs'][$subAggregation->getName()] = $this->handleSubAggregationData($subAggregation);
49-
}
46+
foreach ($this->subAggregations as $subAggregation) {
47+
$aggregationArray[$this->name]['aggs'] = array_merge(
48+
$aggregationArray[$this->name]['aggs'] ?? [],
49+
$subAggregation->toRequestArray()
50+
);
5051
}
5152

52-
return $request;
53-
}
54-
55-
private function handleSubAggregationData(Aggregation $aggregation): array
56-
{
57-
$request = $aggregation->getData();
58-
59-
if (!empty($aggregation->subAggregations)) {
60-
foreach ($aggregation->subAggregations as $subAggregation) {
61-
$subAggregationData = $subAggregation->getData();
62-
63-
$request['aggs'][$subAggregation->getName()] = $subAggregationData;
64-
65-
if (!empty($subAggregation->getSubAggregations())) {
66-
$subAggregationData['aggs'] = $this->handleSubAggregationData($subAggregation);
67-
}
68-
}
69-
}
70-
71-
return $request;
53+
return $aggregationArray;
7254
}
7355
}

src/Builder/Builder.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ public function whereIn(string $field, array $values): self
113113
return $this;
114114
}
115115

116+
public function whereNotIn(string $field, array $values): self
117+
{
118+
$this->query['bool']['must_not'][]['terms'][$field] = $values;
119+
120+
return $this;
121+
}
122+
123+
116124
public function orWhereIn(string $field, array $values): self
117125
{
118126
$this->query['bool']['should'][]['terms'][$field] = $values;
@@ -433,7 +441,7 @@ public function getQuery(): array
433441

434442
public function getSort(): array
435443
{
436-
return $this->sort;
444+
return $this->sort ?? [];
437445
}
438446

439447
public function getSelect(): array

src/Bulk/Bulk.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function addMany(array $documents): self
3737

3838
public function getDocuments(): array
3939
{
40-
return $this->documents;
40+
return $this->documents ?? [];
4141
}
4242

4343
/**
@@ -55,7 +55,7 @@ public function toRequestJson(): string
5555
$body[] = json_encode($document, JSON_THROW_ON_ERROR);
5656
}
5757

58-
return join(PHP_EOL, $body) . PHP_EOL;
58+
return implode(PHP_EOL, $body) . PHP_EOL;
5959
}
6060

6161
/*

0 commit comments

Comments
 (0)