Skip to content

Commit ca58488

Browse files
committed
added support for geo_shape query
1 parent 50e28e1 commit ca58488

File tree

6 files changed

+124
-1
lines changed

6 files changed

+124
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Text;
6+
using Nest.Resolvers;
7+
using Newtonsoft.Json;
8+
using Elasticsearch.Net;
9+
10+
namespace Nest
11+
{
12+
public class GeoShapeQueryDescriptor<T> : IQuery
13+
where T : class
14+
{
15+
internal PropertyPathMarker _Field { get; set; }
16+
bool IQuery.IsConditionless
17+
{
18+
get
19+
{
20+
return this._Field.IsConditionless() || (this._Shape == null || !this._Shape.Coordinates.HasAny());
21+
}
22+
23+
}
24+
public GeoShapeQueryDescriptor<T> OnField(string field)
25+
{
26+
this._Field = field;
27+
return this;
28+
}
29+
public GeoShapeQueryDescriptor<T> OnField(Expression<Func<T, object>> objectPath)
30+
{
31+
this._Field = objectPath;
32+
return this;
33+
}
34+
[JsonProperty("shape")]
35+
internal GeoShapeVector _Shape { get; set; }
36+
37+
public GeoShapeQueryDescriptor<T> Type(string type)
38+
{
39+
if (this._Shape == null)
40+
this._Shape = new GeoShapeVector();
41+
this._Shape.Type = type;
42+
return this;
43+
}
44+
45+
public GeoShapeQueryDescriptor<T> Coordinates(IEnumerable<IEnumerable<double>> coordinates)
46+
{
47+
if (this._Shape == null)
48+
this._Shape = new GeoShapeVector();
49+
this._Shape.Coordinates = coordinates;
50+
return this;
51+
}
52+
53+
}
54+
55+
}

src/Nest/DSL/QueryDescriptor.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ internal QueryDescriptor(bool forceConditionless)
6666
[JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))]
6767
[JsonProperty(PropertyName = "fuzzy")]
6868
internal IDictionary<PropertyPathMarker, object> FuzzyQueryDescriptor { get; set; }
69+
[JsonProperty(PropertyName = "geo_shape")]
70+
[JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))]
71+
internal IDictionary<PropertyPathMarker, object> GeoShapeQueryDescriptor { get; set; }
6972
[JsonProperty(PropertyName = "terms")]
7073
[JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))]
7174
internal IDictionary<PropertyPathMarker, object> TermsQueryDescriptor { get; set; }
@@ -433,6 +436,23 @@ public BaseQuery MoreLikeThis(Action<MoreLikeThisQueryDescriptor<T>> selector)
433436

434437
return this.New(query, q => q.MoreLikeThisDescriptor = query);
435438
}
439+
440+
/// <summary>
441+
/// The geo_shape Filter uses the same grid square representation as the geo_shape mapping to find documents
442+
/// that have a shape that intersects with the query shape.
443+
/// It will also use the same PrefixTree configuration as defined for the field mapping.
444+
/// </summary>
445+
public BaseQuery GeoShape(Action<GeoShapeQueryDescriptor<T>> selector)
446+
{
447+
var query = new GeoShapeQueryDescriptor<T>();
448+
selector(query);
449+
var shape = new Dictionary<PropertyPathMarker, object>
450+
{
451+
{ query._Field, query }
452+
};
453+
return this.New(query, q => q.GeoShapeQueryDescriptor = shape);
454+
}
455+
436456
/// <summary>
437457
/// The has_child query works the same as the has_child filter, by automatically wrapping the filter with a
438458
/// constant_score.

src/Nest/Domain/DSL/Query.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ public static BaseQuery SimpleQueryString(Action<SimpleQueryStringQueryDescripto
134134
{
135135
return new QueryDescriptor<T>().SimpleQueryString(selector);
136136
}
137-
137+
138+
public static BaseQuery GeoShape(Action<GeoShapeQueryDescriptor<T>> selector)
139+
{
140+
return new QueryDescriptor<T>().GeoShape(selector);
141+
}
138142
public static BaseQuery QueryString(Action<QueryStringDescriptor<T>> selector)
139143
{
140144
return new QueryDescriptor<T>().QueryString(selector);

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
<Compile Include="DSL\Facets\Ip4Range.cs" />
171171
<Compile Include="DSL\Facets\DateExpressionRange.cs" />
172172
<Compile Include="DSL\Paths\BasePathDescriptor.cs" />
173+
<Compile Include="DSL\Query\GeoShapeQueryDescriptor.cs" />
173174
<Compile Include="DSL\Query\SimpeQueryStringQueryDescriptor.cs" />
174175
<Compile Include="DSL\SourceDescriptor.cs" />
175176
<Compile Include="DSL\NodesStatsDescriptor.cs" />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"from": 0,
3+
"size": 10,
4+
"query": {
5+
"geo_shape": {
6+
"myGeoShape": {
7+
"shape": {
8+
"type": "enveloppe",
9+
"coordinates": [
10+
[ 13.0, 53.0 ], [ 14.0, 52.0 ]
11+
]
12+
}
13+
}
14+
}
15+
}
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Reflection;
2+
using Nest.Tests.MockData.Domain;
3+
using NUnit.Framework;
4+
5+
namespace Nest.Tests.Unit.Search.Query.Singles.GeoShape
6+
{
7+
[TestFixture]
8+
public class GeoShapeQueryJson : BaseJsonTests
9+
{
10+
[Test]
11+
public void GeoShapeFull()
12+
{
13+
var s = new SearchDescriptor<ElasticsearchProject>()
14+
.From(0)
15+
.Size(10)
16+
.Query(q=>q
17+
.GeoShape(qs=>qs
18+
.OnField(p=>p.MyGeoShape)
19+
.Coordinates(new [] { new [] {13.0, 53.0}, new [] { 14.0, 52.0} })
20+
.Type("enveloppe")
21+
)
22+
);
23+
24+
this.JsonEquals(s, MethodInfo.GetCurrentMethod());
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)