Skip to content

Commit 17950ad

Browse files
Stuart Camrusscam
authored andcommitted
Allow geo_shape types to be coerced into valid shapes. (#3718)
* Allow geo_shape types to be coerced into valid shapes.
1 parent 707ee73 commit 17950ad

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

src/Nest/Mapping/Types/Geo/GeoShape/GeoShapeAttribute.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ public int TreeLevels
6363
set => Self.TreeLevels = value;
6464
}
6565

66+
/// <inheritdoc cref="IGeoShapeProperty.Coerce" />
67+
public bool Coerce
68+
{
69+
get => Self.Coerce.GetValueOrDefault(true);
70+
set => Self.Coerce = value;
71+
}
72+
6673
double? IGeoShapeProperty.DistanceErrorPercentage { get; set; }
6774
bool? IGeoShapeProperty.IgnoreMalformed { get; set; }
6875
bool? IGeoShapeProperty.IgnoreZValue { get; set; }
@@ -74,5 +81,7 @@ public int TreeLevels
7481

7582
GeoTree? IGeoShapeProperty.Tree { get; set; }
7683
int? IGeoShapeProperty.TreeLevels { get; set; }
84+
85+
bool? IGeoShapeProperty.Coerce { get; set; }
7786
}
7887
}

src/Nest/Mapping/Types/Geo/GeoShape/GeoShapeProperty.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public interface IGeoShapeProperty : IDocValuesProperty
9797
/// </summary>
9898
[JsonProperty("tree_levels")]
9999
int? TreeLevels { get; set; }
100+
101+
/// <summary>
102+
/// Should the data be coerced into becoming a valid geo shape (for instance closing a polygon)
103+
/// </summary>
104+
[JsonProperty("coerce")]
105+
bool? Coerce { get; set; }
100106
}
101107

102108
/// <inheritdoc cref="IGeoShapeProperty" />
@@ -131,6 +137,9 @@ public GeoShapeProperty() : base(FieldType.GeoShape) { }
131137

132138
/// <inheritdoc />
133139
public int? TreeLevels { get; set; }
140+
141+
/// <inheritdoc />
142+
public bool? Coerce { get; set; }
134143
}
135144

136145
/// <inheritdoc cref="IGeoShapeProperty" />
@@ -151,6 +160,8 @@ public GeoShapePropertyDescriptor() : base(FieldType.GeoShape) { }
151160
GeoTree? IGeoShapeProperty.Tree { get; set; }
152161
int? IGeoShapeProperty.TreeLevels { get; set; }
153162

163+
bool? IGeoShapeProperty.Coerce { get; set; }
164+
154165
/// <inheritdoc cref="IGeoShapeProperty.Tree" />
155166
public GeoShapePropertyDescriptor<T> Tree(GeoTree? tree) => Assign(tree, (a, v) => a.Tree = v);
156167

@@ -181,5 +192,9 @@ public GeoShapePropertyDescriptor<T> IgnoreMalformed(bool? ignoreMalformed = tru
181192
/// <inheritdoc cref="IGeoShapeProperty.IgnoreZValue" />
182193
public GeoShapePropertyDescriptor<T> IgnoreZValue(bool? ignoreZValue = true) =>
183194
Assign(ignoreZValue, (a, v) => a.IgnoreZValue = v);
195+
196+
/// <inheritdoc cref="IGeoShapeProperty.Coerce" />
197+
public GeoShapePropertyDescriptor<T> Coerce(bool? coerce = true) =>
198+
Assign(coerce, (a, v) => a.Coerce = v);
184199
}
185200
}

src/Tests/Tests/Mapping/Types/Geo/GeoShape/GeoShapeAttributeTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public class GeoShapeTest
1010
Strategy = GeoStrategy.Recursive,
1111
TreeLevels = 3,
1212
PointsOnly = true,
13-
DistanceErrorPercentage = 1.0)]
13+
DistanceErrorPercentage = 1.0,
14+
Coerce = true)]
1415
public object Full { get; set; }
1516

1617
[GeoShape]
@@ -31,7 +32,8 @@ public class GeoShapeAttributeTests : AttributeTestsBase<GeoShapeTest>
3132
strategy = "recursive",
3233
tree_levels = 3,
3334
points_only = true,
34-
distance_error_pct = 1.0
35+
distance_error_pct = 1.0,
36+
coerce = true
3537
},
3638
minimal = new
3739
{

src/Tests/Tests/Mapping/Types/Geo/GeoShape/GeoShapePropertyTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public GeoShapePropertyTests(WritableCluster cluster, EndpointUsage usage) : bas
2222
strategy = "recursive",
2323
tree_levels = 3,
2424
points_only = true,
25-
distance_error_pct = 1.0
25+
distance_error_pct = 1.0,
26+
coerce = true
2627
}
2728
}
2829
};
@@ -36,6 +37,7 @@ public GeoShapePropertyTests(WritableCluster cluster, EndpointUsage usage) : bas
3637
.TreeLevels(3)
3738
.PointsOnly()
3839
.DistanceErrorPercentage(1.0)
40+
.Coerce()
3941
);
4042

4143

@@ -49,7 +51,8 @@ public GeoShapePropertyTests(WritableCluster cluster, EndpointUsage usage) : bas
4951
Strategy = GeoStrategy.Recursive,
5052
TreeLevels = 3,
5153
PointsOnly = true,
52-
DistanceErrorPercentage = 1.0
54+
DistanceErrorPercentage = 1.0,
55+
Coerce = true
5356
}
5457
}
5558
};

0 commit comments

Comments
 (0)