Skip to content

Commit 8cc445d

Browse files
leflingsruflin
authored andcommitted
Allow metadata to be set on AbstractAggregation (#1677) (#1680)
ElasticSearch allows metadata to be set on an aggregation that will be returned as-is with the aggregation result. See https://www.elastic.co/guide/en/elasticsearch/reference/6.8/agg-metadata.html Using `setParam` generates invalid query, hence these additions. See #1677
1 parent 9a3c82e commit 8cc445d

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ All notable changes to this project will be documented in this file based on the
3737
* Move Client configuration in a dedicated class
3838
* Added `callable` type hinting to `$callback` in `Client` constructor. [#1659](https://github.com/ruflin/Elastica/pull/1659)
3939
* Added `setTrackTotalHits` method to `Elastica\Query`[#1663](https://github.com/ruflin/Elastica/issues/1663)
40+
* Allow metadata to be set on Aggregations (via `AbstractAggregation::setMeta(array)`). [#1677](https://github.com/ruflin/Elastica/issues/1677)
4041

4142
### Improvements
4243
* Added `native_function_invocation` CS rule [#1606](https://github.com/ruflin/Elastica/pull/1606)

lib/Elastica/Aggregation/AbstractAggregation.php

+54
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
abstract class AbstractAggregation extends Param implements NameableInterface
1010
{
11+
protected const METADATA_KEY = 'meta';
12+
1113
/**
1214
* @var string The name of this aggregation
1315
*/
@@ -80,6 +82,58 @@ public function addAggregation(AbstractAggregation $aggregation): self
8082
return $this;
8183
}
8284

85+
/**
86+
* Add metadata to the aggregation.
87+
*
88+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/agg-metadata.html
89+
* @see \Elastica\Aggregation\AbstractAggregation::getMeta()
90+
* @see \Elastica\Aggregation\AbstractAggregation::clearMeta()
91+
*
92+
* @param array $meta Metadata to be attached to the aggregation
93+
*
94+
* @return $this
95+
*/
96+
public function setMeta(array $meta): self
97+
{
98+
if (empty($meta)) {
99+
return $this->clearMeta();
100+
}
101+
102+
$this->_setRawParam(self::METADATA_KEY, $meta);
103+
104+
return $this;
105+
}
106+
107+
/**
108+
* Retrieve the currently configured metadata for the aggregation.
109+
*
110+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/agg-metadata.html
111+
* @see \Elastica\Aggregation\AbstractAggregation::setMeta()
112+
* @see \Elastica\Aggregation\AbstractAggregation::clearMeta()
113+
*
114+
* @return array|null
115+
*/
116+
public function getMeta(): ?array
117+
{
118+
return $this->_rawParams[self::METADATA_KEY] ?? null;
119+
}
120+
121+
/**
122+
* Clears any previously set metadata for this aggregation.
123+
*
124+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/agg-metadata.html
125+
* @see \Elastica\Aggregation\AbstractAggregation::setMeta()
126+
* @see \Elastica\Aggregation\AbstractAggregation::getMeta()
127+
*
128+
* @return $this
129+
*/
130+
public function clearMeta(): self
131+
{
132+
unset($this->_rawParams[self::METADATA_KEY]);
133+
134+
return $this;
135+
}
136+
83137
/**
84138
* @return array
85139
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Elastica\Test\Aggregation;
4+
5+
use Elastica\Aggregation\Cardinality;
6+
use Elastica\Query;
7+
8+
class AggregationMetadataTest extends BaseAggregationTest
9+
{
10+
protected function _getIndexForTest()
11+
{
12+
$index = $this->_createIndex();
13+
14+
$index->refresh();
15+
16+
return $index;
17+
}
18+
19+
/**
20+
* @group functional
21+
*/
22+
public function testAggregationSimpleMetadata()
23+
{
24+
$aggName = 'mock';
25+
$metadata = ['color' => 'blue'];
26+
27+
$agg = new Cardinality($aggName);
28+
$agg->setField('mock_field');
29+
$agg->setMeta($metadata);
30+
31+
$query = new Query();
32+
$query->addAggregation($agg);
33+
$results = $this->_getIndexForTest()->search($query)->getAggregation($aggName);
34+
35+
$this->assertEquals($metadata, $results['meta']);
36+
}
37+
38+
/**
39+
* @group functional
40+
*/
41+
public function testAggregationComplexMetadata()
42+
{
43+
$aggName = 'mock';
44+
$metadata = [
45+
'color' => 'blue',
46+
'status' => 'green',
47+
'users' => [
48+
'foo' => 'bar',
49+
'moo' => 'baz',
50+
],
51+
];
52+
53+
$agg = new Cardinality($aggName);
54+
$agg->setField('mock_field');
55+
$agg->setMeta($metadata);
56+
57+
$query = new Query();
58+
$query->addAggregation($agg);
59+
$results = $this->_getIndexForTest()->search($query)->getAggregation($aggName);
60+
61+
$this->assertEquals($metadata, $results['meta']);
62+
}
63+
}

0 commit comments

Comments
 (0)