Skip to content

Commit b71bbad

Browse files
committed
Strongly typed geo precision as combination of double and unit and wrote a converter for it with tests
1 parent a72268e commit b71bbad

File tree

10 files changed

+173
-9
lines changed

10 files changed

+173
-9
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public GeoShapeMappingDescriptor<T> TreeLevels(int treeLevels)
3030
return this;
3131
}
3232

33-
public GeoShapeMappingDescriptor<T> Precision(GeoPrecision precision)
33+
public GeoShapeMappingDescriptor<T> Precision(double precision, GeoPrecisionUnit unit)
3434
{
35-
this._Mapping.Precision = precision;
35+
this._Mapping.Precision = new GeoPrecision(precision, unit);
3636
return this;
3737
}
3838

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class GeoShapeMapping : IElasticType
2020
public GeoTree? Tree { get; set; }
2121

2222
[JsonProperty("precision")]
23-
public GeoPrecision? Precision { get; set; }
23+
public GeoPrecision Precision { get; set; }
2424

2525
[JsonProperty("orientation")]
2626
public GeoOrientation? Orientation { get; set; }

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: 3 additions & 1 deletion
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" />
@@ -243,7 +244,7 @@
243244
<Compile Include="DSL\DeleteScriptDescriptor.cs" />
244245
<Compile Include="ElasticClient-GetIndex.cs" />
245246
<Compile Include="Enums\GeoOrientation.cs" />
246-
<Compile Include="Enums\GeoPrecision.cs" />
247+
<Compile Include="Enums\GeoPrecisionUnit.cs" />
247248
<Compile Include="Enums\GetIndexFeature.cs" />
248249
<Compile Include="DSL\GetScriptDescriptor.cs" />
249250
<Compile Include="DSL\SearchExistsDescriptor.cs" />
@@ -984,6 +985,7 @@
984985
<Compile Include="Resolvers\Converters\TokenFilterCollectionConverter.cs" />
985986
<Compile Include="Resolvers\Converters\TokenizerCollectionConverter.cs" />
986987
<Compile Include="Resolvers\Converters\TypeNameMarkerConverter.cs" />
988+
<Compile Include="Resolvers\Converters\GeoPrecisionConverter.cs" />
987989
<Compile Include="Resolvers\Converters\UriJsonConverter.cs" />
988990
<Compile Include="Resolvers\Converters\WarmerMappingConverter.cs" />
989991
<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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"elasticsearchprojects": {
3+
"properties": {
4+
"myGeoShape": {
5+
"type": "geo_shape",
6+
"precision": "1.2cm",
7+
"distance_error_pct": 0.025
8+
}
9+
}
10+
}
11+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
<Compile Include="Core\Indices\Similarity\SimilarityTests.cs" />
109109
<Compile Include="Core\Map\CustomMapping\ImagePluginMappingTests.cs" />
110110
<Compile Include="Core\Map\GenericTypes\GenericTypeMappingTests.cs" />
111+
<Compile Include="Core\Map\GeoShapeMapping\GeoShapeMappingTests.cs" />
111112
<Compile Include="Core\Map\GetMappingSerializationTests.cs" />
112113
<Compile Include="Core\Map\MappingBehaviourTests.cs" />
113114
<None Include="Cluster\PutSettings\PutSettings.json">
@@ -178,6 +179,9 @@
178179
<None Include="Core\Map\GenericTypes\GenericTypeMapping.json">
179180
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
180181
</None>
182+
<None Include="Core\Map\GeoShapeMapping\PrecisionSerializesAsString.json">
183+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
184+
</None>
181185
<None Include="Core\Map\Properties\CompletionProperty.json">
182186
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
183187
</None>

0 commit comments

Comments
 (0)