Skip to content

Commit 0ff28b7

Browse files
Add geotile_grid transform group by (#4917) (#4938)
Relates: elastic/elasticsearch#56514 Co-authored-by: Russ Cam <russ.cam@elastic.co>
1 parent 61ce7cc commit 0ff28b7

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
using Elasticsearch.Net.Utf8Json;
4+
5+
namespace Nest
6+
{
7+
/// <summary>
8+
/// The geotile grid value source works on geo_point fields and groups points into buckets that represent
9+
/// cells in a grid. The resulting grid can be sparse and only contains cells that have matching data.
10+
/// <para />
11+
/// Available in Elasticsearch 7.9.0+
12+
/// </summary>
13+
[InterfaceDataContract]
14+
public interface IGeoTileGridGroupSource : ISingleGroupSource
15+
{
16+
/// <summary>
17+
/// The highest-precision geotile of length 29 produces cells that cover less than 10cm by 10cm of land.
18+
/// This precision is uniquely suited for composite aggregations as each tile does not have to be
19+
/// generated and loaded in memory.
20+
/// </summary>
21+
[DataMember(Name = "precision")]
22+
GeoTilePrecision? Precision { get; set; }
23+
24+
/// <summary>
25+
/// Constrained to a specific geo bounding box, which reduces the range of tiles used.
26+
/// These bounds are useful when only a specific part of a geographical area needs high precision tiling.
27+
/// <para />
28+
/// Available in Elasticsearch 7.6.0+.
29+
/// </summary>
30+
[DataMember(Name = "bounds")]
31+
IBoundingBox Bounds { get; set; }
32+
}
33+
34+
/// <inheritdoc cref="IGeoTileGridGroupSource" />
35+
public class GeoTileGridGroupSource : SingleGroupSourceBase, IGeoTileGridGroupSource
36+
{
37+
/// <inheritdoc />
38+
public GeoTilePrecision? Precision { get; set; }
39+
/// <inheritdoc />
40+
public IBoundingBox Bounds { get; set; }
41+
}
42+
43+
/// <inheritdoc cref="IGeoTileGridGroupSource" />
44+
public class GeoTileGridGroupSourceDescriptor<T>
45+
: SingleGroupSourceDescriptorBase<GeoTileGridGroupSourceDescriptor<T>, IGeoTileGridGroupSource, T>,
46+
IGeoTileGridGroupSource
47+
{
48+
GeoTilePrecision? IGeoTileGridGroupSource.Precision { get; set; }
49+
IBoundingBox IGeoTileGridGroupSource.Bounds { get; set; }
50+
51+
/// <inheritdoc cref="IGeoTileGridGroupSource.Precision"/>
52+
public GeoTileGridGroupSourceDescriptor<T> Precision(GeoTilePrecision? precision) =>
53+
Assign(precision, (a, v) => a.Precision = v);
54+
55+
/// <inheritdoc cref="IGeoTileGridGroupSource.Bounds"/>
56+
public GeoTileGridGroupSourceDescriptor<T> Bounds(Func<BoundingBoxDescriptor, IBoundingBox> selector) =>
57+
Assign(selector, (a, v) => a.Bounds = v?.Invoke(new BoundingBoxDescriptor()));
58+
}
59+
}

src/Nest/XPack/Transform/Pivot/SingleGroupSource.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ public SingleGroupSourcesDescriptor<T> DateHistogram(string name,
8686
Func<DateHistogramGroupSourceDescriptor<T>, IDateHistogramGroupSource> selector
8787
) =>
8888
Assign(new Tuple<string, IDateHistogramGroupSource>(name, selector?.Invoke(new DateHistogramGroupSourceDescriptor<T>())), (a, v) => a.Add(v.Item1, v.Item2));
89+
90+
/// <inheritdoc cref="IGeoTileGridGroupSource" />
91+
public SingleGroupSourcesDescriptor<T> GeoTileGrid(string name,
92+
Func<GeoTileGridGroupSourceDescriptor<T>, IGeoTileGridGroupSource> selector
93+
) =>
94+
Assign(new Tuple<string, IGeoTileGridGroupSource>(name, selector?.Invoke(new GeoTileGridGroupSourceDescriptor<T>())), (a, v) => a.Add(v.Item1, v.Item2));
8995
}
9096

9197
internal class SingleGroupSourceFormatter : IJsonFormatter<ISingleGroupSource>
@@ -95,6 +101,7 @@ internal class SingleGroupSourceFormatter : IJsonFormatter<ISingleGroupSource>
95101
{ "terms", 0 },
96102
{ "date_histogram", 1 },
97103
{ "histogram", 2 },
104+
{ "geotile_grid", 3 },
98105
};
99106

100107
public void Serialize(ref JsonWriter writer, ISingleGroupSource value, IJsonFormatterResolver formatterResolver)
@@ -121,6 +128,10 @@ public void Serialize(ref JsonWriter writer, ISingleGroupSource value, IJsonForm
121128
writer.WritePropertyName("histogram");
122129
Serialize(ref writer, histogramGroupSource, formatterResolver);
123130
break;
131+
case IGeoTileGridGroupSource geoTileGridGroupSource:
132+
writer.WritePropertyName("geotile_grid");
133+
Serialize(ref writer, geoTileGridGroupSource, formatterResolver);
134+
break;
124135
default:
125136
throw new JsonParsingException($"Unknown {nameof(ISingleGroupSource)}: {value.GetType().Name}");
126137
}
@@ -164,6 +175,10 @@ public ISingleGroupSource Deserialize(ref JsonReader reader, IJsonFormatterResol
164175
groupSource = formatterResolver.GetFormatter<HistogramGroupSource>()
165176
.Deserialize(ref reader, formatterResolver);
166177
break;
178+
case 3:
179+
groupSource = formatterResolver.GetFormatter<GeoTileGridGroupSource>()
180+
.Deserialize(ref reader, formatterResolver);
181+
break;
167182
}
168183
}
169184
else

tests/Tests/XPack/Transform/TransformApiTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Tests.XPack.Transform
1515
{
16-
[SkipVersion("<7.7.0", "Introduced in 7.7.0")]
16+
[SkipVersion("<7.9.0", "Geotile grid group by introduced in 7.9.0")]
1717
public class TransformApiTests : CoordinatedIntegrationTestBase<WritableCluster>
1818
{
1919
private const string PutTransformStep = nameof(PutTransformStep);
@@ -61,6 +61,18 @@ public TransformApiTests(WritableCluster cluster, EndpointUsage usage) : base(ne
6161
Field = Field<Project>(f => f.StartedOn),
6262
CalendarInterval = DateInterval.Week
6363
}
64+
},
65+
{
66+
"geotile", new GeoTileGridGroupSource
67+
{
68+
Field = Field<Project>(f => f.LocationPoint),
69+
Precision = GeoTilePrecision.Precision6,
70+
Bounds = new BoundingBox
71+
{
72+
TopLeft = new GeoLocation(-90, 180),
73+
BottomRight = new GeoLocation(90, -180)
74+
}
75+
}
6476
}
6577
}
6678
}
@@ -92,6 +104,14 @@ public TransformApiTests(WritableCluster cluster, EndpointUsage usage) : base(ne
92104
.Field(f => f.StartedOn)
93105
.CalendarInterval(DateInterval.Week)
94106
)
107+
.GeoTileGrid("geotile", gtg => gtg
108+
.Field(f => f.LocationPoint)
109+
.Precision(GeoTilePrecision.Precision6)
110+
.Bounds(b => b
111+
.TopLeft(-90, 180)
112+
.BottomRight(90, -180)
113+
)
114+
)
95115
)
96116
),
97117
(v, c, f) => c.Transform.Put(v, f),

0 commit comments

Comments
 (0)