Skip to content

Commit 37a34e0

Browse files
author
Stuart Cam
authored
Add support for ip_range datatype (Backport of #3202 to 5.x) (#3215)
Backport of #3202 to 5.x This commit adds support for the ip_range datatype. The existing IpRange type used for IpRangeAggregation renamed to IpRangeAggregationRange to allow for the introduction of an IpRange type whose name aligns with other range datatypes.
1 parent 4b980eb commit 37a34e0

File tree

17 files changed

+215
-21
lines changed

17 files changed

+215
-21
lines changed

src/Nest/Aggregations/Bucket/IpRange/IpRangeAggregation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface IIpRangeAggregation : IBucketAggregation
1313
[JsonProperty("field")]
1414
Field Field { get; set; }
1515

16-
[JsonProperty(PropertyName = "ranges")]
16+
[JsonProperty("ranges")]
1717
IEnumerable<IIpRange> Ranges { get; set; }
1818
}
1919

src/Nest/CommonOptions/Range/Ranges.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,18 @@ public class LongRange
7373
[JsonProperty("lt")]
7474
public long? LessThan { get; set; }
7575
}
76+
public class IpAddressRange
77+
{
78+
[JsonProperty("gte")]
79+
public string GreaterThanOrEqualTo { get; set; }
80+
81+
[JsonProperty("lte")]
82+
public string LessThanOrEqualTo { get; set; }
83+
84+
[JsonProperty("gt")]
85+
public string GreaterThan { get; set; }
86+
87+
[JsonProperty("lt")]
88+
public string LessThan { get; set; }
89+
}
7690
}

src/Nest/Mapping/DynamicTemplate/SingleMapping.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public IProperty IntegerRange(Func<IntegerRangePropertyDescriptor<T>, IIntegerRa
8383
public IProperty LongRange(Func<LongRangePropertyDescriptor<T>, ILongRangeProperty> selector) =>
8484
selector?.Invoke(new LongRangePropertyDescriptor<T>());
8585

86+
public IProperty IpRange(Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector) =>
87+
selector?.Invoke(new IpRangePropertyDescriptor<T>());
88+
8689
#pragma warning disable CS3001 // Argument type is not CLS-compliant
8790
public IProperty Scalar(Expression<Func<T, int>> field, Func<NumberPropertyDescriptor<T>, INumberProperty> selector = null) =>
8891
selector.InvokeOrDefault(new NumberPropertyDescriptor<T>().Name(field).Type(NumberType.Integer));
@@ -246,6 +249,8 @@ public IProperty Scalar(Expression<Func<T, IntegerRange>> field, Func<IntegerRan
246249
selector.InvokeOrDefault(new IntegerRangePropertyDescriptor<T>().Name(field));
247250
public IProperty Scalar(Expression<Func<T, FloatRange>> field, Func<FloatRangePropertyDescriptor<T>, IFloatRangeProperty> selector = null) =>
248251
selector.InvokeOrDefault(new FloatRangePropertyDescriptor<T>().Name(field));
252+
public IProperty Scalar(Expression<Func<T, IpAddressRange>> field, Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector = null) =>
253+
selector.InvokeOrDefault(new IpRangePropertyDescriptor<T>().Name(field));
249254
#pragma warning restore CS3001 // Argument type is not CLS-compliant
250255
}
251256
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using Elasticsearch.Net;
3+
4+
namespace Nest
5+
{
6+
public class IpRangeAttribute : RangePropertyAttributeBase, IIpRangeProperty
7+
{
8+
public IpRangeAttribute() : base(RangeType.IpRange) { }
9+
}
10+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using Elasticsearch.Net;
3+
using Newtonsoft.Json;
4+
5+
namespace Nest
6+
{
7+
[JsonObject(MemberSerialization.OptIn)]
8+
public interface IIpRangeProperty : IRangeProperty { }
9+
10+
public class IpRangeProperty : RangePropertyBase, IIpRangeProperty
11+
{
12+
public IpRangeProperty() : base(RangeType.IpRange) { }
13+
}
14+
15+
public class IpRangePropertyDescriptor<T>
16+
: RangePropertyDescriptorBase<IpRangePropertyDescriptor<T>, IIpRangeProperty, T>, IIpRangeProperty
17+
where T : class
18+
{
19+
public IpRangePropertyDescriptor() : base(RangeType.IpRange) { }
20+
}
21+
}

src/Nest/Mapping/Types/Core/Range/RangePropertyBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ public abstract class RangePropertyDescriptorBase<TDescriptor, TInterface, T>
6565

6666
protected RangePropertyDescriptorBase(RangeType type) : base(type.ToFieldType()) { }
6767

68-
/// <inheritdoc/>
68+
/// <inheritdoc cref="IRangeProperty.Coerce"/>
6969
public TDescriptor Coerce(bool coerce = true) => Assign(a => a.Coerce = coerce);
70-
/// <inheritdoc/>
70+
/// <inheritdoc cref="IRangeProperty.Boost"/>
7171
public TDescriptor Boost(double boost) => Assign(a => a.Boost = boost);
72-
/// <inheritdoc/>
72+
/// <inheritdoc cref="IRangeProperty.IncludeInAll"/>
7373
/// <remarks>Removed in 6.x</remarks>
7474
public TDescriptor IncludeInAll(bool includeInAll = true) => Assign(a => a.IncludeInAll = includeInAll);
75-
/// <inheritdoc/>
75+
/// <inheritdoc cref="IRangeProperty.Index"/>
7676
public TDescriptor Index(bool index = true) => Assign(a => a.Index = index);
7777
}
7878
}

src/Nest/Mapping/Types/Core/Range/RangeType.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ public enum RangeType
3232
/// A range of date values represented as unsigned 64-bit integer milliseconds elapsed since system epoch.
3333
/// </summary>
3434
[EnumMember(Value = "date_range")]
35-
DateRange
35+
DateRange,
36+
/// <summary>
37+
/// A range of ip values supporting either IPv4 or IPv6 (or mixed) addresses.
38+
/// </summary>
39+
[EnumMember(Value = "ip_range")]
40+
IpRange
3641
}
3742
internal static class RangeTypeExtensions
3843
{
@@ -45,6 +50,7 @@ public static FieldType ToFieldType(this RangeType rangeType)
4550
case RangeType.LongRange: return FieldType.LongRange;
4651
case RangeType.DoubleRange: return FieldType.DoubleRange;
4752
case RangeType.DateRange: return FieldType.DateRange;
53+
case RangeType.IpRange: return FieldType.IpRange;
4854
default:
4955
throw new ArgumentOutOfRangeException(nameof(rangeType), rangeType, null);
5056
}

src/Nest/Mapping/Types/FieldType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,7 @@ public enum FieldType
9191
DoubleRange,
9292
[EnumMember(Value = "date_range")]
9393
DateRange,
94+
[EnumMember(Value = "ip_range")]
95+
IpRange
9496
}
9597
}

src/Nest/Mapping/Types/Properties-Scalar.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public partial interface IPropertiesDescriptor<T, out TReturnType>
100100
TReturnType Scalar(Expression<Func<T, LongRange>> field, Func<LongRangePropertyDescriptor<T>, ILongRangeProperty> selector = null);
101101
TReturnType Scalar(Expression<Func<T, IntegerRange>> field, Func<IntegerRangePropertyDescriptor<T>, IIntegerRangeProperty> selector = null);
102102
TReturnType Scalar(Expression<Func<T, FloatRange>> field, Func<FloatRangePropertyDescriptor<T>, IFloatRangeProperty> selector = null);
103+
TReturnType Scalar(Expression<Func<T, IpAddressRange>> field, Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector = null);
103104
#pragma warning restore CS3001 // Argument type is not CLS-compliant
104105
}
105106

@@ -269,6 +270,8 @@ public PropertiesDescriptor<T> Scalar(Expression<Func<T, IntegerRange>> field, F
269270
SetProperty(selector.InvokeOrDefault(new IntegerRangePropertyDescriptor<T>().Name(field)));
270271
public PropertiesDescriptor<T> Scalar(Expression<Func<T, FloatRange>> field, Func<FloatRangePropertyDescriptor<T>, IFloatRangeProperty> selector = null) =>
271272
SetProperty(selector.InvokeOrDefault(new FloatRangePropertyDescriptor<T>().Name(field)));
273+
public PropertiesDescriptor<T> Scalar(Expression<Func<T, IpAddressRange>> field, Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector = null) =>
274+
SetProperty(selector.InvokeOrDefault(new IpRangePropertyDescriptor<T>().Name(field)));
272275

273276
#pragma warning restore CS3001 // Argument type is not CLS-compliant
274277
}

src/Nest/Mapping/Types/Properties.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ TReturnType Nested<TChild>(Func<NestedPropertyDescriptor<T, TChild>, INestedProp
6767
TReturnType FloatRange(Func<FloatRangePropertyDescriptor<T>, IFloatRangeProperty> selector);
6868
TReturnType IntegerRange(Func<IntegerRangePropertyDescriptor<T>, IIntegerRangeProperty> selector);
6969
TReturnType LongRange(Func<LongRangePropertyDescriptor<T>, ILongRangeProperty> selector);
70+
TReturnType IpRange(Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector);
7071
}
7172

7273
public partial class PropertiesDescriptor<T> : IsADictionaryDescriptorBase<PropertiesDescriptor<T>, IProperties, PropertyName, IProperty>, IPropertiesDescriptor<T, PropertiesDescriptor<T>>
@@ -128,6 +129,8 @@ public PropertiesDescriptor<T> Nested<TChild>(Func<NestedPropertyDescriptor<T, T
128129

129130
public PropertiesDescriptor<T> LongRange(Func<LongRangePropertyDescriptor<T>, ILongRangeProperty> selector) => SetProperty(selector);
130131

132+
public PropertiesDescriptor<T> IpRange(Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector) => SetProperty(selector);
133+
131134
public PropertiesDescriptor<T> Custom(IProperty customType) => SetProperty(customType);
132135

133136
private PropertiesDescriptor<T> SetProperty<TDescriptor, TInterface>(Func<TDescriptor, TInterface> selector)

0 commit comments

Comments
 (0)