Skip to content

Commit ba24902

Browse files
committed
Merge pull request #1248 from elasticsearch/feature/precision-and-orientation-geo-shape
fix #1247 add support for precision and orientation on geo shape type ma...
2 parents 9640a1a + 8d3822f commit ba24902

File tree

12 files changed

+229
-5
lines changed

12 files changed

+229
-5
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using Nest.Resolvers.Converters;
7+
8+
namespace Nest
9+
{
10+
[JsonConverter(typeof(GeoPrecisionConverter))]
11+
public class GeoPrecision
12+
{
13+
public double Precision { get; private set; }
14+
public GeoPrecisionUnit Unit { get; private set; }
15+
16+
public GeoPrecision(double precision, GeoPrecisionUnit unit)
17+
{
18+
this.Precision = precision;
19+
this.Unit = unit;
20+
}
21+
}
22+
}

src/Nest/Domain/Mapping/Descriptors/GeoShapeMappingDescriptor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ public GeoShapeMappingDescriptor<T> TreeLevels(int treeLevels)
3030
return this;
3131
}
3232

33+
public GeoShapeMappingDescriptor<T> Precision(double precision, GeoPrecisionUnit unit)
34+
{
35+
this._Mapping.Precision = new GeoPrecision(precision, unit);
36+
return this;
37+
}
38+
39+
public GeoShapeMappingDescriptor<T> Orientation(GeoOrientation orientation)
40+
{
41+
this._Mapping.Orientation = orientation;
42+
return this;
43+
}
44+
3345
public GeoShapeMappingDescriptor<T> DistanceErrorPercentage(double distanceErrorPercentage)
3446
{
3547
this._Mapping.DistanceErrorPercentage = distanceErrorPercentage;

src/Nest/Domain/Mapping/Descriptors/MappingTransformDescriptor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public MappingTransformDescriptor Language(string language)
2828
return this;
2929
}
3030

31-
public MappingTransformDescriptor Language(ScriptLang language)
32-
{
33-
this._mappingTransform.Language = language.GetStringValue();
34-
return this;
35-
}
31+
public MappingTransformDescriptor Language(ScriptLang language)
32+
{
33+
this._mappingTransform.Language = language.GetStringValue();
34+
return this;
35+
}
3636
}
3737
}

src/Nest/Domain/Mapping/Types/GeoShapeMapping.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public class GeoShapeMapping : IElasticType
1919
[JsonProperty("tree"), JsonConverter(typeof(StringEnumConverter))]
2020
public GeoTree? Tree { get; set; }
2121

22+
[JsonProperty("precision")]
23+
public GeoPrecision Precision { get; set; }
24+
25+
[JsonProperty("orientation")]
26+
public GeoOrientation? Orientation { get; set; }
27+
2228
[JsonProperty("tree_levels")]
2329
public int? TreeLevels { get; set; }
2430

src/Nest/Enums/GeoOrientation.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Converters;
3+
using System.Runtime.Serialization;
4+
5+
namespace Nest
6+
{
7+
[JsonConverter(typeof(StringEnumConverter))]
8+
public enum GeoOrientation
9+
{
10+
[EnumMember(Value = "cw")]
11+
ClockWise,
12+
[EnumMember(Value = "ccw")]
13+
CounterClockWise
14+
}
15+
}

src/Nest/Enums/GeoPrecision.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Converters;
3+
using System.Runtime.Serialization;
4+
5+
namespace Nest
6+
{
7+
[JsonConverter(typeof(StringEnumConverter))]
8+
public enum GeoPrecision
9+
{
10+
[EnumMember(Value = "in")]
11+
Inch,
12+
[EnumMember(Value = "yd")]
13+
Yard,
14+
[EnumMember(Value = "mi")]
15+
Miles,
16+
[EnumMember(Value = "km")]
17+
Kilometers,
18+
[EnumMember(Value = "m")]
19+
Meters,
20+
[EnumMember(Value = "cm")]
21+
Centimeters,
22+
[EnumMember(Value = "mm")]
23+
Millimeters
24+
}
25+
}

src/Nest/Enums/GeoPrecisionUnit.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Converters;
3+
using System.Runtime.Serialization;
4+
5+
namespace Nest
6+
{
7+
[JsonConverter(typeof(StringEnumConverter))]
8+
public enum GeoPrecisionUnit
9+
{
10+
[EnumMember(Value = "in")]
11+
Inch,
12+
[EnumMember(Value = "yd")]
13+
Yard,
14+
[EnumMember(Value = "mi")]
15+
Miles,
16+
[EnumMember(Value = "km")]
17+
Kilometers,
18+
[EnumMember(Value = "m")]
19+
Meters,
20+
[EnumMember(Value = "cm")]
21+
Centimeters,
22+
[EnumMember(Value = "mm")]
23+
Millimeters
24+
}
25+
}

src/Nest/Nest.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
<Compile Include="Domain\Cat\CatPluginsRecord.cs" />
131131
<Compile Include="Domain\Cat\CatPendingTasksRecord.cs" />
132132
<Compile Include="Domain\Geo\GeoLocation.cs" />
133+
<Compile Include="Domain\Geo\GeoPrecision.cs" />
133134
<Compile Include="Domain\Hit\ExplainGet.cs" />
134135
<Compile Include="Domain\Mapping\PropertyMapping.cs" />
135136
<Compile Include="Domain\Mapping\Descriptors\FieldDataFilterDescriptor.cs" />
@@ -242,6 +243,8 @@
242243
<Compile Include="DSL\Filter\GeoHashCellFilterDescriptor.cs" />
243244
<Compile Include="DSL\DeleteScriptDescriptor.cs" />
244245
<Compile Include="ElasticClient-GetIndex.cs" />
246+
<Compile Include="Enums\GeoOrientation.cs" />
247+
<Compile Include="Enums\GeoPrecisionUnit.cs" />
245248
<Compile Include="Enums\GetIndexFeature.cs" />
246249
<Compile Include="DSL\GetScriptDescriptor.cs" />
247250
<Compile Include="DSL\SearchExistsDescriptor.cs" />
@@ -982,6 +985,7 @@
982985
<Compile Include="Resolvers\Converters\TokenFilterCollectionConverter.cs" />
983986
<Compile Include="Resolvers\Converters\TokenizerCollectionConverter.cs" />
984987
<Compile Include="Resolvers\Converters\TypeNameMarkerConverter.cs" />
988+
<Compile Include="Resolvers\Converters\GeoPrecisionConverter.cs" />
985989
<Compile Include="Resolvers\Converters\UriJsonConverter.cs" />
986990
<Compile Include="Resolvers\Converters\WarmerMappingConverter.cs" />
987991
<Compile Include="Resolvers\Converters\ShardsSegmentConverter.cs" />
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Converters;
4+
using System.IO;
5+
using System.Text.RegularExpressions;
6+
7+
namespace Nest.Resolvers.Converters
8+
{
9+
public class GeoPrecisionConverter : JsonConverter
10+
{
11+
private static readonly Regex SplitRegex = new Regex(@"^(\d+(?:[.,]\d+)?)(\D+)$");
12+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
13+
{
14+
var p = value as GeoPrecision;
15+
if (p == null)
16+
{
17+
writer.WriteNull();
18+
return;
19+
}
20+
using (var sw = new StringWriter())
21+
using (var localWriter = new JsonTextWriter(sw))
22+
{
23+
serializer.Serialize(localWriter, p.Precision);
24+
localWriter.WriteRaw(p.Unit.GetStringValue());
25+
var s = sw.ToString();
26+
writer.WriteValue(s);
27+
}
28+
}
29+
30+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
31+
{
32+
if (reader.TokenType != JsonToken.String) return null;
33+
var v = reader.Value as string;
34+
if (v == null) return null;
35+
var matches = SplitRegex.Matches(v);
36+
if (matches.Count < 0
37+
|| matches[0].Groups.Count < 3)
38+
return null;
39+
double p;
40+
var sp = matches[0].Groups[1].Captures[0].Value;
41+
if (!double.TryParse(sp, out p)) return null;
42+
var unit = matches[0].Groups[2].Captures[0].Value.ToEnum<GeoPrecisionUnit>();
43+
if (!unit.HasValue) return null;
44+
return new GeoPrecision(p, unit.Value);
45+
}
46+
47+
public override bool CanConvert(Type objectType)
48+
{
49+
return true;
50+
}
51+
}
52+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Reflection;
4+
using Elasticsearch.Net;
5+
using FluentAssertions;
6+
using Nest.Resolvers;
7+
using Nest.Tests.MockData.Domain;
8+
using NUnit.Framework;
9+
10+
namespace Nest.Tests.Unit.Core.Map.GeoShape
11+
{
12+
[TestFixture]
13+
public class GeoShapeMappingTests : BaseJsonTests
14+
{
15+
16+
[Test]
17+
public void PrecisionSerializesAsString()
18+
{
19+
var result = this._client.Map<ElasticsearchProject>(m => m
20+
.Properties(props => props
21+
.GeoShape(s => s
22+
.Name(p => p.MyGeoShape)
23+
.Precision(1.2, GeoPrecisionUnit.Centimeters)
24+
.DistanceErrorPercentage(0.025)
25+
)
26+
)
27+
);
28+
this.JsonEquals(result.ConnectionStatus.Request, MethodInfo.GetCurrentMethod());
29+
}
30+
31+
[Test]
32+
public void PrecisionSurvivesDeserialize()
33+
{
34+
var mapping = new GeoShapeMapping()
35+
{
36+
Name = Property.Name<ElasticsearchProject>(p => p.MyGeoShape),
37+
Precision = new GeoPrecision(3.4, GeoPrecisionUnit.Yard)
38+
};
39+
var serialized = this._client.Serializer.Serialize(mapping);
40+
var deserialized = this._client.Serializer.Deserialize<GeoShapeMapping>(new MemoryStream(serialized));
41+
deserialized.Should().NotBeNull();
42+
deserialized.Precision.Should().NotBeNull();
43+
deserialized.Precision.Precision.Should().Be(3.4);
44+
deserialized.Precision.Unit.Should().Be(GeoPrecisionUnit.Yard);
45+
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)