Skip to content

Commit 0764518

Browse files
committed
added geo shape filter
1 parent f979ca5 commit 0764518

File tree

8 files changed

+146
-10
lines changed

8 files changed

+146
-10
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using NUnit.Framework;
2+
using Nest.Tests.MockData.Domain;
3+
4+
namespace Nest.Tests.Unit.Search.Filter.Singles
5+
{
6+
[TestFixture]
7+
public class GeoIndexedShapeFilterJson
8+
{
9+
[Test]
10+
public void GeoShapeFilter()
11+
{
12+
//[13.0, 53.0], [14.0, 52.0]]
13+
var s = new SearchDescriptor<ElasticSearchProject>()
14+
.From(0)
15+
.Size(10)
16+
.Filter(filter => filter
17+
.Cache(true)
18+
.Name("my_geo_filter")
19+
.GeoIndexedShape(f=>f.Origin, d=>d
20+
.Lookup<ElasticSearchProject>(p=>p.MyGeoShape, "1")
21+
)
22+
);
23+
24+
var json = TestElasticClient.Serialize(s);
25+
var expected = @"{ from: 0, size: 10,
26+
filter : {
27+
geo_shape: {
28+
origin: {
29+
indexed_shape: {
30+
id: ""1"",
31+
type: ""elasticsearchprojects"",
32+
index: ""mydefaultindex"",
33+
shape_field_name: ""myGeoShape""
34+
}
35+
},
36+
_cache: true,
37+
_name: ""my_geo_filter""
38+
}
39+
}
40+
}";
41+
Assert.True(json.JsonEquals(expected), json);
42+
}
43+
44+
}
45+
}

src/Nest.Tests.Unit/Search/Filter/Singles/GeoShapeFilterJson.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void GeoShapeFilter()
1414
.From(0)
1515
.Size(10)
1616
.Filter(filter => filter
17-
.Cache(true)
17+
.Cache(true)
1818
.Name("my_geo_filter")
1919
.GeoShape(f=>f.Origin, d=>d
2020
.Type("envelope")

src/Nest.Tests.Unit/TestElasticClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static class TestElasticClient
1111
public static ConnectionSettings Settings;
1212
static TestElasticClient()
1313
{
14-
Settings = new ConnectionSettings(Test.Default.Uri);
14+
Settings = new ConnectionSettings(Test.Default.Uri).SetDefaultIndex("mydefaultindex");
1515
Client = new ElasticClient(Settings);
1616
}
1717
public static string Serialize<T>(T obj)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
9+
namespace Nest
10+
{
11+
public class GeoIndexedShapeFilterDescriptor : FilterBase
12+
{
13+
internal override bool IsConditionless
14+
{
15+
get
16+
{
17+
return this._Shape == null || this._Shape.Id.IsNullOrEmpty();
18+
}
19+
20+
}
21+
22+
[JsonProperty("indexed_shape")]
23+
internal GeoIndexedShapeVector _Shape { get; set; }
24+
25+
26+
public GeoIndexedShapeFilterDescriptor Lookup<T>(string field, string id, string index = null, string type = null)
27+
{
28+
this._Shape = new GeoIndexedShapeVector
29+
{
30+
Field = field,
31+
Id = id,
32+
Type = type ?? new TypeNameMarker { Type = typeof(T) },
33+
Index = index ?? new IndexNameMarker { Type = typeof(T) }
34+
};
35+
return this;
36+
}
37+
38+
public GeoIndexedShapeFilterDescriptor Lookup<T>(Expression<Func<T, object>> field, string id, string index = null, string type = null)
39+
{
40+
var fieldName = new PropertyNameResolver().Resolve(field);
41+
return this.Lookup<T>(fieldName, id, index, type);
42+
}
43+
44+
}
45+
46+
}

src/Nest/DSL/FilterDescriptor.cs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ public class FilterDescriptor : FilterDescriptor<dynamic>
2020

2121
public class FilterDescriptor<T> : BaseFilter, IFilterDescriptor<T> where T : class
2222
{
23-
private readonly TypeNameResolver typeNameResolver;
24-
public FilterDescriptor()
25-
{
26-
this.typeNameResolver = new TypeNameResolver();
27-
}
28-
2923
internal string _Name { get; set; }
3024
internal string _CacheKey { get; set; }
3125
internal bool? _Cache { get; set; }
@@ -453,15 +447,15 @@ public BaseFilter GeoDistanceRange(string field, Action<GeoDistanceRangeFilterDe
453447
}
454448

455449
/// <summary>
456-
/// Filters documents that exists within a range from a specific point:
450+
/// Filter documents indexed using the geo_shape type.
457451
/// </summary>
458452
public BaseFilter GeoShape(Expression<Func<T, object>> fieldDescriptor, Action<GeoShapeFilterDescriptor> filterDescriptor)
459453
{
460454
var field = new PropertyNameResolver().Resolve(fieldDescriptor);
461455
return this.GeoShape(field, filterDescriptor);
462456
}
463457
/// <summary>
464-
/// Filters documents that exists within a range from a specific point:
458+
/// Filter documents indexed using the geo_shape type.
465459
/// </summary>
466460
public BaseFilter GeoShape(string field, Action<GeoShapeFilterDescriptor> filterDescriptor)
467461
{
@@ -479,6 +473,34 @@ public BaseFilter GeoShape(string field, Action<GeoShapeFilterDescriptor> filter
479473
});
480474

481475
}
476+
/// <summary>
477+
/// Filter documents indexed using the geo_shape type.
478+
/// </summary>
479+
public BaseFilter GeoIndexedShape(Expression<Func<T, object>> fieldDescriptor, Action<GeoIndexedShapeFilterDescriptor> filterDescriptor)
480+
{
481+
var field = new PropertyNameResolver().Resolve(fieldDescriptor);
482+
return this.GeoIndexedShape(field, filterDescriptor);
483+
}
484+
/// <summary>
485+
/// Filter documents indexed using the geo_shape type.
486+
/// </summary>
487+
public BaseFilter GeoIndexedShape(string field, Action<GeoIndexedShapeFilterDescriptor> filterDescriptor)
488+
{
489+
var filter = new GeoIndexedShapeFilterDescriptor();
490+
if (filterDescriptor == null)
491+
return CreateConditionlessFilterDescriptor("geo_shape", filter);
492+
493+
filterDescriptor(filter);
494+
if (filter.IsConditionless)
495+
return CreateConditionlessFilterDescriptor("geo_shape", filter);
496+
497+
return this.SetDictionary("geo_shape", field, filter, (d, b) =>
498+
{
499+
b.GeoShapeFilter = d;
500+
});
501+
502+
}
503+
482504

483505
/// <summary>
484506
/// A filter allowing to include hits that only fall within a polygon of points.

src/Nest/Domain/DSL/GeoShapeVector.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using Nest.Resolvers;
56
using Newtonsoft.Json;
67

78
namespace Nest
@@ -18,4 +19,24 @@ public class GeoShapeVector
1819
[JsonProperty("coordinates")]
1920
public IEnumerable<IEnumerable<double>> Coordinates { get; set; }
2021
}
22+
23+
24+
/// <summary>
25+
/// An object to describe a geoshape vetor
26+
/// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-filter.html
27+
/// </summary>
28+
public class GeoIndexedShapeVector
29+
{
30+
[JsonProperty("id")]
31+
public string Id { get; set; }
32+
33+
[JsonProperty("type")]
34+
public TypeNameMarker Type { get; set; }
35+
36+
[JsonProperty("index")]
37+
public IndexNameMarker Index { get; set; }
38+
39+
[JsonProperty("shape_field_name")]
40+
public string Field { get; set; }
41+
}
2142
}

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<Compile Include="Domain\Mapping\Descriptors\CompletionMappingDescriptor.cs" />
7575
<Compile Include="Domain\Mapping\Types\CompletionMapping.cs" />
7676
<Compile Include="Domain\Responses\RootVersionInfoResponse.cs" />
77+
<Compile Include="DSL\Filter\GeoIndexedShapeFilterDescriptor.cs" />
7778
<Compile Include="DSL\Filter\GeoShapeFilterDescriptor.cs" />
7879
<Compile Include="DSL\Suggest\FuzzySuggestDescriptor.cs" />
7980
<Compile Include="DSL\Filter\RegexpFilterDescriptor.cs" />

src/Nest/Resolvers/PropertyNameResolver.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public string Resolve(Expression expression)
9090
}
9191
return resolved;
9292
}
93+
9394
public string ResolveToLastToken(Expression expression)
9495
{
9596
var stack = new Stack<string>();

0 commit comments

Comments
 (0)