Skip to content

Commit 982fc92

Browse files
committed
Better GeoJSON object support for geo shape queries/filters
1 parent 72057a8 commit 982fc92

35 files changed

+662
-77
lines changed

src/Nest/DSL/Filter/GeoShapeFilterDescriptor.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public interface IGeoShapeBaseFilter : IFieldNameFilter
1818
public interface IGeoShapeFilter : IGeoShapeBaseFilter
1919
{
2020
[JsonProperty("shape")]
21-
GeoShapeVector Shape { get; set; }
21+
GeoShape Shape { get; set; }
2222
}
2323

2424
public class GeoShapeFilter : PlainFilter, IGeoShapeFilter
@@ -30,38 +30,29 @@ protected internal override void WrapInContainer(IFilterContainer container)
3030

3131
public PropertyPathMarker Field { get; set; }
3232

33-
public GeoShapeVector Shape { get; set; }
33+
public GeoShape Shape { get; set; }
3434
}
3535

3636
public class GeoShapeFilterDescriptor : FilterBase, IGeoShapeFilter
3737
{
38+
IGeoShapeFilter Self { get { return this; } }
39+
3840
bool IFilter.IsConditionless
3941
{
4042
get
4143
{
42-
return ((IGeoShapeFilter)this).Shape == null || !((IGeoShapeFilter)this).Shape.Coordinates.HasAny();
44+
return this.Self.Shape == null;
4345
}
4446
}
4547

4648
PropertyPathMarker IFieldNameFilter.Field { get; set; }
47-
GeoShapeVector IGeoShapeFilter.Shape { get; set; }
48-
49-
public GeoShapeFilterDescriptor Type(string type)
50-
{
51-
if (((IGeoShapeFilter)this).Shape == null)
52-
((IGeoShapeFilter)this).Shape = new GeoShapeVector();
53-
((IGeoShapeFilter)this).Shape.Type = type;
54-
return this;
55-
}
49+
GeoShape IGeoShapeFilter.Shape { get; set; }
5650

57-
public GeoShapeFilterDescriptor Coordinates(IEnumerable<IEnumerable<double>> coordinates)
51+
public GeoShapeFilterDescriptor Shape<TCoordinates>(IGeometryObject<TCoordinates> shape)
5852
{
59-
if (((IGeoShapeFilter)this).Shape == null)
60-
((IGeoShapeFilter)this).Shape = new GeoShapeVector();
61-
((IGeoShapeFilter)this).Shape.Coordinates = coordinates;
53+
this.Self.Shape = shape.ToGeoShape();
6254
return this;
6355
}
64-
6556
}
6657

6758
}

src/Nest/DSL/Query/GeoShapeQueryDescriptor.cs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public interface IGeoShapeQuery : IFieldNameQuery
1616
PropertyPathMarker Field { get; set; }
1717

1818
[JsonProperty("shape")]
19-
GeoShapeVector Shape { get; set; }
19+
GeoShape Shape { get; set; }
2020
}
2121

2222
public class GeoShapeQuery : PlainQuery, IGeoShapeQuery
@@ -39,60 +39,51 @@ void IFieldNameQuery.SetFieldName(string fieldName)
3939
}
4040

4141
public PropertyPathMarker Field { get; set; }
42-
public GeoShapeVector Shape { get; set; }
42+
43+
public GeoShape Shape { get; set; }
4344
}
4445

4546
public class GeoShapeQueryDescriptor<T> : IGeoShapeQuery where T : class
4647
{
48+
IGeoShapeQuery Self { get { return this; } }
49+
4750
PropertyPathMarker IGeoShapeQuery.Field { get; set; }
4851

49-
GeoShapeVector IGeoShapeQuery.Shape { get; set; }
50-
52+
GeoShape IGeoShapeQuery.Shape { get; set; }
53+
5154
bool IQuery.IsConditionless
5255
{
5356
get
5457
{
55-
return ((IGeoShapeQuery)this).Field.IsConditionless() || (((IGeoShapeQuery)this).Shape == null || !((IGeoShapeQuery)this).Shape.Coordinates.HasAny());
58+
return this.Self.Field.IsConditionless() || this.Self.Shape == null;
5659
}
5760

5861
}
5962
void IFieldNameQuery.SetFieldName(string fieldName)
6063
{
61-
((IGeoShapeQuery)this).Field = fieldName;
64+
this.Self.Field = fieldName;
6265
}
6366
PropertyPathMarker IFieldNameQuery.GetFieldName()
6467
{
65-
return ((IGeoShapeQuery)this).Field;
68+
return this.Self.Field;
6669
}
6770

6871
public GeoShapeQueryDescriptor<T> OnField(string field)
6972
{
70-
((IGeoShapeQuery)this).Field = field;
73+
this.Self.Field = field;
7174
return this;
7275
}
7376
public GeoShapeQueryDescriptor<T> OnField(Expression<Func<T, object>> objectPath)
7477
{
75-
((IGeoShapeQuery)this).Field = objectPath;
78+
this.Self.Field = objectPath;
7679
return this;
7780
}
78-
7981

80-
public GeoShapeQueryDescriptor<T> Type(string type)
82+
public GeoShapeQueryDescriptor<T> Shape<TCoordinates>(IGeometryObject<TCoordinates> shape)
8183
{
82-
if (((IGeoShapeQuery)this).Shape == null)
83-
((IGeoShapeQuery)this).Shape = new GeoShapeVector();
84-
((IGeoShapeQuery)this).Shape.Type = type;
84+
shape.ThrowIfNull("shape");
85+
this.Self.Shape = shape.ToGeoShape();
8586
return this;
8687
}
87-
88-
public GeoShapeQueryDescriptor<T> Coordinates(IEnumerable<IEnumerable<double>> coordinates)
89-
{
90-
if (((IGeoShapeQuery)this).Shape == null)
91-
((IGeoShapeQuery)this).Shape = new GeoShapeVector();
92-
((IGeoShapeQuery)this).Shape.Coordinates = coordinates;
93-
return this;
94-
}
95-
9688
}
97-
9889
}

src/Nest/Domain/DSL/GeoShapeVector.cs renamed to src/Nest/Domain/DSL/GeoShape.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
namespace Nest
88
{
99
/// <summary>
10-
/// An object to describe a geoshape vetor
10+
/// An object to describe a geoshape
1111
/// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-filter.html
1212
/// </summary>
13-
public class GeoShapeVector
13+
public class GeoShape
1414
{
1515
[JsonProperty("type")]
1616
public string Type { get; set; }
1717

1818
[JsonProperty("coordinates")]
19-
public IEnumerable<IEnumerable<double>> Coordinates { get; set; }
19+
public object Coordinates { get; set; }
20+
21+
[JsonProperty("radius")]
22+
public string Radius { get; set; }
2023
}
2124
}

src/Nest/Domain/Geometry/Circle.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest
7+
{
8+
public class Circle
9+
: GeometryObject<IEnumerable<double>>
10+
{
11+
public Circle() : this(null) { }
12+
13+
public Circle(IEnumerable<double> coordinates)
14+
: base("circle")
15+
{
16+
this.Coordinates = coordinates ?? new List<double>();
17+
}
18+
19+
public string Radius { get; set; }
20+
21+
public override GeoShape ToGeoShape()
22+
{
23+
var shape = base.ToGeoShape();
24+
shape.Radius = this.Radius;
25+
return shape;
26+
}
27+
}
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest
7+
{
8+
public class Envelope
9+
: GeometryObject<IEnumerable<IEnumerable<double>>>
10+
{
11+
public Envelope() : this(null) { }
12+
13+
public Envelope(IEnumerable<IEnumerable<double>> coordinates)
14+
: base("envelope")
15+
{
16+
this.Coordinates = coordinates ?? new List<List<double>>();
17+
}
18+
}
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest
7+
{
8+
public abstract class GeometryObject<TCoordinates>
9+
: IGeometryObject<TCoordinates>
10+
{
11+
public GeometryObject(string type)
12+
{
13+
this.Type = type;
14+
}
15+
16+
public string Type { get; protected set; }
17+
18+
public TCoordinates Coordinates { get; set; }
19+
20+
public virtual GeoShape ToGeoShape()
21+
{
22+
var shape = new GeoShape
23+
{
24+
Type = this.Type,
25+
Coordinates = this.Coordinates
26+
};
27+
28+
return shape;
29+
}
30+
}
31+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest
7+
{
8+
public interface IGeometryObject<TCoordinates>
9+
{
10+
string Type { get; }
11+
TCoordinates Coordinates { get; set; }
12+
GeoShape ToGeoShape();
13+
}
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest
7+
{
8+
public class LineString
9+
: GeometryObject<IEnumerable<IEnumerable<double>>>
10+
{
11+
public LineString() : this(null) { }
12+
13+
public LineString(IEnumerable<IEnumerable<double>> coordinates)
14+
: base("linestring")
15+
{
16+
this.Coordinates = coordinates ?? new List<List<double>>();
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest
7+
{
8+
public class MultiLineString
9+
: GeometryObject<IEnumerable<IEnumerable<IEnumerable<double>>>>
10+
{
11+
public MultiLineString() : this(null) { }
12+
13+
public MultiLineString(IEnumerable<IEnumerable<IEnumerable<double>>> coordinates)
14+
: base("multilinestring")
15+
{
16+
this.Coordinates = coordinates ?? new List<List<List<double>>>();
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest
7+
{
8+
public class MultiPoint :
9+
GeometryObject<IEnumerable<IEnumerable<double>>>
10+
{
11+
public MultiPoint() : this(null) { }
12+
13+
public MultiPoint(IEnumerable<IEnumerable<double>> coordinates)
14+
: base("multipoint")
15+
{
16+
this.Coordinates = coordinates ?? new List<List<double>>();
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)