Skip to content

Commit

Permalink
Add geotile_grid transform group by (#4917) (#4938)
Browse files Browse the repository at this point in the history
Relates: elastic/elasticsearch#56514

Co-authored-by: Russ Cam <russ.cam@elastic.co>
  • Loading branch information
github-actions[bot] and russcam authored Aug 5, 2020
1 parent 61ce7cc commit 0ff28b7
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/Nest/XPack/Transform/Pivot/GeoTileGridGroupSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Runtime.Serialization;
using Elasticsearch.Net.Utf8Json;

namespace Nest
{
/// <summary>
/// The geotile grid value source works on geo_point fields and groups points into buckets that represent
/// cells in a grid. The resulting grid can be sparse and only contains cells that have matching data.
/// <para />
/// Available in Elasticsearch 7.9.0+
/// </summary>
[InterfaceDataContract]
public interface IGeoTileGridGroupSource : ISingleGroupSource
{
/// <summary>
/// The highest-precision geotile of length 29 produces cells that cover less than 10cm by 10cm of land.
/// This precision is uniquely suited for composite aggregations as each tile does not have to be
/// generated and loaded in memory.
/// </summary>
[DataMember(Name = "precision")]
GeoTilePrecision? Precision { get; set; }

/// <summary>
/// Constrained to a specific geo bounding box, which reduces the range of tiles used.
/// These bounds are useful when only a specific part of a geographical area needs high precision tiling.
/// <para />
/// Available in Elasticsearch 7.6.0+.
/// </summary>
[DataMember(Name = "bounds")]
IBoundingBox Bounds { get; set; }
}

/// <inheritdoc cref="IGeoTileGridGroupSource" />
public class GeoTileGridGroupSource : SingleGroupSourceBase, IGeoTileGridGroupSource
{
/// <inheritdoc />
public GeoTilePrecision? Precision { get; set; }
/// <inheritdoc />
public IBoundingBox Bounds { get; set; }
}

/// <inheritdoc cref="IGeoTileGridGroupSource" />
public class GeoTileGridGroupSourceDescriptor<T>
: SingleGroupSourceDescriptorBase<GeoTileGridGroupSourceDescriptor<T>, IGeoTileGridGroupSource, T>,
IGeoTileGridGroupSource
{
GeoTilePrecision? IGeoTileGridGroupSource.Precision { get; set; }
IBoundingBox IGeoTileGridGroupSource.Bounds { get; set; }

/// <inheritdoc cref="IGeoTileGridGroupSource.Precision"/>
public GeoTileGridGroupSourceDescriptor<T> Precision(GeoTilePrecision? precision) =>
Assign(precision, (a, v) => a.Precision = v);

/// <inheritdoc cref="IGeoTileGridGroupSource.Bounds"/>
public GeoTileGridGroupSourceDescriptor<T> Bounds(Func<BoundingBoxDescriptor, IBoundingBox> selector) =>
Assign(selector, (a, v) => a.Bounds = v?.Invoke(new BoundingBoxDescriptor()));
}
}
15 changes: 15 additions & 0 deletions src/Nest/XPack/Transform/Pivot/SingleGroupSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public SingleGroupSourcesDescriptor<T> DateHistogram(string name,
Func<DateHistogramGroupSourceDescriptor<T>, IDateHistogramGroupSource> selector
) =>
Assign(new Tuple<string, IDateHistogramGroupSource>(name, selector?.Invoke(new DateHistogramGroupSourceDescriptor<T>())), (a, v) => a.Add(v.Item1, v.Item2));

/// <inheritdoc cref="IGeoTileGridGroupSource" />
public SingleGroupSourcesDescriptor<T> GeoTileGrid(string name,
Func<GeoTileGridGroupSourceDescriptor<T>, IGeoTileGridGroupSource> selector
) =>
Assign(new Tuple<string, IGeoTileGridGroupSource>(name, selector?.Invoke(new GeoTileGridGroupSourceDescriptor<T>())), (a, v) => a.Add(v.Item1, v.Item2));
}

internal class SingleGroupSourceFormatter : IJsonFormatter<ISingleGroupSource>
Expand All @@ -95,6 +101,7 @@ internal class SingleGroupSourceFormatter : IJsonFormatter<ISingleGroupSource>
{ "terms", 0 },
{ "date_histogram", 1 },
{ "histogram", 2 },
{ "geotile_grid", 3 },
};

public void Serialize(ref JsonWriter writer, ISingleGroupSource value, IJsonFormatterResolver formatterResolver)
Expand All @@ -121,6 +128,10 @@ public void Serialize(ref JsonWriter writer, ISingleGroupSource value, IJsonForm
writer.WritePropertyName("histogram");
Serialize(ref writer, histogramGroupSource, formatterResolver);
break;
case IGeoTileGridGroupSource geoTileGridGroupSource:
writer.WritePropertyName("geotile_grid");
Serialize(ref writer, geoTileGridGroupSource, formatterResolver);
break;
default:
throw new JsonParsingException($"Unknown {nameof(ISingleGroupSource)}: {value.GetType().Name}");
}
Expand Down Expand Up @@ -164,6 +175,10 @@ public ISingleGroupSource Deserialize(ref JsonReader reader, IJsonFormatterResol
groupSource = formatterResolver.GetFormatter<HistogramGroupSource>()
.Deserialize(ref reader, formatterResolver);
break;
case 3:
groupSource = formatterResolver.GetFormatter<GeoTileGridGroupSource>()
.Deserialize(ref reader, formatterResolver);
break;
}
}
else
Expand Down
22 changes: 21 additions & 1 deletion tests/Tests/XPack/Transform/TransformApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Tests.XPack.Transform
{
[SkipVersion("<7.7.0", "Introduced in 7.7.0")]
[SkipVersion("<7.9.0", "Geotile grid group by introduced in 7.9.0")]
public class TransformApiTests : CoordinatedIntegrationTestBase<WritableCluster>
{
private const string PutTransformStep = nameof(PutTransformStep);
Expand Down Expand Up @@ -61,6 +61,18 @@ public TransformApiTests(WritableCluster cluster, EndpointUsage usage) : base(ne
Field = Field<Project>(f => f.StartedOn),
CalendarInterval = DateInterval.Week
}
},
{
"geotile", new GeoTileGridGroupSource
{
Field = Field<Project>(f => f.LocationPoint),
Precision = GeoTilePrecision.Precision6,
Bounds = new BoundingBox
{
TopLeft = new GeoLocation(-90, 180),
BottomRight = new GeoLocation(90, -180)
}
}
}
}
}
Expand Down Expand Up @@ -92,6 +104,14 @@ public TransformApiTests(WritableCluster cluster, EndpointUsage usage) : base(ne
.Field(f => f.StartedOn)
.CalendarInterval(DateInterval.Week)
)
.GeoTileGrid("geotile", gtg => gtg
.Field(f => f.LocationPoint)
.Precision(GeoTilePrecision.Precision6)
.Bounds(b => b
.TopLeft(-90, 180)
.BottomRight(90, -180)
)
)
)
),
(v, c, f) => c.Transform.Put(v, f),
Expand Down

0 comments on commit 0ff28b7

Please sign in to comment.