Skip to content

Commit acad200

Browse files
authored
[Backport master] Add geo-line aggregration (#5289)
* Add geo-line aggregration (#5286) * Add geo-line aggregation and initial test * Make AggregationUsageTestBase generic * Add documentation (cherry picked from commit 3312afe) * Fixup * Disable MovingAverage Tests in 8.0 These will be removed and trackked in #5301
1 parent 7d556c9 commit acad200

File tree

77 files changed

+470
-199
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+470
-199
lines changed

docs/aggregations.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ The values are typically extracted from the fields of the document (using the fi
4646

4747
* <<geo-centroid-aggregation-usage,Geo Centroid Aggregation Usage>>
4848

49+
* <<geo-line-aggregation-usage,Geo Line Aggregation Usage>>
50+
4951
* <<max-aggregation-usage,Max Aggregation Usage>>
5052

5153
* <<median-absolute-deviation-aggregation-usage,Median Absolute Deviation Aggregation Usage>>
@@ -92,6 +94,8 @@ include::aggregations/metric/geo-bounds/geo-bounds-aggregation-usage.asciidoc[]
9294

9395
include::aggregations/metric/geo-centroid/geo-centroid-aggregation-usage.asciidoc[]
9496

97+
include::aggregations/metric/geo-line/geo-line-aggregation-usage.asciidoc[]
98+
9599
include::aggregations/metric/max/max-aggregation-usage.asciidoc[]
96100

97101
include::aggregations/metric/median-absolute-deviation/median-absolute-deviation-aggregation-usage.asciidoc[]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/7.11
2+
3+
:github: https://github.com/elastic/elasticsearch-net
4+
5+
:nuget: https://www.nuget.org/packages
6+
7+
////
8+
IMPORTANT NOTE
9+
==============
10+
This file has been generated from https://github.com/elastic/elasticsearch-net/tree/7.x/src/Tests/Tests/Aggregations/Metric/GeoLine/GeoLineAggregationUsageTests.cs.
11+
If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file,
12+
please modify the original csharp file found at the link and submit the PR with that change. Thanks!
13+
////
14+
15+
[[geo-line-aggregation-usage]]
16+
=== Geo Line Aggregation Usage
17+
18+
The geo_line aggregation aggregates all geo_point values within a bucket into a LineString ordered by the chosen sort field.
19+
20+
Be sure to read the Elasticsearch documentation on {ref_current}/search-aggregations-metrics-geo-line.html[Geo-Line Aggregation].
21+
22+
==== Fluent DSL example
23+
24+
[source,csharp]
25+
----
26+
a => a
27+
.GeoLine("line", d => d
28+
.Point(p => p.LocationPoint)
29+
.Sort(p => p.StartedOn)
30+
.IncludeSort()
31+
.Size(25))
32+
----
33+
34+
==== Object Initializer syntax example
35+
36+
[source,csharp]
37+
----
38+
new GeoLineAggregation("line", Field<Project>(f => f.LocationPoint), Field<Project>(f => f.StartedOn))
39+
{
40+
IncludeSort = true,
41+
Size = 25
42+
}
43+
----
44+
45+
[source,javascript]
46+
.Example json output
47+
----
48+
{
49+
"line": {
50+
"geo_line": {
51+
"point": {
52+
"field": "locationPoint"
53+
},
54+
"sort": {
55+
"field": "startedOn"
56+
},
57+
"include_sort": true,
58+
"size": 25
59+
}
60+
}
61+
}
62+
----
63+
64+
==== Handling Responses
65+
66+
[source,csharp]
67+
----
68+
response.ShouldBeValid();
69+
var geoLine = response.Aggregations.GeoLine("line");
70+
geoLine.Should().NotBeNull();
71+
geoLine.Type.Should().Be("Feature");
72+
geoLine.Geometry.Type.Should().Be("linestring");
73+
geoLine.Geometry.Coordinates.Should().NotBeEmpty();
74+
geoLine.Properties.Complete.Should().BeFalse();
75+
geoLine.Properties.SortValues.Should().NotBeEmpty();
76+
----
77+

src/Nest/Aggregations/AggregateDictionary.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ public ScriptedMetricAggregate ScriptedMetric(string key)
9393

9494
public GeoBoundsAggregate GeoBounds(string key) => TryGet<GeoBoundsAggregate>(key);
9595

96+
public GeoLineAggregate GeoLine(string key) => TryGet<GeoLineAggregate>(key);
97+
9698
public PercentilesAggregate Percentiles(string key) => TryGet<PercentilesAggregate>(key);
9799

98100
public PercentilesAggregate PercentilesBucket(string key) => TryGet<PercentilesAggregate>(key);

src/Nest/Aggregations/AggregateDictionaryFormatter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ Dictionary<string, IAggregate> dictionary
5656
case "geo_centroid":
5757
ReadAggregate<GeoCentroidAggregate>(ref reader, formatterResolver, name, dictionary);
5858
break;
59+
case "geo_line":
60+
ReadAggregate<GeoLineAggregate>(ref reader, formatterResolver, name, dictionary);
61+
break;
5962
default:
6063
//still fall back to heuristics based parsed in case we do not know the key
6164
ParseAggregate(ref reader, formatterResolver, name, dictionary);

src/Nest/Aggregations/AggregateFormatter.cs

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ internal class AggregateFormatter : IJsonFormatter<IAggregate>
3535
{ Parser.BottomRight, 1 },
3636
};
3737

38+
private static readonly AutomataDictionary GeoLineFields = new AutomataDictionary
39+
{
40+
{ Parser.Geometry, 0 },
41+
{ Parser.Properties, 1 },
42+
};
43+
3844
private static readonly byte[] KeysField = JsonWriter.GetEncodedPropertyNameWithoutQuotation(Parser.Keys);
3945
private static readonly byte[] MetaField = JsonWriter.GetEncodedPropertyNameWithoutQuotation(Parser.Meta);
4046
private static readonly byte[] MinLengthField = JsonWriter.GetEncodedPropertyNameWithoutQuotation(Parser.MinLength);
@@ -54,6 +60,7 @@ internal class AggregateFormatter : IJsonFormatter<IAggregate>
5460
{ Parser.Fields, 10 },
5561
{ Parser.Min, 11 },
5662
{ Parser.Top, 12 },
63+
{ Parser.Type, 13 }
5764
};
5865

5966
private static readonly byte[] SumOtherDocCount = JsonWriter.GetEncodedPropertyNameWithoutQuotation(Parser.SumOtherDocCount);
@@ -170,6 +177,9 @@ private IAggregate ReadAggregate(ref JsonReader reader, IJsonFormatterResolver f
170177
case 12:
171178
aggregate = GetTopMetricsAggregate(ref reader, formatterResolver, meta);
172179
break;
180+
case 13:
181+
aggregate = GetGeoLineAggregate(ref reader, formatterResolver, meta);
182+
break;
173183
}
174184
}
175185
else
@@ -269,7 +279,7 @@ private IAggregate GetBoxplotAggregate(ref JsonReader reader, IReadOnlyDictionar
269279
}
270280

271281
return boxplot;
272-
}
282+
}
273283

274284
private IAggregate GetTopMetricsAggregate(ref JsonReader reader, IJsonFormatterResolver formatterResolver, IReadOnlyDictionary<string, object> meta)
275285
{
@@ -373,6 +383,45 @@ private IAggregate GetGeoBoundsAggregate(ref JsonReader reader, IJsonFormatterRe
373383
return geoBoundsMetric;
374384
}
375385

386+
private IAggregate GetGeoLineAggregate(ref JsonReader reader, IJsonFormatterResolver formatterResolver, IReadOnlyDictionary<string, object> meta)
387+
{
388+
var geoLine = new GeoLineAggregate { Meta = meta };
389+
390+
if (reader.GetCurrentJsonToken() == JsonToken.Null)
391+
{
392+
reader.ReadNext();
393+
return geoLine;
394+
}
395+
396+
var geometryFormatter = formatterResolver.GetFormatter<LineStringGeoShape>();
397+
var propertiesFormatter = formatterResolver.GetFormatter<GeoLineProperties>();
398+
399+
geoLine.Type = reader.ReadString();
400+
reader.ReadNext();
401+
402+
for (var i = 0; i < 2; i++)
403+
{
404+
var propertyName = reader.ReadPropertyNameSegmentRaw();
405+
if (GeoLineFields.TryGetValue(propertyName, out var value))
406+
{
407+
switch (value)
408+
{
409+
case 0:
410+
geoLine.Geometry = geometryFormatter.Deserialize(ref reader, formatterResolver);
411+
reader.ReadNextBlock();
412+
break;
413+
case 1:
414+
geoLine.Properties = propertiesFormatter.Deserialize(ref reader, formatterResolver);
415+
break;
416+
}
417+
}
418+
else
419+
reader.ReadNextBlock();
420+
}
421+
422+
return geoLine;
423+
}
424+
376425
private IAggregate GetPercentilesAggregate(ref JsonReader reader, IReadOnlyDictionary<string, object> meta)
377426
{
378427
var metric = new PercentilesAggregate { Meta = meta };
@@ -940,17 +989,17 @@ private IBucket GetKeyedBucket(ref JsonReader reader, IJsonFormatterResolver for
940989
case Parser.Score:
941990
return GetSignificantTermsBucket(ref reader, formatterResolver, key, docCount);
942991
case Parser.DocCountErrorUpperBound:
943-
{
944-
docCountErrorUpperBound = reader.ReadNullableLong();
945-
token = reader.GetCurrentJsonToken();
946-
if (token == JsonToken.ValueSeparator)
947992
{
948-
reader.ReadNext(); // ,
949-
propertyName = reader.ReadPropertyName();
950-
subAggregates = GetSubAggregates(ref reader, propertyName, formatterResolver);
993+
docCountErrorUpperBound = reader.ReadNullableLong();
994+
token = reader.GetCurrentJsonToken();
995+
if (token == JsonToken.ValueSeparator)
996+
{
997+
reader.ReadNext(); // ,
998+
propertyName = reader.ReadPropertyName();
999+
subAggregates = GetSubAggregates(ref reader, propertyName, formatterResolver);
1000+
}
1001+
break;
9511002
}
952-
break;
953-
}
9541003
default:
9551004
subAggregates = GetSubAggregates(ref reader, propertyName, formatterResolver);
9561005
break;
@@ -1048,6 +1097,7 @@ private static class Parser
10481097
public const string Fields = "fields";
10491098
public const string From = "from";
10501099
public const string Top = "top";
1100+
public const string Type = "type";
10511101

10521102
public const string FromAsString = "from_as_string";
10531103
public const string Hits = "hits";
@@ -1074,6 +1124,9 @@ private static class Parser
10741124

10751125
public const string ValueAsString = "value_as_string";
10761126
public const string Values = "values";
1127+
1128+
public const string Geometry = "geometry";
1129+
public const string Properties = "properties";
10771130
}
10781131
}
10791132
}

src/Nest/Aggregations/AggregationContainer.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ public interface IAggregationContainer
162162
[DataMember(Name = "geohash_grid")]
163163
IGeoHashGridAggregation GeoHash { get; set; }
164164

165+
[DataMember(Name = "geo_line")]
166+
IGeoLineAggregation GeoLine { get; set; }
167+
165168
[DataMember(Name = "geotile_grid")]
166169
IGeoTileGridAggregation GeoTile { get; set; }
167170

@@ -356,6 +359,8 @@ public class AggregationContainer : IAggregationContainer
356359

357360
public IGeoHashGridAggregation GeoHash { get; set; }
358361

362+
public IGeoLineAggregation GeoLine { get; set; }
363+
359364
public IGeoTileGridAggregation GeoTile { get; set; }
360365

361366
public IGlobalAggregation Global { get; set; }
@@ -521,6 +526,8 @@ public class AggregationContainerDescriptor<T> : DescriptorBase<AggregationConta
521526

522527
IGeoHashGridAggregation IAggregationContainer.GeoHash { get; set; }
523528

529+
IGeoLineAggregation IAggregationContainer.GeoLine { get; set; }
530+
524531
IGeoTileGridAggregation IAggregationContainer.GeoTile { get; set; }
525532

526533
IGlobalAggregation IAggregationContainer.Global { get; set; }
@@ -663,6 +670,10 @@ Func<GeoHashGridAggregationDescriptor<T>, IGeoHashGridAggregation> selector
663670
) =>
664671
_SetInnerAggregation(name, selector, (a, d) => a.GeoHash = d);
665672

673+
public AggregationContainerDescriptor<T> GeoLine(string name,
674+
Func<GeoLineAggregationDescriptor<T>, IGeoLineAggregation> selector) =>
675+
_SetInnerAggregation(name, selector, (a, d) => a.GeoLine = d);
676+
666677
public AggregationContainerDescriptor<T> GeoTile(string name,
667678
Func<GeoTileGridAggregationDescriptor<T>, IGeoTileGridAggregation> selector
668679
) =>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Collections.Generic;
6+
using System.Runtime.Serialization;
7+
8+
namespace Nest
9+
{
10+
[DataContract]
11+
public class GeoLineAggregate : MetricAggregateBase
12+
{
13+
[DataMember(Name = "type")]
14+
public string Type { get; set; }
15+
16+
[DataMember(Name = "geometry")]
17+
public LineStringGeoShape Geometry { get; set; }
18+
19+
[DataMember(Name = "properties")]
20+
public GeoLineProperties Properties { get; set; }
21+
}
22+
23+
[DataContract]
24+
public class GeoLineProperties
25+
{
26+
[DataMember(Name = "complete")]
27+
public bool Complete { get; set; }
28+
29+
[DataMember(Name = "sort_values")]
30+
public IEnumerable<double> SortValues { get; set; }
31+
}
32+
}

0 commit comments

Comments
 (0)