Skip to content

Commit f979ca5

Browse files
committed
added geoshape filter
1 parent 6a75540 commit f979ca5

File tree

8 files changed

+156
-11
lines changed

8 files changed

+156
-11
lines changed

src/Nest.Tests.Unit/Nest.Tests.Unit.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<Compile Include="Internals\Inferno\HostNameWithPathTests.cs" />
108108
<Compile Include="Internals\Inferno\MapTypeNamesTests.cs" />
109109
<Compile Include="Internals\Serialize\OptOutTests.cs" />
110+
<Compile Include="Search\Filter\Singles\GeoShapeFilterJson.cs" />
110111
<Compile Include="Search\Filter\Singles\RegexpFilterJson.cs" />
111112
<Compile Include="Search\Query\BoolQueryMerges\BoolQueryMergesTests.cs" />
112113
<Compile Include="Search\Filter\BoolFilterMerges\BoolFilterMergesTests.cs" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void GeoPolygonFilter()
1717
.Filter(filter => filter
1818
.GeoPolygon("origin", "drn5x1g8cu2y", "30, -80", "20, -90")
1919
);
20-
20+
2121
var json = TestElasticClient.Serialize(s);
2222
var expected = @"{ from: 0, size: 10,
2323
filter : {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 GeoShapeFilterJson
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+
.GeoShape(f=>f.Origin, d=>d
20+
.Type("envelope")
21+
.Coordinates(new[] { new[] { 13.0, 53.0 }, new[] { 14.0, 52.0 } })
22+
)
23+
);
24+
25+
var json = TestElasticClient.Serialize(s);
26+
var expected = @"{ from: 0, size: 10,
27+
filter : {
28+
geo_shape: {
29+
origin: {
30+
shape: {
31+
type: ""envelope"",
32+
coordinates: [[13.0, 53.0], [14.0, 52.0]]
33+
}
34+
},
35+
_cache: true,
36+
_name: ""my_geo_filter""
37+
}
38+
}
39+
}";
40+
Assert.True(json.JsonEquals(expected), json);
41+
}
42+
43+
}
44+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Newtonsoft.Json;
6+
7+
namespace Nest
8+
{
9+
public class GeoShapeFilterDescriptor : FilterBase
10+
{
11+
internal override bool IsConditionless
12+
{
13+
get
14+
{
15+
return this._Shape == null || !this._Shape.Coordinates.HasAny();
16+
}
17+
18+
}
19+
20+
[JsonProperty("shape")]
21+
internal GeoShapeVector _Shape { get; set; }
22+
23+
24+
public GeoShapeFilterDescriptor Type(string type)
25+
{
26+
if (this._Shape == null)
27+
this._Shape = new GeoShapeVector();
28+
this._Shape.Type = type;
29+
return this;
30+
}
31+
32+
public GeoShapeFilterDescriptor Coordinates(IEnumerable<IEnumerable<double>> coordinates)
33+
{
34+
if (this._Shape == null)
35+
this._Shape = new GeoShapeVector();
36+
this._Shape.Coordinates = coordinates;
37+
return this;
38+
}
39+
40+
}
41+
42+
}

src/Nest/DSL/FilterDescriptor.cs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public FilterDescriptor<T> Cache(bool cache)
7171
[JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))]
7272
internal Dictionary<string, object> GeoPolygonFilter { get; set; }
7373

74+
[JsonProperty(PropertyName = "geo_shape")]
75+
[JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))]
76+
internal Dictionary<string, object> GeoShapeFilter { get; set; }
77+
7478
[JsonProperty(PropertyName = "limit")]
7579
internal LimitFilter LimitFilter { get; set; }
7680

@@ -409,6 +413,7 @@ public BaseFilter GeoDistance(string field, Action<GeoDistanceFilterDescriptor>
409413
});
410414

411415
}
416+
412417
/// <summary>
413418
/// Filters documents that exists within a range from a specific point:
414419
/// </summary>
@@ -424,17 +429,11 @@ public BaseFilter GeoDistanceRange(string field, Action<GeoDistanceRangeFilterDe
424429
{
425430
var filter = new GeoDistanceRangeFilterDescriptor();
426431
if (filterDescriptor == null)
427-
return CreateConditionlessFilterDescriptor("geo_distance", filter);
432+
return CreateConditionlessFilterDescriptor("geo_distance_range", filter);
428433

429434
filterDescriptor(filter);
430-
if (this.IsStrict && filter._FromDistance == null)
431-
throw new DslException("Missing from distance, Distance should be set when using the geo distance range DSL in strict mode");
432-
433-
if (this.IsStrict && filter._ToDistance == null)
434-
throw new DslException("Missing to distance, Distance should be set when using the geo distance range DSL in strict mode");
435-
436435
if (filter.IsConditionless)
437-
return CreateConditionlessFilterDescriptor("geo_distance", filter);
436+
return CreateConditionlessFilterDescriptor("geo_distance_range", filter);
438437

439438
return this.SetDictionary("geo_distance_range", field, filter._Location, (d, b) =>
440439
{
@@ -451,8 +450,36 @@ public BaseFilter GeoDistanceRange(string field, Action<GeoDistanceRangeFilterDe
451450
d.ForEachWithIndex((kv, i) => dd.Add(kv.Key, kv.Value));
452451
b.GeoDistanceRangeFilter = dd;
453452
});
453+
}
454+
455+
/// <summary>
456+
/// Filters documents that exists within a range from a specific point:
457+
/// </summary>
458+
public BaseFilter GeoShape(Expression<Func<T, object>> fieldDescriptor, Action<GeoShapeFilterDescriptor> filterDescriptor)
459+
{
460+
var field = new PropertyNameResolver().Resolve(fieldDescriptor);
461+
return this.GeoShape(field, filterDescriptor);
462+
}
463+
/// <summary>
464+
/// Filters documents that exists within a range from a specific point:
465+
/// </summary>
466+
public BaseFilter GeoShape(string field, Action<GeoShapeFilterDescriptor> filterDescriptor)
467+
{
468+
var filter = new GeoShapeFilterDescriptor();
469+
if (filterDescriptor == null)
470+
return CreateConditionlessFilterDescriptor("geo_shape", filter);
471+
472+
filterDescriptor(filter);
473+
if (filter.IsConditionless)
474+
return CreateConditionlessFilterDescriptor("geo_shape", filter);
475+
476+
return this.SetDictionary("geo_shape", field, filter, (d, b) =>
477+
{
478+
b.GeoShapeFilter = d;
479+
});
454480

455481
}
482+
456483
/// <summary>
457484
/// A filter allowing to include hits that only fall within a polygon of points.
458485
/// </summary>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Newtonsoft.Json;
6+
7+
namespace Nest
8+
{
9+
/// <summary>
10+
/// An object to describe a geoshape vetor
11+
/// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-filter.html
12+
/// </summary>
13+
public class GeoShapeVector
14+
{
15+
[JsonProperty("type")]
16+
public string Type { get; set; }
17+
18+
[JsonProperty("coordinates")]
19+
public IEnumerable<IEnumerable<double>> Coordinates { get; set; }
20+
}
21+
}

src/Nest/Nest.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@
7070
</ItemGroup>
7171
<ItemGroup>
7272
<Compile Include="Domain\Connection\AsyncRequestOperation.cs" />
73+
<Compile Include="Domain\DSL\GeoShapeVector.cs" />
7374
<Compile Include="Domain\Mapping\Descriptors\CompletionMappingDescriptor.cs" />
7475
<Compile Include="Domain\Mapping\Types\CompletionMapping.cs" />
7576
<Compile Include="Domain\Responses\RootVersionInfoResponse.cs" />
77+
<Compile Include="DSL\Filter\GeoShapeFilterDescriptor.cs" />
7678
<Compile Include="DSL\Suggest\FuzzySuggestDescriptor.cs" />
7779
<Compile Include="DSL\Filter\RegexpFilterDescriptor.cs" />
7880
<Compile Include="DSL\Query\RegexpQueryDescriptor.cs" />

src/RawClientGenerator/Views/Enums.Generated.cshtml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
@using System.Collections.Generic
22
@using System.Linq
33
@using RawClientGenerator
4+
5+
@functions {
6+
private string CreateEnum
7+
(string o)
8+
{
9+
return string.Format("[EnumMember(Value = \"{0}\")]\r\n\t\t{1}", o, o.ToPascalCase());
10+
}
11+
}
412
using System;
513
using System.Collections.Generic;
614
using System.Linq;
@@ -21,8 +29,8 @@ namespace Nest
2129
[JsonConverter(typeof(StringEnumConverter))]
2230
public enum @e.Name
2331
{
24-
@Raw(string.Join(",\r\n\t\t", e.Options.Select(o=> string.Format("[EnumMember(Value = \"{0}\")]\r\n\t\t{1}", o, o.ToPascalCase()))))
25-
}
32+
@Raw(string.Join(",\r\n\t\t", e.Options.Select(CreateEnum)))
33+
}
2634
</text>
2735
}
2836
}

0 commit comments

Comments
 (0)