Skip to content

Commit 03d5ac4

Browse files
authored
Merge pull request #2187 from elastic/fix/2.x-georelation
Add relation to geo_shape queries
2 parents b2ad5dd + 3136443 commit 03d5ac4

22 files changed

+95
-45
lines changed

src/Nest/CommonAbstractions/Extensions/Extensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ internal static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Fu
5858
internal static ConcurrentDictionary<string, object> _enumCache = new ConcurrentDictionary<string, object>();
5959
internal static T? ToEnum<T>(this string str, StringComparison comparison = StringComparison.OrdinalIgnoreCase) where T : struct
6060
{
61+
if (str == null) return null;
62+
6163
var enumType = typeof(T);
6264
var key = $"{enumType.Name}.{str}";
6365
object value;

src/Nest/CommonOptions/Geo/GeoShapeRelation.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public enum GeoShapeRelation
1212
[EnumMember(Value = "disjoint")]
1313
Disjoint,
1414
[EnumMember(Value = "within")]
15-
Within
15+
Within,
16+
[EnumMember(Value = "contains")]
17+
Contains
1618
}
1719
}

src/Nest/QueryDsl/Geo/Shape/Circle/GeoShapeCircleQuery.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ public interface IGeoShapeCircleQuery : IGeoShapeQuery
99
ICircleGeoShape Shape { get; set; }
1010
}
1111

12-
public class GeoShapeCircleQuery : FieldNameQueryBase, IGeoShapeCircleQuery
12+
public class GeoShapeCircleQuery : GeoShapeQueryBase, IGeoShapeCircleQuery
1313
{
1414
protected override bool Conditionless => IsConditionless(this);
1515
public ICircleGeoShape Shape { get; set; }
1616

1717
internal override void InternalWrapInContainer(IQueryContainer c) => c.GeoShape = this;
18+
1819
internal static bool IsConditionless(IGeoShapeCircleQuery q) => q.Field.IsConditionless() || q.Shape == null || q.Shape.Coordinates == null;
1920
}
2021

21-
public class GeoShapeCircleQueryDescriptor<T>
22-
: FieldNameQueryDescriptorBase<GeoShapeCircleQueryDescriptor<T>, IGeoShapeCircleQuery, T>
22+
public class GeoShapeCircleQueryDescriptor<T>
23+
: GeoShapeQueryDescriptorBase<GeoShapeCircleQueryDescriptor<T>, IGeoShapeCircleQuery, T>
2324
, IGeoShapeCircleQuery where T : class
2425
{
2526
protected override bool Conditionless => GeoShapeCircleQuery.IsConditionless(this);

src/Nest/QueryDsl/Geo/Shape/Envelope/GeoShapeEnvelopeQuery.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public interface IGeoShapeEnvelopeQuery : IGeoShapeQuery
1010
IEnvelopeGeoShape Shape { get; set; }
1111
}
1212

13-
public class GeoShapeEnvelopeQuery : FieldNameQueryBase, IGeoShapeEnvelopeQuery
13+
public class GeoShapeEnvelopeQuery : GeoShapeQueryBase, IGeoShapeEnvelopeQuery
1414
{
1515
protected override bool Conditionless => IsConditionless(this);
1616
public IEnvelopeGeoShape Shape { get; set; }
@@ -19,8 +19,8 @@ public class GeoShapeEnvelopeQuery : FieldNameQueryBase, IGeoShapeEnvelopeQuery
1919
internal static bool IsConditionless(IGeoShapeEnvelopeQuery q) => q.Field.IsConditionless() || q.Shape == null || !q.Shape.Coordinates.HasAny();
2020
}
2121

22-
public class GeoShapeEnvelopeQueryDescriptor<T>
23-
: FieldNameQueryDescriptorBase<GeoShapeEnvelopeQueryDescriptor<T>, IGeoShapeEnvelopeQuery, T>
22+
public class GeoShapeEnvelopeQueryDescriptor<T>
23+
: GeoShapeQueryDescriptorBase<GeoShapeEnvelopeQueryDescriptor<T>, IGeoShapeEnvelopeQuery, T>
2424
, IGeoShapeEnvelopeQuery where T : class
2525
{
2626
protected override bool Conditionless => GeoShapeEnvelopeQuery.IsConditionless(this);

src/Nest/QueryDsl/Geo/Shape/GeoShapeQueryJsonConverter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
3737
JToken shape;
3838
JToken indexedShape;
3939
IGeoShapeQuery query = null;
40+
4041
if (jo.TryGetValue("shape", out shape))
4142
query = ParseShape(shape, serializer);
4243
else if (jo.TryGetValue("indexed_shape", out indexedShape))
@@ -45,9 +46,11 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
4546
if (query == null) return null;
4647
var boost = jo["boost"]?.Value<double>();
4748
var name = jo["_name"]?.Value<string>();
49+
var relation = jo["relation"]?.Value<string>().ToEnum<GeoShapeRelation>();
4850
query.Boost = boost;
4951
query.Name = name;
5052
query.Field = field;
53+
query.Relation = relation;
5154
return query;
5255
}
5356

src/Nest/QueryDsl/Geo/Shape/IGeoShapeQuery.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,26 @@
33
namespace Nest
44
{
55
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
6-
[JsonConverter(typeof (CompositeJsonConverter<GeoShapeQueryJsonConverter, FieldNameQueryJsonConverter<GeoShapeCircleQuery>>))]
6+
[JsonConverter(typeof(CompositeJsonConverter<GeoShapeQueryJsonConverter, FieldNameQueryJsonConverter<GeoShapeCircleQuery>>))]
77
public interface IGeoShapeQuery : IFieldNameQuery
88
{
9+
[JsonProperty("relation")]
10+
GeoShapeRelation? Relation { get; set; }
911
}
10-
}
12+
13+
public abstract class GeoShapeQueryBase : FieldNameQueryBase, IGeoShapeQuery
14+
{
15+
public GeoShapeRelation? Relation { get; set; }
16+
}
17+
18+
public abstract class GeoShapeQueryDescriptorBase<TDescriptor, TInterface, T>
19+
: FieldNameQueryDescriptorBase<TDescriptor, TInterface, T>, IGeoShapeQuery
20+
where TDescriptor : FieldNameQueryDescriptorBase<TDescriptor, TInterface, T>, TInterface
21+
where TInterface : class, IGeoShapeQuery
22+
where T : class
23+
{
24+
GeoShapeRelation? IGeoShapeQuery.Relation { get; set; }
25+
26+
public TDescriptor Relation(GeoShapeRelation relation) => Assign(a => a.Relation = relation);
27+
}
28+
}

src/Nest/QueryDsl/Geo/Shape/IndexedShape/GeoIndexedShapeQuery.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ public interface IGeoIndexedShapeQuery : IGeoShapeQuery
1010
IFieldLookup IndexedShape { get; set; }
1111
}
1212

13-
public class GeoIndexedShapeQuery : FieldNameQueryBase, IGeoIndexedShapeQuery
13+
public class GeoIndexedShapeQuery : GeoShapeQueryBase, IGeoIndexedShapeQuery
1414
{
1515
protected override bool Conditionless => IsConditionless(this);
1616
public IFieldLookup IndexedShape { get; set; }
1717

1818
internal override void InternalWrapInContainer(IQueryContainer c) => c.GeoShape = this;
1919

20-
internal static bool IsConditionless(IGeoIndexedShapeQuery q) =>
20+
internal static bool IsConditionless(IGeoIndexedShapeQuery q) =>
2121
q.Field.IsConditionless() || q.IndexedShape == null
2222
|| q.IndexedShape.Id == null | q.IndexedShape.Index == null || q.IndexedShape.Type == null
2323
|| q.IndexedShape.Path == null;
2424
}
2525

26-
public class GeoIndexedShapeQueryDescriptor<T> : FieldNameQueryDescriptorBase<GeoIndexedShapeQueryDescriptor<T>, IGeoIndexedShapeQuery, T>
26+
public class GeoIndexedShapeQueryDescriptor<T> : GeoShapeQueryDescriptorBase<GeoIndexedShapeQueryDescriptor<T>, IGeoIndexedShapeQuery, T>
2727
, IGeoIndexedShapeQuery where T : class
2828
{
2929
protected override bool Conditionless => GeoIndexedShapeQuery.IsConditionless(this);

src/Nest/QueryDsl/Geo/Shape/LineString/GeoShapeLineStringQuery.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public interface IGeoShapeLineStringQuery : IGeoShapeQuery
1010
ILineStringGeoShape Shape { get; set; }
1111
}
1212

13-
public class GeoShapeLineStringQuery : FieldNameQueryBase, IGeoShapeLineStringQuery
13+
public class GeoShapeLineStringQuery : GeoShapeQueryBase, IGeoShapeLineStringQuery
1414
{
1515
protected override bool Conditionless => IsConditionless(this);
1616
public ILineStringGeoShape Shape { get; set; }
@@ -19,8 +19,8 @@ public class GeoShapeLineStringQuery : FieldNameQueryBase, IGeoShapeLineStringQu
1919
internal static bool IsConditionless(IGeoShapeLineStringQuery q) => q.Field.IsConditionless() || q.Shape == null || !q.Shape.Coordinates.HasAny();
2020
}
2121

22-
public class GeoShapeLineStringQueryDescriptor<T>
23-
: FieldNameQueryDescriptorBase<GeoShapeLineStringQueryDescriptor<T>, IGeoShapeLineStringQuery, T>
22+
public class GeoShapeLineStringQueryDescriptor<T>
23+
: GeoShapeQueryDescriptorBase<GeoShapeLineStringQueryDescriptor<T>, IGeoShapeLineStringQuery, T>
2424
, IGeoShapeLineStringQuery where T : class
2525
{
2626
protected override bool Conditionless => GeoShapeLineStringQuery.IsConditionless(this);

src/Nest/QueryDsl/Geo/Shape/MultiLineString/GeoShapeMultiLineStringQuery.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public interface IGeoShapeMultiLineStringQuery : IGeoShapeQuery
1010
IMultiLineStringGeoShape Shape { get; set; }
1111
}
1212

13-
public class GeoShapeMultiLineStringQuery : FieldNameQueryBase, IGeoShapeMultiLineStringQuery
13+
public class GeoShapeMultiLineStringQuery : GeoShapeQueryBase, IGeoShapeMultiLineStringQuery
1414
{
1515
protected override bool Conditionless => IsConditionless(this);
1616
public IMultiLineStringGeoShape Shape { get; set; }
@@ -19,8 +19,8 @@ public class GeoShapeMultiLineStringQuery : FieldNameQueryBase, IGeoShapeMultiLi
1919
internal static bool IsConditionless(IGeoShapeMultiLineStringQuery q) => q.Field.IsConditionless() || q.Shape == null || !q.Shape.Coordinates.HasAny();
2020
}
2121

22-
public class GeoShapeMultiLineStringQueryDescriptor<T>
23-
: FieldNameQueryDescriptorBase<GeoShapeMultiLineStringQueryDescriptor<T>, IGeoShapeMultiLineStringQuery, T>
22+
public class GeoShapeMultiLineStringQueryDescriptor<T>
23+
: GeoShapeQueryDescriptorBase<GeoShapeMultiLineStringQueryDescriptor<T>, IGeoShapeMultiLineStringQuery, T>
2424
, IGeoShapeMultiLineStringQuery where T : class
2525
{
2626
protected override bool Conditionless => GeoShapeMultiLineStringQuery.IsConditionless(this);

src/Nest/QueryDsl/Geo/Shape/MultiPoint/GeoShapeMultiPointQuery.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ public interface IGeoShapeMultiPointQuery : IGeoShapeQuery
1010
IMultiPointGeoShape Shape { get; set; }
1111
}
1212

13-
public class GeoShapeMultiPointQuery : FieldNameQueryBase, IGeoShapeMultiPointQuery
13+
public class GeoShapeMultiPointQuery : GeoShapeQueryBase, IGeoShapeMultiPointQuery
1414
{
1515
protected override bool Conditionless => IsConditionless(this);
1616
public IMultiPointGeoShape Shape { get; set; }
1717

1818
internal override void InternalWrapInContainer(IQueryContainer c) => c.GeoShape = this;
1919
internal static bool IsConditionless(IGeoShapeMultiPointQuery q) => q.Field.IsConditionless() || q.Shape == null || !q.Shape.Coordinates.HasAny();
2020
}
21-
22-
public class GeoShapeMultiPointQueryDescriptor<T>
23-
: FieldNameQueryDescriptorBase<GeoShapeMultiPointQueryDescriptor<T>, IGeoShapeMultiPointQuery, T>
21+
22+
public class GeoShapeMultiPointQueryDescriptor<T>
23+
: GeoShapeQueryDescriptorBase<GeoShapeMultiPointQueryDescriptor<T>, IGeoShapeMultiPointQuery, T>
2424
, IGeoShapeMultiPointQuery where T : class
2525
{
2626
protected override bool Conditionless => GeoShapeMultiPointQuery.IsConditionless(this);

0 commit comments

Comments
 (0)