Skip to content

Commit c729779

Browse files
author
Stuart Cam
authored
Add support for ip_range datatype (Backport of #3202 to 6.x) (#3216)
Add support for ip_range datatype (Backport of #3202 to 6.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 e8cd0e5 commit c729779

File tree

16 files changed

+203
-19
lines changed

16 files changed

+203
-19
lines changed

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
@@ -73,6 +73,9 @@ public IProperty IntegerRange(Func<IntegerRangePropertyDescriptor<T>, IIntegerRa
7373
public IProperty LongRange(Func<LongRangePropertyDescriptor<T>, ILongRangeProperty> selector) =>
7474
selector?.Invoke(new LongRangePropertyDescriptor<T>());
7575

76+
public IProperty IpRange(Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector) =>
77+
selector?.Invoke(new IpRangePropertyDescriptor<T>());
78+
7679
public IProperty Join(Func<JoinPropertyDescriptor<T>, IJoinProperty> selector) =>
7780
selector?.Invoke(new JoinPropertyDescriptor<T>());
7881

@@ -239,6 +242,8 @@ public IProperty Scalar(Expression<Func<T, IntegerRange>> field, Func<IntegerRan
239242
selector.InvokeOrDefault(new IntegerRangePropertyDescriptor<T>().Name(field));
240243
public IProperty Scalar(Expression<Func<T, FloatRange>> field, Func<FloatRangePropertyDescriptor<T>, IFloatRangeProperty> selector = null) =>
241244
selector.InvokeOrDefault(new FloatRangePropertyDescriptor<T>().Name(field));
245+
public IProperty Scalar(Expression<Func<T, IpAddressRange>> field, Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector = null) =>
246+
selector.InvokeOrDefault(new IpRangePropertyDescriptor<T>().Name(field));
242247
#pragma warning restore CS3001 // Argument type is not CLS-compliant
243248
}
244249
}
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+
}
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+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public abstract class RangePropertyDescriptorBase<TDescriptor, TInterface, T>
4949

5050
protected RangePropertyDescriptorBase(RangeType type) : base(type.ToFieldType()) { }
5151

52-
/// <inheritdoc/>
52+
/// <inheritdoc cref="IRangeProperty.Coerce"/>
5353
public TDescriptor Coerce(bool? coerce = true) => Assign(a => a.Coerce = coerce);
54-
/// <inheritdoc/>
54+
/// <inheritdoc cref="IRangeProperty.Boost"/>
5555
public TDescriptor Boost(double? boost) => Assign(a => a.Boost = boost);
56-
/// <inheritdoc/>
56+
/// <inheritdoc cref="IRangeProperty.Index"/>
5757
public TDescriptor Index(bool? index = true) => Assign(a => a.Index = index);
5858
}
5959
}

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
@@ -84,6 +84,8 @@ public enum FieldType
8484
DoubleRange,
8585
[EnumMember(Value = "date_range")]
8686
DateRange,
87+
[EnumMember(Value = "ip_range")]
88+
IpRange,
8789
[EnumMember(Value = "join")]
8890
Join,
8991
}

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
@@ -72,6 +72,7 @@ TReturnType Nested<TChild>(Func<NestedPropertyDescriptor<T, TChild>, INestedProp
7272
TReturnType FloatRange(Func<FloatRangePropertyDescriptor<T>, IFloatRangeProperty> selector);
7373
TReturnType IntegerRange(Func<IntegerRangePropertyDescriptor<T>, IIntegerRangeProperty> selector);
7474
TReturnType LongRange(Func<LongRangePropertyDescriptor<T>, ILongRangeProperty> selector);
75+
TReturnType IpRange(Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector);
7576
TReturnType Join(Func<JoinPropertyDescriptor<T>, IJoinProperty> selector);
7677
}
7778

@@ -127,6 +128,8 @@ public PropertiesDescriptor<T> Nested<TChild>(Func<NestedPropertyDescriptor<T, T
127128

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

131+
public PropertiesDescriptor<T> IpRange(Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector) => SetProperty(selector);
132+
130133
public PropertiesDescriptor<T> Join(Func<JoinPropertyDescriptor<T>, IJoinProperty> selector) => SetProperty(selector);
131134

132135
public PropertiesDescriptor<T> Custom(IProperty customType) => SetProperty(customType);

src/Nest/Mapping/Types/PropertiesJsonConverter.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,10 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
5454
foreach (var p in o.Properties())
5555
{
5656
var name = p.Name;
57-
var po = p.First as JObject;
58-
if (po == null) continue;
59-
60-
var mapping = _elasticTypeConverter.ReadJson(po.CreateReader(), objectType, existingValue, serializer) as IProperty;
61-
if (mapping == null) continue;
57+
if (!(p.First is JObject po)) continue;
58+
if (!(_elasticTypeConverter.ReadJson(po.CreateReader(), objectType, existingValue, serializer) is IProperty mapping)) continue;
6259

6360
mapping.Name = name;
64-
6561
r.Add(name, mapping);
6662
}
6763
return r;

0 commit comments

Comments
 (0)