Skip to content

Commit 218ce47

Browse files
authored
Add support for geo_bounding_box query. (#7329)
* Add support for geo_bounding_box query Includes code generation of geo types and converters
1 parent 0cca78d commit 218ce47

File tree

57 files changed

+1797
-4
lines changed

Some content is hidden

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

57 files changed

+1797
-4
lines changed

.editorconfig

-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ resharper_redundant_case_label_highlighting=do_not_show
201201
resharper_redundant_argument_default_value_highlighting=do_not_show
202202

203203
spelling_languages=en-us,en-gb
204-
spelling_exclusion_path=.\exclusion.dic
205204

206205
[Jenkinsfile]
207206
indent_style = space

exclusion.dic

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ async
44
inferrer
55
elasticsearch
66
asciidocs
7-
yyyy
7+
yyyy
8+
enum
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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;
6+
7+
namespace Elastic.Clients.Elasticsearch.Core;
8+
9+
internal interface IComplexUnion<TEnum> where TEnum : Enum
10+
{
11+
internal TEnum ValueKind { get; }
12+
internal object Value { get; }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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;
6+
using System.Collections.Generic;
7+
using System.Text.Json;
8+
using System.Text.Json.Serialization;
9+
using Elastic.Clients.Elasticsearch.Core;
10+
11+
namespace Elastic.Clients.Elasticsearch.Serialization;
12+
13+
/// <summary>
14+
/// A base converter for any multi-item (>2 items) unions. The code-generator creates a
15+
/// derived type which populates the fields in its constructor.
16+
/// IMPORTANT: This is a MVP implementation which meets the requirements for the currently
17+
/// generated multi-item unions. Additional logic may be needed when we first encounter
18+
/// other item types. In the interests of time, we are not covering all cases for now.
19+
/// </summary>
20+
internal abstract class MultiItemUnionConverter<TUnion, TEnum> : JsonConverter<TUnion>
21+
where TUnion : IComplexUnion<TEnum>
22+
where TEnum : Enum
23+
{
24+
// Used when serializing to specify the type for each enum kind.
25+
protected Dictionary<TEnum, Type> _types;
26+
27+
// Used when creating an instance of the TUnion for a specific type.
28+
protected Dictionary<Type, Func<object, TUnion>> _factories;
29+
30+
// Used when deserializing objects, to determine which type we have.
31+
protected Dictionary<string, Type> _uniquePropertyToType;
32+
33+
protected Type _arrayType; // For now, we handle only unions with one item being defined as an array
34+
35+
public override TUnion? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
36+
{
37+
const string exceptionMessage = "Unable to match JSON object to union item.";
38+
39+
// Plan of attack!!
40+
// - If the token is start object:
41+
// - For types which are objects, we need to identify required properties
42+
// - Determine if each object has a unique required property (if not we need to find unique combinations)
43+
// - If none are unique, we may struggle, but we can also check for unique optional properties
44+
// - If the token is a literal value, see if we have a matching literal available
45+
// - If the token is the start of an array, do we have an array type identified (we currently only support one array-based item).
46+
47+
if (_factories is null)
48+
ThrowHelper.ThrowJsonException("No factories have been registered for deserialization.");
49+
50+
// We'll be handling an object
51+
if (reader.TokenType == JsonTokenType.StartObject)
52+
{
53+
var readerCopy = reader; // We need a copy to use when reading ahead
54+
55+
if (_uniquePropertyToType is null)
56+
ThrowHelper.ThrowJsonException(exceptionMessage);
57+
58+
using var jsonDoc = JsonDocument.ParseValue(ref readerCopy);
59+
60+
if (jsonDoc is null)
61+
ThrowHelper.ThrowJsonException(exceptionMessage);
62+
63+
Type? matchedType = null;
64+
65+
// Loop through the unique properties of each possible object.
66+
// Once we find a match we can stop checking any further.
67+
foreach (var item in _uniquePropertyToType)
68+
{
69+
if (jsonDoc.RootElement.TryGetProperty(item.Key, out _))
70+
{
71+
// We've matched a unique property in the JSON object, so now know the type
72+
matchedType = item.Value;
73+
break;
74+
}
75+
}
76+
77+
if (matchedType is null)
78+
ThrowHelper.ThrowJsonException(exceptionMessage);
79+
80+
if (!_factories.TryGetValue(matchedType, out var factory))
81+
ThrowHelper.ThrowJsonException("Unable to locate factory for multi-item union object item.");
82+
83+
// Since we now know the type and have the factory for that type, we can deserialize the object
84+
// and pass it to the factory to create the instance.
85+
86+
var value = JsonSerializer.Deserialize(ref reader, matchedType, options);
87+
88+
return factory.Invoke(value);
89+
}
90+
91+
if (reader.TokenType == JsonTokenType.String)
92+
{
93+
var value = reader.GetString();
94+
reader.Read();
95+
96+
if (!_factories.TryGetValue(typeof(string), out var factory))
97+
ThrowHelper.ThrowJsonException("Unable to locate factory for multi-item union accepting a string value.");
98+
99+
return factory.Invoke(value);
100+
}
101+
102+
if (reader.TokenType == JsonTokenType.StartArray)
103+
{
104+
if (_arrayType is null)
105+
ThrowHelper.ThrowJsonException(exceptionMessage);
106+
107+
if (!_factories.TryGetValue(_arrayType, out var factory))
108+
ThrowHelper.ThrowJsonException("Unable to locate factory for multi-item union accepting an array value.");
109+
110+
var value = JsonSerializer.Deserialize(ref reader, _arrayType, options);
111+
112+
return factory.Invoke(value);
113+
}
114+
115+
ThrowHelper.ThrowJsonException($"Unable to deserialize JSON representing {typeof(TUnion)}.");
116+
117+
return default; // We never reach here!
118+
}
119+
120+
public override void Write(Utf8JsonWriter writer, TUnion value, JsonSerializerOptions options)
121+
{
122+
if (_types is null)
123+
ThrowHelper.ThrowJsonException("No types have been registered for serialization.");
124+
125+
if (value is null)
126+
{
127+
writer.WriteNullValue();
128+
return;
129+
}
130+
131+
var serializeAsType = _types[value.ValueKind];
132+
133+
JsonSerializer.Serialize(writer, value.Value, serializeAsType, options);
134+
}
135+
}

src/Elastic.Clients.Elasticsearch/_Generated/Types/AggregateOrderConverter.g.cs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Elastic.Transport;
2121
using System;
2222
using System.Collections.Generic;
23+
using System.Diagnostics.CodeAnalysis;
2324
using System.Linq.Expressions;
2425
using System.Text.Json;
2526
using System.Text.Json.Serialization;

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/AggregateDictionary.g.cs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Elastic.Transport;
2121
using System;
2222
using System.Collections.Generic;
23+
using System.Diagnostics.CodeAnalysis;
2324
using System.Linq.Expressions;
2425
using System.Text.Json;
2526
using System.Text.Json.Serialization;

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/Buckets.g.cs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Elastic.Transport;
2121
using System;
2222
using System.Collections.Generic;
23+
using System.Diagnostics.CodeAnalysis;
2324
using System.Linq.Expressions;
2425
using System.Text.Json;
2526
using System.Text.Json.Serialization;

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/FieldDateMath.g.cs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Elastic.Transport;
2121
using System;
2222
using System.Collections.Generic;
23+
using System.Diagnostics.CodeAnalysis;
2324
using System.Linq.Expressions;
2425
using System.Text.Json;
2526
using System.Text.Json.Serialization;

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/Percentiles.g.cs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Elastic.Transport;
2121
using System;
2222
using System.Collections.Generic;
23+
using System.Diagnostics.CodeAnalysis;
2324
using System.Linq.Expressions;
2425
using System.Text.Json;
2526
using System.Text.Json.Serialization;

src/Elastic.Clients.Elasticsearch/_Generated/Types/ByteSize.g.cs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Elastic.Transport;
2121
using System;
2222
using System.Collections.Generic;
23+
using System.Diagnostics.CodeAnalysis;
2324
using System.Linq.Expressions;
2425
using System.Text.Json;
2526
using System.Text.Json.Serialization;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
// ███╗ ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
6+
// ████╗ ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
7+
// ██╔██╗ ██║██║ ██║ ██║ ██║██║ █████╗
8+
// ██║╚██╗██║██║ ██║ ██║ ██║██║ ██╔══╝
9+
// ██║ ╚████║╚██████╔╝ ██║ ██║╚██████╗███████╗
10+
// ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝
11+
// ------------------------------------------------
12+
//
13+
// This file is automatically generated.
14+
// Please do not edit these files manually.
15+
//
16+
// ------------------------------------------------
17+
18+
using Elastic.Clients.Elasticsearch.Fluent;
19+
using Elastic.Clients.Elasticsearch.Serialization;
20+
using System;
21+
using System.Collections.Generic;
22+
using System.Linq.Expressions;
23+
using System.Text.Json;
24+
using System.Text.Json.Serialization;
25+
26+
#nullable restore
27+
namespace Elastic.Clients.Elasticsearch;
28+
public sealed partial class CoordsGeoBounds
29+
{
30+
[JsonInclude, JsonPropertyName("bottom")]
31+
public double Bottom { get; set; }
32+
33+
[JsonInclude, JsonPropertyName("left")]
34+
public double Left { get; set; }
35+
36+
[JsonInclude, JsonPropertyName("right")]
37+
public double Right { get; set; }
38+
39+
[JsonInclude, JsonPropertyName("top")]
40+
public double Top { get; set; }
41+
}
42+
43+
public sealed partial class CoordsGeoBoundsDescriptor : SerializableDescriptor<CoordsGeoBoundsDescriptor>
44+
{
45+
internal CoordsGeoBoundsDescriptor(Action<CoordsGeoBoundsDescriptor> configure) => configure.Invoke(this);
46+
public CoordsGeoBoundsDescriptor() : base()
47+
{
48+
}
49+
50+
private double BottomValue { get; set; }
51+
52+
private double LeftValue { get; set; }
53+
54+
private double RightValue { get; set; }
55+
56+
private double TopValue { get; set; }
57+
58+
public CoordsGeoBoundsDescriptor Bottom(double bottom)
59+
{
60+
BottomValue = bottom;
61+
return Self;
62+
}
63+
64+
public CoordsGeoBoundsDescriptor Left(double left)
65+
{
66+
LeftValue = left;
67+
return Self;
68+
}
69+
70+
public CoordsGeoBoundsDescriptor Right(double right)
71+
{
72+
RightValue = right;
73+
return Self;
74+
}
75+
76+
public CoordsGeoBoundsDescriptor Top(double top)
77+
{
78+
TopValue = top;
79+
return Self;
80+
}
81+
82+
protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
83+
{
84+
writer.WriteStartObject();
85+
writer.WritePropertyName("bottom");
86+
writer.WriteNumberValue(BottomValue);
87+
writer.WritePropertyName("left");
88+
writer.WriteNumberValue(LeftValue);
89+
writer.WritePropertyName("right");
90+
writer.WriteNumberValue(RightValue);
91+
writer.WritePropertyName("top");
92+
writer.WriteNumberValue(TopValue);
93+
writer.WriteEndObject();
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
// ███╗ ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
6+
// ████╗ ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
7+
// ██╔██╗ ██║██║ ██║ ██║ ██║██║ █████╗
8+
// ██║╚██╗██║██║ ██║ ██║ ██║██║ ██╔══╝
9+
// ██║ ╚████║╚██████╔╝ ██║ ██║╚██████╗███████╗
10+
// ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝
11+
// ------------------------------------------------
12+
//
13+
// This file is automatically generated.
14+
// Please do not edit these files manually.
15+
//
16+
// ------------------------------------------------
17+
18+
using Elastic.Clients.Elasticsearch.Fluent;
19+
using Elastic.Clients.Elasticsearch.Serialization;
20+
using Elastic.Transport;
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Diagnostics.CodeAnalysis;
24+
using System.Linq.Expressions;
25+
using System.Text.Json;
26+
using System.Text.Json.Serialization;
27+
28+
#nullable restore
29+
namespace Elastic.Clients.Elasticsearch.Core.Search;
30+
public sealed partial class Context : Union<string, Elastic.Clients.Elasticsearch.GeoLocation>
31+
{
32+
public Context(string category) : base(category)
33+
{
34+
}
35+
36+
public Context(Elastic.Clients.Elasticsearch.GeoLocation location) : base(location)
37+
{
38+
}
39+
}

src/Elastic.Clients.Elasticsearch/_Generated/Types/Core/MultiGetResponseItem.g.cs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Elastic.Transport;
2121
using System;
2222
using System.Collections.Generic;
23+
using System.Diagnostics.CodeAnalysis;
2324
using System.Linq.Expressions;
2425
using System.Text.Json;
2526
using System.Text.Json.Serialization;

src/Elastic.Clients.Elasticsearch/_Generated/Types/Core/MultiSearchResponseItem.g.cs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Elastic.Transport;
2121
using System;
2222
using System.Collections.Generic;
23+
using System.Diagnostics.CodeAnalysis;
2324
using System.Linq.Expressions;
2425
using System.Text.Json;
2526
using System.Text.Json.Serialization;

0 commit comments

Comments
 (0)