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
2 changes: 1 addition & 1 deletion src/Nest/Aggregations/Bucket/IpRange/IpRangeAggregation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IIpRangeAggregation : IBucketAggregation
[JsonProperty("field")]
Field Field { get; set; }

[JsonProperty(PropertyName = "ranges")]
[JsonProperty("ranges")]
IEnumerable<IIpRange> Ranges { get; set; }
}

Expand Down
14 changes: 14 additions & 0 deletions src/Nest/CommonOptions/Range/Ranges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,18 @@ public class LongRange
[JsonProperty("lt")]
public long? LessThan { get; set; }
}
public class IpAddressRange
{
[JsonProperty("gte")]
public string GreaterThanOrEqualTo { get; set; }

[JsonProperty("lte")]
public string LessThanOrEqualTo { get; set; }

[JsonProperty("gt")]
public string GreaterThan { get; set; }

[JsonProperty("lt")]
public string LessThan { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/Nest/Mapping/DynamicTemplate/SingleMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public IProperty IntegerRange(Func<IntegerRangePropertyDescriptor<T>, IIntegerRa
public IProperty LongRange(Func<LongRangePropertyDescriptor<T>, ILongRangeProperty> selector) =>
selector?.Invoke(new LongRangePropertyDescriptor<T>());

public IProperty IpRange(Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector) =>
selector?.Invoke(new IpRangePropertyDescriptor<T>());

#pragma warning disable CS3001 // Argument type is not CLS-compliant
public IProperty Scalar(Expression<Func<T, int>> field, Func<NumberPropertyDescriptor<T>, INumberProperty> selector = null) =>
selector.InvokeOrDefault(new NumberPropertyDescriptor<T>().Name(field).Type(NumberType.Integer));
Expand Down Expand Up @@ -246,6 +249,8 @@ public IProperty Scalar(Expression<Func<T, IntegerRange>> field, Func<IntegerRan
selector.InvokeOrDefault(new IntegerRangePropertyDescriptor<T>().Name(field));
public IProperty Scalar(Expression<Func<T, FloatRange>> field, Func<FloatRangePropertyDescriptor<T>, IFloatRangeProperty> selector = null) =>
selector.InvokeOrDefault(new FloatRangePropertyDescriptor<T>().Name(field));
public IProperty Scalar(Expression<Func<T, IpAddressRange>> field, Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector = null) =>
selector.InvokeOrDefault(new IpRangePropertyDescriptor<T>().Name(field));
#pragma warning restore CS3001 // Argument type is not CLS-compliant
}
}
10 changes: 10 additions & 0 deletions src/Nest/Mapping/Types/Core/Range/Ip/IpRangeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using Elasticsearch.Net;

namespace Nest
{
public class IpRangeAttribute : RangePropertyAttributeBase, IIpRangeProperty
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IpAddressRangeAttribute, to align with IpAddressRange?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎

{
public IpRangeAttribute() : base(RangeType.IpRange) { }
}
}
21 changes: 21 additions & 0 deletions src/Nest/Mapping/Types/Core/Range/Ip/IpRangeProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using Elasticsearch.Net;
using Newtonsoft.Json;

namespace Nest
{
[JsonObject(MemberSerialization.OptIn)]
public interface IIpRangeProperty : IRangeProperty { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIpAddressRangeProperty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎


public class IpRangeProperty : RangePropertyBase, IIpRangeProperty
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IpAddressRangeProperty ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎

{
public IpRangeProperty() : base(RangeType.IpRange) { }
}

public class IpRangePropertyDescriptor<T>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IpAddressRangePropertyDescriptor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎

: RangePropertyDescriptorBase<IpRangePropertyDescriptor<T>, IIpRangeProperty, T>, IIpRangeProperty
where T : class
{
public IpRangePropertyDescriptor() : base(RangeType.IpRange) { }
}
}
8 changes: 4 additions & 4 deletions src/Nest/Mapping/Types/Core/Range/RangePropertyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ public abstract class RangePropertyDescriptorBase<TDescriptor, TInterface, T>

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

/// <inheritdoc/>
/// <inheritdoc cref="IRangeProperty.Coerce"/>
public TDescriptor Coerce(bool coerce = true) => Assign(a => a.Coerce = coerce);
/// <inheritdoc/>
/// <inheritdoc cref="IRangeProperty.Boost"/>
public TDescriptor Boost(double boost) => Assign(a => a.Boost = boost);
/// <inheritdoc/>
/// <inheritdoc cref="IRangeProperty.IncludeInAll"/>
/// <remarks>Removed in 6.x</remarks>
public TDescriptor IncludeInAll(bool includeInAll = true) => Assign(a => a.IncludeInAll = includeInAll);
/// <inheritdoc/>
/// <inheritdoc cref="IRangeProperty.Index"/>
public TDescriptor Index(bool index = true) => Assign(a => a.Index = index);
}
}
8 changes: 7 additions & 1 deletion src/Nest/Mapping/Types/Core/Range/RangeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ public enum RangeType
/// A range of date values represented as unsigned 64-bit integer milliseconds elapsed since system epoch.
/// </summary>
[EnumMember(Value = "date_range")]
DateRange
DateRange,
/// <summary>
/// A range of ip values supporting either IPv4 or IPv6 (or mixed) addresses.
/// </summary>
[EnumMember(Value = "ip_range")]
IpRange
}
internal static class RangeTypeExtensions
{
Expand All @@ -45,6 +50,7 @@ public static FieldType ToFieldType(this RangeType rangeType)
case RangeType.LongRange: return FieldType.LongRange;
case RangeType.DoubleRange: return FieldType.DoubleRange;
case RangeType.DateRange: return FieldType.DateRange;
case RangeType.IpRange: return FieldType.IpRange;
default:
throw new ArgumentOutOfRangeException(nameof(rangeType), rangeType, null);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Nest/Mapping/Types/FieldType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,7 @@ public enum FieldType
DoubleRange,
[EnumMember(Value = "date_range")]
DateRange,
[EnumMember(Value = "ip_range")]
IpRange
}
}
3 changes: 3 additions & 0 deletions src/Nest/Mapping/Types/Properties-Scalar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public partial interface IPropertiesDescriptor<T, out TReturnType>
TReturnType Scalar(Expression<Func<T, LongRange>> field, Func<LongRangePropertyDescriptor<T>, ILongRangeProperty> selector = null);
TReturnType Scalar(Expression<Func<T, IntegerRange>> field, Func<IntegerRangePropertyDescriptor<T>, IIntegerRangeProperty> selector = null);
TReturnType Scalar(Expression<Func<T, FloatRange>> field, Func<FloatRangePropertyDescriptor<T>, IFloatRangeProperty> selector = null);
TReturnType Scalar(Expression<Func<T, IpAddressRange>> field, Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector = null);
#pragma warning restore CS3001 // Argument type is not CLS-compliant
}

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

#pragma warning restore CS3001 // Argument type is not CLS-compliant
}
Expand Down
3 changes: 3 additions & 0 deletions src/Nest/Mapping/Types/Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ TReturnType Nested<TChild>(Func<NestedPropertyDescriptor<T, TChild>, INestedProp
TReturnType FloatRange(Func<FloatRangePropertyDescriptor<T>, IFloatRangeProperty> selector);
TReturnType IntegerRange(Func<IntegerRangePropertyDescriptor<T>, IIntegerRangeProperty> selector);
TReturnType LongRange(Func<LongRangePropertyDescriptor<T>, ILongRangeProperty> selector);
TReturnType IpRange(Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector);
}

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

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

public PropertiesDescriptor<T> IpRange(Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector) => SetProperty(selector);

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

private PropertiesDescriptor<T> SetProperty<TDescriptor, TInterface>(Func<TDescriptor, TInterface> selector)
Expand Down
2 changes: 2 additions & 0 deletions src/Nest/Mapping/Types/PropertyJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
return jObject.ToObject<IntegerRangeProperty>();
case FieldType.LongRange:
return jObject.ToObject<LongRangeProperty>();
case FieldType.IpRange:
return jObject.ToObject<IpRangeProperty>();
case FieldType.None:
break;
default:
Expand Down
3 changes: 3 additions & 0 deletions src/Nest/Mapping/Visitor/PropertyWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ private IProperty InferProperty(Type type)
if (type == typeof(LongRange))
return new LongRangeProperty();

if (type == typeof(IpAddressRange))
return new IpRangeProperty();

return new ObjectProperty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,30 @@ public IpRangeAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba

protected override object ExpectJson => new
{
aggs = new
{
aggs = new {
ip_ranges = new
{
ip_range = new
{
field = "leadDeveloper.iPAddress",
ranges = new object[]
{
new { to = "10.0.0.5" },
new { from = "10.0.0.5" }
new {to = "127.0.0.1"},
new {from = "127.0.0.1"}
}
}
}
}
}
};

protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s
.Aggregations(a => a
.IpRange("ip_ranges", ip => ip
.Field(p => p.LeadDeveloper.IPAddress)
.Ranges(
r => r.To("10.0.0.5"),
r => r.From("10.0.0.5")
)
.Aggregations(a => a.IpRange("ip_ranges", ip => ip
.Field(p => p.LeadDeveloper.IPAddress)
.Ranges(
r => r.To("127.0.0.1"),
r => r.From("127.0.0.1")
)
);
));

protected override SearchRequest<Project> Initializer =>
new SearchRequest<Project>
Expand All @@ -53,8 +50,8 @@ public IpRangeAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
Field = Field((Project p) => p.LeadDeveloper.IPAddress),
Ranges = new List<Nest.IpRange>
{
new Nest.IpRange { To = "10.0.0.5" },
new Nest.IpRange { From = "10.0.0.5" }
new Nest.IpRange {To = "127.0.0.1"},
new Nest.IpRange {From = "127.0.0.1"}
}
}
};
Expand Down
24 changes: 24 additions & 0 deletions src/Tests/Framework/MockData/Ranges.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net;
using Bogus;
using Nest;

Expand All @@ -11,6 +12,7 @@ public class Ranges
public FloatRange Floats { get; set; }
public IntegerRange Integers { get; set; }
public LongRange Longs { get; set; }
public IpAddressRange Ips { get; set; }

//for deserialization
public Ranges() { }
Expand All @@ -23,6 +25,7 @@ private Ranges(Faker faker)
SetFloats(faker, r);
SetIntegers(faker, r);
SetLongs(faker, r);
SetIps(faker, r);
}

private void SetDates(Faker faker, Func<bool> r)
Expand Down Expand Up @@ -70,6 +73,27 @@ private void SetLongs(Faker faker, Func<bool> r)
SwapAssign(r(), high, v => d.LessThan = v, v => d.LessThanOrEqualTo = v);
this.Longs = d;
}
private void SetIps(Faker faker, Func<bool> r)
{
var low = faker.Internet.Ip();
var high = faker.Internet.Ip();
var lowBytes = IPAddress.Parse(low).GetAddressBytes();
var highBytes = IPAddress.Parse(high).GetAddressBytes();
for (int i = 0; i < lowBytes.Length; i++)
{
if (lowBytes[i] > highBytes[i])
{
var s = low;
low = high;
high = s;
break;
}
}
var d = new IpAddressRange();
SwapAssign(r(), low, v => d.GreaterThan = v, v => d.GreaterThanOrEqualTo = v);
SwapAssign(r(), high, v => d.LessThan = v, v => d.LessThanOrEqualTo = v);
this.Ips = d;
}

private static void SwapAssign<T>(bool b, T value, Action<T> first, Action<T> second)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ protected override LazyResponses ClientUsage() => Calls(
},
longs = new {
type = "long_range"
},
ips = new
{
type = "ip_range"
}
},
type = "object"
Expand Down Expand Up @@ -315,6 +319,7 @@ protected override LazyResponses ClientUsage() => Calls(
{ p => p.Floats, new FloatRangeProperty() },
{ p => p.Integers, new IntegerRangeProperty() },
{ p => p.Longs, new LongRangeProperty() },
{ p => p.Ips, new IpRangeProperty() },
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Nest;
using Tests.Framework;

namespace Tests.Mapping.Types.Core.Range.IpRange
{
public class IpRangeTest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IpAddressRangeTest for consistency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎

{
[IpRange]
public Nest.IpAddressRange Range { get; set; }
}

[SkipVersion("<5.5.0", "ip range type is a new 5.5.0 feature")]
public class IpRangeAttributeTests : AttributeTestsBase<IpRangeTest>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IpAddressRangeAttributeTests for consistency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎

{
protected override object ExpectJson => new
{
properties = new
{
range = new
{
type = "ip_range"
}
}
};
}
}
Loading