Skip to content

Commit bf599d6

Browse files
committed
Merge pull request #11042 from jpountz/feature/aggs_missing
Aggs: Make it possible to configure missing values.
2 parents 3e215e7 + 32e23b9 commit bf599d6

25 files changed

+1311
-48
lines changed

docs/reference/aggregations/bucket/datehistogram-aggregation.asciidoc

+23
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,26 @@ settings and filter the returned buckets based on a `min_doc_count` setting (by
123123
bucket that matches documents and the last one are returned). This histogram also supports the `extended_bounds`
124124
setting, which enables extending the bounds of the histogram beyond the data itself (to read more on why you'd want to
125125
do that please refer to the explanation <<search-aggregations-bucket-histogram-aggregation-extended-bounds,here>>).
126+
127+
==== Missing value
128+
129+
The `missing` parameter defines how documents that are missing a value should be treated.
130+
By default they will be ignored but it is also possible to treat them as if they
131+
had a value.
132+
133+
[source,js]
134+
--------------------------------------------------
135+
{
136+
"aggs" : {
137+
"publish_date" : {
138+
"datehistogram" : {
139+
"field" : "publish_date",
140+
"interval": "year",
141+
"missing": "2000-01-01" <1>
142+
}
143+
}
144+
}
145+
}
146+
--------------------------------------------------
147+
148+
<1> Documents without a value in the `publish_date` field will fall into the same bucket as documents that have the value `2000-01-01`.

docs/reference/aggregations/bucket/histogram-aggregation.asciidoc

+23
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,26 @@ Response:
317317
}
318318
}
319319
--------------------------------------------------
320+
321+
==== Missing value
322+
323+
The `missing` parameter defines how documents that are missing a value should be treated.
324+
By default they will be ignored but it is also possible to treat them as if they
325+
had a value.
326+
327+
[source,js]
328+
--------------------------------------------------
329+
{
330+
"aggs" : {
331+
"quantity" : {
332+
"histogram" : {
333+
"field" : "quantity",
334+
"interval": 10,
335+
"missing": 0 <1>
336+
}
337+
}
338+
}
339+
}
340+
--------------------------------------------------
341+
342+
<1> Documents without a value in the `quantity` field will fall into the same bucket as documents that have the value `0`.

docs/reference/aggregations/bucket/terms-aggregation.asciidoc

+22
Original file line numberDiff line numberDiff line change
@@ -655,3 +655,25 @@ in inner aggregations.
655655
<1> experimental[] the possible values are `map`, `global_ordinals`, `global_ordinals_hash` and `global_ordinals_low_cardinality`
656656

657657
Please note that Elasticsearch will ignore this execution hint if it is not applicable and that there is no backward compatibility guarantee on these hints.
658+
659+
==== Missing value
660+
661+
The `missing` parameter defines how documents that are missing a value should be treated.
662+
By default they will be ignored but it is also possible to treat them as if they
663+
had a value.
664+
665+
[source,js]
666+
--------------------------------------------------
667+
{
668+
"aggs" : {
669+
"tags" : {
670+
"terms" : {
671+
"field" : "tags",
672+
"missing": "N/A" <1>
673+
}
674+
}
675+
}
676+
}
677+
--------------------------------------------------
678+
679+
<1> Documents without a value in the `tags` field will fall into the same bucket as documents that have the value `N/A`.

docs/reference/aggregations/metrics/avg-aggregation.asciidoc

+23-1
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,26 @@ It turned out that the exam was way above the level of the students and a grade
7272
}
7373
}
7474
}
75-
--------------------------------------------------
75+
--------------------------------------------------
76+
77+
==== Missing value
78+
79+
The `missing` parameter defines how documents that are missing a value should be treated.
80+
By default they will be ignored but it is also possible to treat them as if they
81+
had a value.
82+
83+
[source,js]
84+
--------------------------------------------------
85+
{
86+
"aggs" : {
87+
"grade_avg" : {
88+
"avg" : {
89+
"field" : "grade",
90+
"missing": 10 <1>
91+
}
92+
}
93+
}
94+
}
95+
--------------------------------------------------
96+
97+
<1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `10`.

docs/reference/aggregations/metrics/cardinality-aggregation.asciidoc

+21
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,24 @@ however since hashes need to be computed on the fly.
155155

156156
TIP: The `script` parameter expects an inline script. Use `script_id` for indexed scripts and `script_file` for scripts in the `config/scripts/` directory.
157157

158+
==== Missing value
159+
160+
The `missing` parameter defines how documents that are missing a value should be treated.
161+
By default they will be ignored but it is also possible to treat them as if they
162+
had a value.
163+
164+
[source,js]
165+
--------------------------------------------------
166+
{
167+
"aggs" : {
168+
"tag_cardinality" : {
169+
"cardinality" : {
170+
"field" : "tag",
171+
"missing": "N/A" <1>
172+
}
173+
}
174+
}
175+
}
176+
--------------------------------------------------
177+
178+
<1> Documents without a value in the `tag` field will fall into the same bucket as documents that have the value `N/A`.

docs/reference/aggregations/metrics/extendedstats-aggregation.asciidoc

+23-1
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,26 @@ It turned out that the exam was way above the level of the students and a grade
116116
}
117117
}
118118
}
119-
--------------------------------------------------
119+
--------------------------------------------------
120+
121+
==== Missing value
122+
123+
The `missing` parameter defines how documents that are missing a value should be treated.
124+
By default they will be ignored but it is also possible to treat them as if they
125+
had a value.
126+
127+
[source,js]
128+
--------------------------------------------------
129+
{
130+
"aggs" : {
131+
"grades_stats" : {
132+
"extended_stats" : {
133+
"field" : "grade",
134+
"missing": 0 <1>
135+
}
136+
}
137+
}
138+
}
139+
--------------------------------------------------
140+
141+
<1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `0`.

docs/reference/aggregations/metrics/max-aggregation.asciidoc

+21
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,24 @@ Let's say that the prices of the documents in our index are in USD, but we would
6767
}
6868
--------------------------------------------------
6969

70+
==== Missing value
71+
72+
The `missing` parameter defines how documents that are missing a value should be treated.
73+
By default they will be ignored but it is also possible to treat them as if they
74+
had a value.
75+
76+
[source,js]
77+
--------------------------------------------------
78+
{
79+
"aggs" : {
80+
"grade_max" : {
81+
"max" : {
82+
"field" : "grade",
83+
"missing": 10 <1>
84+
}
85+
}
86+
}
87+
}
88+
--------------------------------------------------
89+
90+
<1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `10`.

docs/reference/aggregations/metrics/min-aggregation.asciidoc

+22
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,25 @@ Let's say that the prices of the documents in our index are in USD, but we would
6666
}
6767
}
6868
--------------------------------------------------
69+
70+
==== Missing value
71+
72+
The `missing` parameter defines how documents that are missing a value should be treated.
73+
By default they will be ignored but it is also possible to treat them as if they
74+
had a value.
75+
76+
[source,js]
77+
--------------------------------------------------
78+
{
79+
"aggs" : {
80+
"grade_min" : {
81+
"min" : {
82+
"field" : "grade",
83+
"missing": 10 <1>
84+
}
85+
}
86+
}
87+
}
88+
--------------------------------------------------
89+
90+
<1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `10`.

docs/reference/aggregations/metrics/percentile-aggregation.asciidoc

+22
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,25 @@ A "node" uses roughly 32 bytes of memory, so under worst-case scenarios (large a
190190
of data which arrives sorted and in-order) the default settings will produce a
191191
TDigest roughly 64KB in size. In practice data tends to be more random and
192192
the TDigest will use less memory.
193+
194+
==== Missing value
195+
196+
The `missing` parameter defines how documents that are missing a value should be treated.
197+
By default they will be ignored but it is also possible to treat them as if they
198+
had a value.
199+
200+
[source,js]
201+
--------------------------------------------------
202+
{
203+
"aggs" : {
204+
"grade_percentiles" : {
205+
"percentiles" : {
206+
"field" : "grade",
207+
"missing": 10 <1>
208+
}
209+
}
210+
}
211+
}
212+
--------------------------------------------------
213+
214+
<1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `10`.

docs/reference/aggregations/metrics/percentile-rank-aggregation.asciidoc

+22
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,25 @@ script to generate values which percentile ranks are calculated on
8686
<2> Scripting supports parameterized input just like any other script
8787

8888
TIP: The `script` parameter expects an inline script. Use `script_id` for indexed scripts and `script_file` for scripts in the `config/scripts/` directory.
89+
90+
==== Missing value
91+
92+
The `missing` parameter defines how documents that are missing a value should be treated.
93+
By default they will be ignored but it is also possible to treat them as if they
94+
had a value.
95+
96+
[source,js]
97+
--------------------------------------------------
98+
{
99+
"aggs" : {
100+
"grade_ranks" : {
101+
"percentile_ranks" : {
102+
"field" : "grade",
103+
"missing": 10 <1>
104+
}
105+
}
106+
}
107+
}
108+
--------------------------------------------------
109+
110+
<1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `10`.

docs/reference/aggregations/metrics/stats-aggregation.asciidoc

+23-1
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,26 @@ It turned out that the exam was way above the level of the students and a grade
7878
}
7979
}
8080
}
81-
--------------------------------------------------
81+
--------------------------------------------------
82+
83+
==== Missing value
84+
85+
The `missing` parameter defines how documents that are missing a value should be treated.
86+
By default they will be ignored but it is also possible to treat them as if they
87+
had a value.
88+
89+
[source,js]
90+
--------------------------------------------------
91+
{
92+
"aggs" : {
93+
"grades_stats" : {
94+
"stats" : {
95+
"field" : "grade",
96+
"missing": 0 <1>
97+
}
98+
}
99+
}
100+
}
101+
--------------------------------------------------
102+
103+
<1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `0`.

docs/reference/aggregations/metrics/sum-aggregation.asciidoc

+22
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,25 @@ Computing the sum of squares over all stock tick changes:
7777
}
7878
}
7979
--------------------------------------------------
80+
81+
==== Missing value
82+
83+
The `missing` parameter defines how documents that are missing a value should be treated.
84+
By default they will be ignored but it is also possible to treat them as if they
85+
had a value.
86+
87+
[source,js]
88+
--------------------------------------------------
89+
{
90+
"aggs" : {
91+
"total_time" : {
92+
"sum" : {
93+
"field" : "took",
94+
"missing": 100 <1>
95+
}
96+
}
97+
}
98+
}
99+
--------------------------------------------------
100+
101+
<1> Documents without a value in the `took` field will fall into the same bucket as documents that have the value `100`.

src/main/java/org/elasticsearch/common/geo/GeoUtils.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -409,19 +409,24 @@ public static GeoPoint parseGeoPoint(XContentParser parser, GeoPoint point) thro
409409
return point.reset(lat, lon);
410410
} else if(parser.currentToken() == Token.VALUE_STRING) {
411411
String data = parser.text();
412-
int comma = data.indexOf(',');
413-
if(comma > 0) {
414-
lat = Double.parseDouble(data.substring(0, comma).trim());
415-
lon = Double.parseDouble(data.substring(comma + 1).trim());
416-
return point.reset(lat, lon);
417-
} else {
418-
return point.resetFromGeoHash(data);
419-
}
412+
return parseGeoPoint(data, point);
420413
} else {
421414
throw new ElasticsearchParseException("geo_point expected");
422415
}
423416
}
424417

418+
/** parse a {@link GeoPoint} from a String */
419+
public static GeoPoint parseGeoPoint(String data, GeoPoint point) {
420+
int comma = data.indexOf(',');
421+
if(comma > 0) {
422+
double lat = Double.parseDouble(data.substring(0, comma).trim());
423+
double lon = Double.parseDouble(data.substring(comma + 1).trim());
424+
return point.reset(lat, lon);
425+
} else {
426+
return point.resetFromGeoHash(data);
427+
}
428+
}
429+
425430
private GeoUtils() {
426431
}
427432
}

src/main/java/org/elasticsearch/search/aggregations/ValuesSourceAggregationBuilder.java

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public abstract class ValuesSourceAggregationBuilder<B extends ValuesSourceAggre
3434
private String script;
3535
private String lang;
3636
private Map<String, Object> params;
37+
private Object missing;
3738

3839
/**
3940
* Constructs a new builder.
@@ -117,6 +118,14 @@ public B params(Map<String, Object> params) {
117118
return (B) this;
118119
}
119120

121+
/**
122+
* Configure the value to use when documents miss a value.
123+
*/
124+
public B missing(Object missingValue) {
125+
this.missing = missingValue;
126+
return (B) this;
127+
}
128+
120129
@Override
121130
protected final XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
122131
builder.startObject();
@@ -132,6 +141,9 @@ protected final XContentBuilder internalXContent(XContentBuilder builder, Params
132141
if (this.params != null) {
133142
builder.field("params").map(this.params);
134143
}
144+
if (missing != null) {
145+
builder.field("missing", missing);
146+
}
135147

136148
doInternalXContent(builder, params);
137149
return builder.endObject();

0 commit comments

Comments
 (0)