Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Circle processor #4161

Merged
merged 3 commits into from
Oct 21, 2019
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
7 changes: 7 additions & 0 deletions src/Nest/Ingest/ProcessorFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ internal class ProcessorFormatter : IJsonFormatter<IProcessor>
{ "set_security_user", 27 },
{ "pipeline", 28 },
{ "drop", 29 },
{ "circle", 30 },
};

public IProcessor Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
Expand Down Expand Up @@ -149,6 +150,9 @@ public IProcessor Deserialize(ref JsonReader reader, IJsonFormatterResolver form
case 29:
processor = Deserialize<DropProcessor>(ref reader, formatterResolver);
break;
case 30:
processor = Deserialize<CircleProcessor>(ref reader, formatterResolver);
break;
}
}
else
Expand Down Expand Up @@ -260,6 +264,9 @@ public void Serialize(ref JsonWriter writer, IProcessor value, IJsonFormatterRes
case "drop":
Serialize<IDropProcessor>(ref writer, value, formatterResolver);
break;
case "circle":
Serialize<ICircleProcessor>(ref writer, value, formatterResolver);
break;
default:
var formatter = DynamicObjectResolver.ExcludeNullCamelCase.GetFormatter<IProcessor>();
formatter.Serialize(ref writer, value, formatterResolver);
Expand Down
114 changes: 114 additions & 0 deletions src/Nest/Ingest/Processors/CircleProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using Elasticsearch.Net;
using Elasticsearch.Net.Utf8Json;

namespace Nest
{
[StringEnum]
public enum ShapeType
{
[EnumMember(Value = "geo_shape")]
GeoShape,

[EnumMember(Value = "shape")]
Shape
}

[InterfaceDataContract]
public interface ICircleProcessor : IProcessor
{
/// <summary>
/// The string-valued field to trim whitespace from.
/// </summary>
[DataMember(Name ="field")]
Field Field { get; set; }

/// <summary>
/// The field to assign the polygon shape to, by default field is updated in-place.
/// </summary>
[DataMember(Name ="target_field")]
Field TargetField { get; set; }

/// <summary>
/// If true and field does not exist, the processor quietly exits without modifying the document.
/// </summary>
[DataMember(Name = "ignore_missing")]
bool? IgnoreMissing { get; set; }

/// <summary>
/// The difference between the resulting inscribed distance from center to side and the circle’s radius
/// (measured in meters for geo_shape, unit-less for shape)
/// </summary>
[DataMember(Name = "error_distance")]
double? ErrorDistance { get; set; }

/// <summary>
/// Which field mapping type is to be used when processing the circle.
/// </summary>
[DataMember(Name = "shape_type")]
ShapeType? ShapeType { get; set; }
}

public class CircleProcessor : ProcessorBase, ICircleProcessor
{
/// <inheritdoc />
public Field Field { get; set; }

/// <inheritdoc />
public Field TargetField { get; set; }

/// <inheritdoc />
public bool? IgnoreMissing { get; set; }

/// <inheritdoc />
public double? ErrorDistance { get; set; }

/// <inheritdoc />
public ShapeType? ShapeType { get; set; }

/// <inheritdoc />
protected override string Name => "circle";
}

public class CircleProcessorDescriptor<T>
: ProcessorDescriptorBase<CircleProcessorDescriptor<T>, ICircleProcessor>, ICircleProcessor
where T : class
{
protected override string Name => "circle";

Field ICircleProcessor.Field { get; set; }
Field ICircleProcessor.TargetField { get; set; }
bool? ICircleProcessor.IgnoreMissing { get; set; }
double? ICircleProcessor.ErrorDistance { get; set; }
ShapeType? ICircleProcessor.ShapeType { get; set; }

/// <inheritdoc cref="ICircleProcessor.Field" />
public CircleProcessorDescriptor<T> Field(Field field) => Assign(field, (a, v) => a.Field = v);

/// <inheritdoc cref="ICircleProcessor.Field" />
public CircleProcessorDescriptor<T> Field<TValue>(Expression<Func<T, TValue>> objectPath) =>
Assign(objectPath, (a, v) => a.Field = v);

/// <inheritdoc cref="ICircleProcessor.TargetField" />
public CircleProcessorDescriptor<T> TargetField(Field field) => Assign(field, (a, v) => a.TargetField = v);

/// <inheritdoc cref="ICircleProcessor.TargetField" />
public CircleProcessorDescriptor<T> TargetField<TValue>(Expression<Func<T, TValue>> objectPath) =>
Assign(objectPath, (a, v) => a.TargetField = v);

/// <inheritdoc cref="ICircleProcessor.IgnoreMissing">
public CircleProcessorDescriptor<T> IgnoreMissing(bool? ignoreMissing = true) =>
Assign(ignoreMissing, (a, v) => a.IgnoreMissing = v);

/// <inheritdoc cref="ICircleProcessor.IgnoreMissing">
public CircleProcessorDescriptor<T> ErrorDistance(double? errorDistance) =>
Assign(errorDistance, (a, v) => a.ErrorDistance = v);

/// <inheritdoc cref="ICircleProcessor.ShapeType">
public CircleProcessorDescriptor<T> ShapeType(ShapeType? shapeType) =>
Assign(shapeType, (a, v) => a.ShapeType = v);
}
}
1 change: 1 addition & 0 deletions src/Nest/Ingest/Processors/DissectProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public DissectProcessorDescriptor<T> Field<TValue>(Expression<Func<T, TValue>> o
public DissectProcessorDescriptor<T> Pattern(string pattern) =>
Assign(pattern, (a, v) => a.Pattern = v);

// TODO! rename the parameter in 8.0
russcam marked this conversation as resolved.
Show resolved Hide resolved
/// <inheritdoc cref="IDissectProcessor.IgnoreMissing">
public DissectProcessorDescriptor<T> IgnoreMissing(bool? traceMatch = true) =>
Assign(traceMatch, (a, v) => a.IgnoreMissing = v);
Expand Down
3 changes: 3 additions & 0 deletions src/Nest/Ingest/ProcessorsDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,8 @@ public ProcessorsDescriptor SetSecurityUser<T>(Func<SetSecurityUserProcessorDesc
public ProcessorsDescriptor Pipeline(Func<PipelineProcessorDescriptor, IPipelineProcessor> selector) =>
Assign(selector, (a, v) => a.AddIfNotNull(v?.Invoke(new PipelineProcessorDescriptor())));

/// <inheritdoc cref="IPipelineProcessor" />
public ProcessorsDescriptor Circle<T>(Func<CircleProcessorDescriptor<T>, ICircleProcessor> selector) where T : class =>
Assign(selector, (a, v) => a.AddIfNotNull(v?.Invoke(new CircleProcessorDescriptor<T>())));
}
}
32 changes: 32 additions & 0 deletions src/Tests/Tests/Ingest/ProcessorAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,38 @@ public class Attachment : ProcessorAssertion
public override string Key => "attachment";
}

[SkipVersion("<7.4.0", "Circle processor added in 7.4.0")]
public class Circle : ProcessorAssertion
{
public override Func<ProcessorsDescriptor, IPromise<IList<IProcessor>>> Fluent => d => d
.Circle<Project>(ud => ud
.Field(p => p.Description)
.TargetField(p => p.ArbitraryShape)
.ShapeType(ShapeType.Shape)
.ErrorDistance(10d)
.IgnoreMissing()
);

public override IProcessor Initializer => new CircleProcessor
{
Field = "description",
TargetField = "arbitraryShape",
ShapeType = ShapeType.Shape,
ErrorDistance = 10d,
IgnoreMissing = true
};

public override object Json => new
{
field = "description",
target_field = "arbitraryShape",
shape_type = "shape",
error_distance = 10.0,
ignore_missing = true,
};

public override string Key => "circle";
}

[SkipVersion("<6.4.0", "")]
public class Bytes : ProcessorAssertion
Expand Down