Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Nest/Domain/Geo/GeoPrecision.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nest.Resolvers.Converters;

namespace Nest
{
[JsonConverter(typeof(GeoPrecisionConverter))]
public class GeoPrecision
{
public double Precision { get; private set; }
public GeoPrecisionUnit Unit { get; private set; }

public GeoPrecision(double precision, GeoPrecisionUnit unit)
{
this.Precision = precision;
this.Unit = unit;
}
}
}
12 changes: 12 additions & 0 deletions src/Nest/Domain/Mapping/Descriptors/GeoShapeMappingDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ public GeoShapeMappingDescriptor<T> TreeLevels(int treeLevels)
return this;
}

public GeoShapeMappingDescriptor<T> Precision(double precision, GeoPrecisionUnit unit)
{
this._Mapping.Precision = new GeoPrecision(precision, unit);
return this;
}

public GeoShapeMappingDescriptor<T> Orientation(GeoOrientation orientation)
{
this._Mapping.Orientation = orientation;
return this;
}

public GeoShapeMappingDescriptor<T> DistanceErrorPercentage(double distanceErrorPercentage)
{
this._Mapping.DistanceErrorPercentage = distanceErrorPercentage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public MappingTransformDescriptor Language(string language)
return this;
}

public MappingTransformDescriptor Language(ScriptLang language)
{
this._mappingTransform.Language = language.GetStringValue();
return this;
}
public MappingTransformDescriptor Language(ScriptLang language)
{
this._mappingTransform.Language = language.GetStringValue();
return this;
}
}
}
6 changes: 6 additions & 0 deletions src/Nest/Domain/Mapping/Types/GeoShapeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public class GeoShapeMapping : IElasticType
[JsonProperty("tree"), JsonConverter(typeof(StringEnumConverter))]
public GeoTree? Tree { get; set; }

[JsonProperty("precision")]
public GeoPrecision Precision { get; set; }

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

[JsonProperty("tree_levels")]
public int? TreeLevels { get; set; }

Expand Down
15 changes: 15 additions & 0 deletions src/Nest/Enums/GeoOrientation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Runtime.Serialization;

namespace Nest
{
[JsonConverter(typeof(StringEnumConverter))]
public enum GeoOrientation
{
[EnumMember(Value = "cw")]
ClockWise,
[EnumMember(Value = "ccw")]
CounterClockWise
}
}
25 changes: 25 additions & 0 deletions src/Nest/Enums/GeoPrecision.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Runtime.Serialization;

namespace Nest
{
[JsonConverter(typeof(StringEnumConverter))]
public enum GeoPrecision
{
[EnumMember(Value = "in")]
Inch,
[EnumMember(Value = "yd")]
Yard,
[EnumMember(Value = "mi")]
Miles,
[EnumMember(Value = "km")]
Kilometers,
[EnumMember(Value = "m")]
Meters,
[EnumMember(Value = "cm")]
Centimeters,
[EnumMember(Value = "mm")]
Millimeters
}
}
25 changes: 25 additions & 0 deletions src/Nest/Enums/GeoPrecisionUnit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Runtime.Serialization;

namespace Nest
{
[JsonConverter(typeof(StringEnumConverter))]
public enum GeoPrecisionUnit
{
[EnumMember(Value = "in")]
Inch,
[EnumMember(Value = "yd")]
Yard,
[EnumMember(Value = "mi")]
Miles,
[EnumMember(Value = "km")]
Kilometers,
[EnumMember(Value = "m")]
Meters,
[EnumMember(Value = "cm")]
Centimeters,
[EnumMember(Value = "mm")]
Millimeters
}
}
4 changes: 4 additions & 0 deletions src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
<Compile Include="Domain\Cat\CatPluginsRecord.cs" />
<Compile Include="Domain\Cat\CatPendingTasksRecord.cs" />
<Compile Include="Domain\Geo\GeoLocation.cs" />
<Compile Include="Domain\Geo\GeoPrecision.cs" />
<Compile Include="Domain\Hit\ExplainGet.cs" />
<Compile Include="Domain\Mapping\PropertyMapping.cs" />
<Compile Include="Domain\Mapping\Descriptors\FieldDataFilterDescriptor.cs" />
Expand Down Expand Up @@ -242,6 +243,8 @@
<Compile Include="DSL\Filter\GeoHashCellFilterDescriptor.cs" />
<Compile Include="DSL\DeleteScriptDescriptor.cs" />
<Compile Include="ElasticClient-GetIndex.cs" />
<Compile Include="Enums\GeoOrientation.cs" />
<Compile Include="Enums\GeoPrecisionUnit.cs" />
<Compile Include="Enums\GetIndexFeature.cs" />
<Compile Include="DSL\GetScriptDescriptor.cs" />
<Compile Include="DSL\SearchExistsDescriptor.cs" />
Expand Down Expand Up @@ -982,6 +985,7 @@
<Compile Include="Resolvers\Converters\TokenFilterCollectionConverter.cs" />
<Compile Include="Resolvers\Converters\TokenizerCollectionConverter.cs" />
<Compile Include="Resolvers\Converters\TypeNameMarkerConverter.cs" />
<Compile Include="Resolvers\Converters\GeoPrecisionConverter.cs" />
<Compile Include="Resolvers\Converters\UriJsonConverter.cs" />
<Compile Include="Resolvers\Converters\WarmerMappingConverter.cs" />
<Compile Include="Resolvers\Converters\ShardsSegmentConverter.cs" />
Expand Down
52 changes: 52 additions & 0 deletions src/Nest/Resolvers/Converters/GeoPrecisionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.IO;
using System.Text.RegularExpressions;

namespace Nest.Resolvers.Converters
{
public class GeoPrecisionConverter : JsonConverter
{
private static readonly Regex SplitRegex = new Regex(@"^(\d+(?:[.,]\d+)?)(\D+)$");
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var p = value as GeoPrecision;
if (p == null)
{
writer.WriteNull();
return;
}
using (var sw = new StringWriter())
using (var localWriter = new JsonTextWriter(sw))
{
serializer.Serialize(localWriter, p.Precision);
localWriter.WriteRaw(p.Unit.GetStringValue());
var s = sw.ToString();
writer.WriteValue(s);
}
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType != JsonToken.String) return null;
var v = reader.Value as string;
if (v == null) return null;
var matches = SplitRegex.Matches(v);
if (matches.Count < 0
|| matches[0].Groups.Count < 3)
return null;
double p;
var sp = matches[0].Groups[1].Captures[0].Value;
if (!double.TryParse(sp, out p)) return null;
var unit = matches[0].Groups[2].Captures[0].Value.ToEnum<GeoPrecisionUnit>();
if (!unit.HasValue) return null;
return new GeoPrecision(p, unit.Value);
}

public override bool CanConvert(Type objectType)
{
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Elasticsearch.Net;
using FluentAssertions;
using Nest.Resolvers;
using Nest.Tests.MockData.Domain;
using NUnit.Framework;

namespace Nest.Tests.Unit.Core.Map.GeoShape
{
[TestFixture]
public class GeoShapeMappingTests : BaseJsonTests
{

[Test]
public void PrecisionSerializesAsString()
{
var result = this._client.Map<ElasticsearchProject>(m => m
.Properties(props => props
.GeoShape(s => s
.Name(p => p.MyGeoShape)
.Precision(1.2, GeoPrecisionUnit.Centimeters)
.DistanceErrorPercentage(0.025)
)
)
);
this.JsonEquals(result.ConnectionStatus.Request, MethodInfo.GetCurrentMethod());
}

[Test]
public void PrecisionSurvivesDeserialize()
{
var mapping = new GeoShapeMapping()
{
Name = Property.Name<ElasticsearchProject>(p => p.MyGeoShape),
Precision = new GeoPrecision(3.4, GeoPrecisionUnit.Yard)
};
var serialized = this._client.Serializer.Serialize(mapping);
var deserialized = this._client.Serializer.Deserialize<GeoShapeMapping>(new MemoryStream(serialized));
deserialized.Should().NotBeNull();
deserialized.Precision.Should().NotBeNull();
deserialized.Precision.Precision.Should().Be(3.4);
deserialized.Precision.Unit.Should().Be(GeoPrecisionUnit.Yard);

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"elasticsearchprojects": {
"properties": {
"myGeoShape": {
"type": "geo_shape",
"precision": "1.2cm",
"distance_error_pct": 0.025
}
}
}
}
4 changes: 4 additions & 0 deletions src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
<Compile Include="Core\Indices\Similarity\SimilarityTests.cs" />
<Compile Include="Core\Map\CustomMapping\ImagePluginMappingTests.cs" />
<Compile Include="Core\Map\GenericTypes\GenericTypeMappingTests.cs" />
<Compile Include="Core\Map\GeoShape\GeoShapeMappingTests.cs" />
<Compile Include="Core\Map\GetMappingSerializationTests.cs" />
<Compile Include="Core\Map\MappingBehaviourTests.cs" />
<None Include="Cluster\PutSettings\PutSettings.json">
Expand Down Expand Up @@ -178,6 +179,9 @@
<None Include="Core\Map\GenericTypes\GenericTypeMapping.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Core\Map\GeoShape\PrecisionSerializesAsString.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Core\Map\Properties\CompletionProperty.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down