Skip to content

Commit c5bdafb

Browse files
authored
Implement URI parts ingest processor (#5304)
1 parent de2174d commit c5bdafb

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

src/Nest/Ingest/ProcessorFormatter.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ internal class ProcessorFormatter : IJsonFormatter<IProcessor>
4646
{ "circle", 30 },
4747
{ "enrich", 31 },
4848
{ "csv", 32 },
49+
{ "uri_parts", 33 }
4950
};
5051

5152
public IProcessor Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
@@ -165,6 +166,9 @@ public IProcessor Deserialize(ref JsonReader reader, IJsonFormatterResolver form
165166
case 32:
166167
processor = Deserialize<CsvProcessor>(ref reader, formatterResolver);
167168
break;
169+
case 33:
170+
processor = Deserialize<UriPartsProcessor>(ref reader, formatterResolver);
171+
break;
168172
}
169173
}
170174
else
@@ -285,6 +289,9 @@ public void Serialize(ref JsonWriter writer, IProcessor value, IJsonFormatterRes
285289
case "circle":
286290
Serialize<ICircleProcessor>(ref writer, value, formatterResolver);
287291
break;
292+
case "uri_parts":
293+
Serialize<IUriPartsProcessor>(ref writer, value, formatterResolver);
294+
break;
288295
default:
289296
var formatter = DynamicObjectResolver.ExcludeNullCamelCase.GetFormatter<IProcessor>();
290297
formatter.Serialize(ref writer, value, formatterResolver);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System;
6+
using System.Linq.Expressions;
7+
using System.Runtime.Serialization;
8+
using Elasticsearch.Net.Utf8Json;
9+
10+
namespace Nest
11+
{
12+
[InterfaceDataContract]
13+
public interface IUriPartsProcessor : IProcessor
14+
{
15+
/// <summary>
16+
/// The field to decode
17+
/// </summary>
18+
[DataMember(Name = "field")]
19+
Field Field { get; set; }
20+
21+
/// <summary>
22+
/// If <c>true</c> the processor copies the unparsed URI to <target_field>.original.
23+
/// </summary>
24+
[DataMember(Name = "keep_original")]
25+
bool? KeepOriginal { get; set; }
26+
27+
/// <summary>
28+
/// The field to assign the converted value to, by default <see cref="Field" /> is updated in-place
29+
/// </summary>
30+
[DataMember(Name = "target_field")]
31+
Field TargetField { get; set; }
32+
33+
/// <summary>
34+
/// If <c>true</c> the processor removes the <see cref="Field" /> after parsing the URI string.
35+
/// If parsing fails, the processor does not remove the <see cref="Field" />.
36+
/// </summary>
37+
[DataMember(Name = "remove_if_successful")]
38+
bool? RemoveIfSuccessful { get; set; }
39+
}
40+
41+
/// <inheritdoc cref="IUriPartsProcessor" />
42+
public class UriPartsProcessor : ProcessorBase, IUriPartsProcessor
43+
{
44+
/// <inheritdoc />
45+
[DataMember(Name = "field")]
46+
public Field Field { get; set; }
47+
48+
/// <inheritdoc />
49+
[DataMember(Name = "keep_original")]
50+
public bool? KeepOriginal { get; set; }
51+
52+
/// <inheritdoc />
53+
[DataMember(Name = "target_field")]
54+
public Field TargetField { get; set; }
55+
56+
/// <inheritdoc />
57+
[DataMember(Name = "remove_if_successful")]
58+
public bool? RemoveIfSuccessful { get; set; }
59+
60+
protected override string Name => "uri_parts";
61+
}
62+
63+
/// <inheritdoc cref="IUriPartsProcessor" />
64+
public class UriPartsProcessorDescriptor<T>
65+
: ProcessorDescriptorBase<UriPartsProcessorDescriptor<T>, IUriPartsProcessor>, IUriPartsProcessor
66+
where T : class
67+
{
68+
protected override string Name => "uri_parts";
69+
70+
Field IUriPartsProcessor.Field { get; set; }
71+
bool? IUriPartsProcessor.KeepOriginal { get; set; }
72+
Field IUriPartsProcessor.TargetField { get; set; }
73+
bool? IUriPartsProcessor.RemoveIfSuccessful { get; set; }
74+
75+
/// <inheritdoc cref="IUriPartsProcessor.Field" />
76+
public UriPartsProcessorDescriptor<T> Field(Field field) => Assign(field, (a, v) => a.Field = v);
77+
78+
/// <inheritdoc cref="IUriPartsProcessor.Field" />
79+
public UriPartsProcessorDescriptor<T> Field<TValue>(Expression<Func<T, TValue>> objectPath) =>
80+
Assign(objectPath, (a, v) => a.Field = v);
81+
82+
/// <inheritdoc cref="IUriPartsProcessor.TargetField" />
83+
public UriPartsProcessorDescriptor<T> TargetField(Field field) => Assign(field, (a, v) => a.TargetField = v);
84+
85+
/// <inheritdoc cref="IUriPartsProcessor.TargetField" />
86+
public UriPartsProcessorDescriptor<T> TargetField<TValue>(Expression<Func<T, TValue>> objectPath) =>
87+
Assign(objectPath, (a, v) => a.TargetField = v);
88+
89+
/// <inheritdoc cref="IUriPartsProcessor.KeepOriginal" />
90+
public UriPartsProcessorDescriptor<T> KeepOriginal(bool? keepOriginal = true) =>
91+
Assign(keepOriginal, (a, v) => a.KeepOriginal = v);
92+
93+
/// <inheritdoc cref="IUriPartsProcessor.RemoveIfSuccessful" />
94+
public UriPartsProcessorDescriptor<T> RemoveIfSuccessful(bool? removeIfSuccessful = true) =>
95+
Assign(removeIfSuccessful, (a, v) => a.RemoveIfSuccessful = v);
96+
}
97+
}

src/Nest/Ingest/ProcessorsDescriptor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,9 @@ public ProcessorsDescriptor Pipeline(Func<PipelineProcessorDescriptor, IPipeline
177177
/// <inheritdoc cref="IPipelineProcessor" />
178178
public ProcessorsDescriptor Circle<T>(Func<CircleProcessorDescriptor<T>, ICircleProcessor> selector) where T : class =>
179179
Assign(selector, (a, v) => a.AddIfNotNull(v?.Invoke(new CircleProcessorDescriptor<T>())));
180+
181+
/// <inheritdoc cref="IUriPartsProcessor"/>
182+
public ProcessorsDescriptor UriParts<T>(Func<UriPartsProcessorDescriptor<T>, IUriPartsProcessor> selector) where T : class =>
183+
Assign(selector, (a, v) => a.AddIfNotNull(v?.Invoke(new UriPartsProcessorDescriptor<T>())));
180184
}
181185
}

tests/Tests/Ingest/ProcessorAssertions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,5 +702,19 @@ public class Pipeline : ProcessorAssertion
702702

703703
public override string Key => "pipeline";
704704
}
705+
706+
[SkipVersion("<7.11.0", "Uses URI parts which was introduced in 7.11.0")]
707+
public class UriParts : ProcessorAssertion
708+
{
709+
public override Func<ProcessorsDescriptor, IPromise<IList<IProcessor>>> Fluent => d => d
710+
.UriParts<Project>(ud => ud
711+
.Field(p => p.Description)
712+
.KeepOriginal()
713+
.RemoveIfSuccessful());
714+
715+
public override IProcessor Initializer => new UriPartsProcessor { Field = "description", KeepOriginal = true, RemoveIfSuccessful = true };
716+
public override object Json => new { field = "description", keep_original = true, remove_if_successful = true };
717+
public override string Key => "uri_parts";
718+
}
705719
}
706720
}

0 commit comments

Comments
 (0)