Skip to content

Commit

Permalink
Support geotile_grid aggregation in composite agg sources. (#4271)
Browse files Browse the repository at this point in the history
(cherry picked from commit c01ec36)
  • Loading branch information
codebrain authored and russcam committed Dec 17, 2019
1 parent ad414a0 commit f7ceee9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ public CompositeAggregationSourcesDescriptor<T> DateHistogram(string name,
Func<DateHistogramCompositeAggregationSourceDescriptor<T>, IDateHistogramCompositeAggregationSource> selector
) =>
Assign(selector?.Invoke(new DateHistogramCompositeAggregationSourceDescriptor<T>(name)), (a, v) => a.Add(v));

/// <inheritdoc cref="IGeoTileGridCompositeAggregationSource" />
public CompositeAggregationSourcesDescriptor<T> GeoTileGrid(string name,
Func<GeoTileGridCompositeAggregationSourceDescriptor<T>, IGeoTileGridCompositeAggregationSource> selector
) =>
Assign(selector?.Invoke(new GeoTileGridCompositeAggregationSourceDescriptor<T>(name)), (a, v) => a.Add(v));
}

/// <inheritdoc cref="ICompositeAggregationSource" />
Expand Down Expand Up @@ -154,6 +160,9 @@ public void Serialize(ref JsonWriter writer, ICompositeAggregationSource value,
case IHistogramCompositeAggregationSource histogramCompositeAggregationSource:
Serialize(ref writer, histogramCompositeAggregationSource, formatterResolver);
break;
case IGeoTileGridCompositeAggregationSource geoTileGridCompositeAggregationSource:
Serialize(ref writer, geoTileGridCompositeAggregationSource, formatterResolver);
break;
default:
Serialize(ref writer, value, formatterResolver);
break;
Expand All @@ -176,6 +185,7 @@ IJsonFormatterResolver formatterResolver
{ "terms", 0 },
{ "date_histogram", 1 },
{ "histogram", 2 },
{ "geotile_grid", 3 },
};

public ICompositeAggregationSource Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
Expand Down Expand Up @@ -208,6 +218,10 @@ public ICompositeAggregationSource Deserialize(ref JsonReader reader, IJsonForma
compositeAggregationSource = formatterResolver.GetFormatter<HistogramCompositeAggregationSource>()
.Deserialize(ref reader, formatterResolver);
break;
case 3:
compositeAggregationSource = formatterResolver.GetFormatter<GeoTileGridCompositeAggregationSource>()
.Deserialize(ref reader, formatterResolver);
break;
}
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Runtime.Serialization;

namespace Nest
{
/// <summary>
/// A values source that is equivalent to a simple Geo aggregation.
/// </summary>
public interface IGeoTileGridCompositeAggregationSource : ICompositeAggregationSource
{
/// <summary>
/// The zoom of the key used to define cells/buckets in the results.
/// </summary>
[DataMember(Name ="precision")]
GeoTilePrecision? Precision { get; set; }
}

/// <inheritdoc cref="IGeoTileGridCompositeAggregationSource" />
public class GeoTileGridCompositeAggregationSource : CompositeAggregationSourceBase, IGeoTileGridCompositeAggregationSource
{
public GeoTileGridCompositeAggregationSource(string name) : base(name) { }

/// <inheritdoc />
public GeoTilePrecision? Precision { get; set; }

/// <inheritdoc />
protected override string SourceType => "geotile_grid";
}

/// <inheritdoc cref="IGeoTileGridCompositeAggregationSource" />
public class GeoTileGridCompositeAggregationSourceDescriptor<T>
: CompositeAggregationSourceDescriptorBase<GeoTileGridCompositeAggregationSourceDescriptor<T>, IGeoTileGridCompositeAggregationSource, T>,
IGeoTileGridCompositeAggregationSource
{
public GeoTileGridCompositeAggregationSourceDescriptor(string name) : base(name, "geotile_grid") { }

GeoTilePrecision? IGeoTileGridCompositeAggregationSource.Precision { get; set; }

/// <inheritdoc cref="IGeoTileGridCompositeAggregationSource.Precision" />
public GeoTileGridCompositeAggregationSourceDescriptor<T> Precision(GeoTilePrecision? precision) =>
Assign(precision, (a, v) => a.Precision = v);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Tests.Aggregations.Bucket.Composite
*
* Be sure to read the Elasticsearch documentation on {ref_current}/search-aggregations-bucket-composite-aggregation.html[Composite Aggregation].
*/
[SkipVersion("<6.1.0", "Composite Aggregation is only available in Elasticsearch 6.1.0+")]
[SkipVersion("<7.5.0", "Geo tile grid composite agg added in 7.5.0, rest available in 6.1.0+")]
public class CompositeAggregationUsageTests : ProjectsOnlyAggregationUsageTestBase
{
public CompositeAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
Expand Down Expand Up @@ -73,6 +73,17 @@ public CompositeAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) :
}
}
},
new
{
geo = new
{
geotile_grid = new
{
field = "locationPoint",
precision = 12
}
}
},
}
},
aggs = new
Expand Down Expand Up @@ -109,6 +120,10 @@ public CompositeAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) :
.Field(f => f.RequiredBranches)
.Interval(1)
)
.GeoTileGrid("geo", h => h
.Field(f => f.LocationPoint)
.Precision(GeoTilePrecision.Precision12)
)
)
.Aggregations(childAggs => childAggs
.Nested("project_tags", n => n
Expand Down Expand Up @@ -138,6 +153,11 @@ public CompositeAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) :
{
Field = Field<Project>(f => f.RequiredBranches),
Interval = 1
},
new GeoTileGridCompositeAggregationSource("geo")
{
Field = Field<Project>(f => f.LocationPoint),
Precision = GeoTilePrecision.Precision12
}
},
Aggregations = new NestedAggregation("project_tags")
Expand All @@ -164,8 +184,8 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
composite.AfterKey.Should().NotBeNull();
if (TestConfiguration.Instance.InRange(">=6.3.0"))
composite.AfterKey.Should()
.HaveCount(3)
.And.ContainKeys("branches", "started", "branch_count");
.HaveCount(4)
.And.ContainKeys("branches", "started", "branch_count", "geo");
foreach (var item in composite.Buckets)
{
var key = item.Key;
Expand Down

0 comments on commit f7ceee9

Please sign in to comment.