Skip to content

Commit 78e178c

Browse files
committed
Add Date Index Name Ingest Processor
See elastic/elasticsearch#17973
1 parent 21b07bf commit 78e178c

File tree

5 files changed

+159
-1
lines changed

5 files changed

+159
-1
lines changed

src/Nest/Ingest/PipelineJsonConverter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ private List<IProcessor> GetProcessors(JToken jsonProcessors, JsonSerializer ser
4646
case "date":
4747
processors.Add(jsonProcessor.ToObject<DateProcessor>(serializer));
4848
break;
49+
case "date_index_name":
50+
processors.Add(jsonProcessor.ToObject<DateIndexNameProcessor>(serializer));
51+
break;
4952
case "fail":
5053
processors.Add(jsonProcessor.ToObject<FailProcessor>(serializer));
5154
break;
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
using System.Runtime.Serialization;
5+
using Newtonsoft.Json;
6+
using Newtonsoft.Json.Converters;
7+
8+
namespace Nest
9+
{
10+
/// <summary>
11+
/// The purpose of this processor is to point documents to the right time
12+
/// based index based on a date or timestamp field in a document
13+
/// by using the date math index name support.
14+
/// </summary>
15+
[JsonObject(MemberSerialization.OptIn)]
16+
[JsonConverter(typeof(ProcessorJsonConverter<DateIndexNameProcessor>))]
17+
public interface IDateIndexNameProcessor : IProcessor
18+
{
19+
/// <summary>
20+
/// The field to get the date or timestamp from.
21+
/// </summary>
22+
[JsonProperty("field")]
23+
Field Field { get; set; }
24+
25+
/// <summary>
26+
/// A prefix of the index name to be prepended before the printed date.
27+
/// </summary>
28+
[JsonProperty("index_name_prefix")]
29+
string IndexNamePrefix { get; set; }
30+
31+
/// <summary>
32+
/// How to round the date when formatting the date into the index name.
33+
/// </summary>
34+
[JsonProperty("date_rounding")]
35+
DateRounding DateRounding { get; set; }
36+
37+
/// <summary>
38+
/// An array of the expected date formats for parsing
39+
/// dates / timestamps in the document being preprocessed.
40+
/// Default is yyyy-MM-dd’T'HH:mm:ss.SSSZ
41+
/// </summary>
42+
[JsonProperty("date_formats")]
43+
IEnumerable<string> DateFormats { get; set; }
44+
45+
/// <summary>
46+
/// The timezone to use when parsing the date and when date
47+
/// math index supports resolves expressions into concrete
48+
/// index names.
49+
/// </summary>
50+
[JsonProperty("timezone")]
51+
string TimeZone { get; set; }
52+
53+
/// <summary>
54+
/// The locale to use when parsing the date from the document
55+
/// being preprocessed, relevant when parsing month names or
56+
/// week days.
57+
/// </summary>
58+
[JsonProperty("locale")]
59+
string Locale { get; set; }
60+
61+
/// <summary>
62+
/// The format to be used when printing the parsed date into
63+
/// the index name.
64+
/// </summary>
65+
[JsonProperty("index_name_format")]
66+
string IndexNameFormat { get; set; }
67+
}
68+
69+
public class DateIndexNameProcessor : ProcessorBase, IDateIndexNameProcessor
70+
{
71+
protected override string Name => "date_index_name";
72+
73+
public Field Field { get; set; }
74+
75+
public string IndexNamePrefix { get; set; }
76+
77+
public DateRounding DateRounding { get; set; }
78+
79+
public IEnumerable<string> DateFormats { get; set; }
80+
81+
public string TimeZone { get; set; }
82+
83+
public string Locale { get; set; }
84+
85+
public string IndexNameFormat { get; set; }
86+
}
87+
88+
public class DateIndexNameProcessorDescriptor<T>
89+
: ProcessorDescriptorBase<DateIndexNameProcessorDescriptor<T>, IDateIndexNameProcessor>, IDateIndexNameProcessor
90+
where T : class
91+
{
92+
protected override string Name => "date_index_name";
93+
94+
Field IDateIndexNameProcessor.Field { get; set; }
95+
string IDateIndexNameProcessor.IndexNamePrefix { get; set; }
96+
DateRounding IDateIndexNameProcessor.DateRounding { get; set; }
97+
IEnumerable<string> IDateIndexNameProcessor.DateFormats { get; set; }
98+
string IDateIndexNameProcessor.TimeZone { get; set; }
99+
string IDateIndexNameProcessor.Locale { get; set; }
100+
string IDateIndexNameProcessor.IndexNameFormat { get; set; }
101+
102+
public DateIndexNameProcessorDescriptor<T> Field(Field field) => Assign(a => a.Field = field);
103+
104+
public DateIndexNameProcessorDescriptor<T> Field(Expression<Func<T, object>> objectPath) =>
105+
Assign(a => a.Field = objectPath);
106+
107+
public DateIndexNameProcessorDescriptor<T> IndexNamePrefix(string indexNamePrefix) =>
108+
Assign(a => a.IndexNamePrefix = indexNamePrefix);
109+
110+
public DateIndexNameProcessorDescriptor<T> DateRounding(DateRounding dateRounding) =>
111+
Assign(a => a.DateRounding = dateRounding);
112+
113+
public DateIndexNameProcessorDescriptor<T> DateFormats(IEnumerable<string> dateFormats) =>
114+
Assign(a => a.DateFormats = dateFormats);
115+
116+
public DateIndexNameProcessorDescriptor<T> DateFormats(params string[] dateFormats) =>
117+
Assign(a => a.DateFormats = dateFormats);
118+
119+
public DateIndexNameProcessorDescriptor<T> TimeZone(string timeZone) =>
120+
Assign(a => a.TimeZone = timeZone);
121+
122+
public DateIndexNameProcessorDescriptor<T> Locale(string locale) =>
123+
Assign(a => a.Locale = locale);
124+
125+
public DateIndexNameProcessorDescriptor<T> IndexNameFormat(string indexNameFormat) =>
126+
Assign(a => a.IndexNameFormat = indexNameFormat);
127+
}
128+
129+
[JsonConverter(typeof(StringEnumConverter))]
130+
public enum DateRounding
131+
{
132+
[EnumMember(Value = "s")]
133+
Second,
134+
[EnumMember(Value = "m")]
135+
Minute,
136+
[EnumMember(Value = "h")]
137+
Hour,
138+
[EnumMember(Value = "d")]
139+
Day,
140+
[EnumMember(Value = "w")]
141+
Week,
142+
[EnumMember(Value = "M")]
143+
Month,
144+
[EnumMember(Value = "y")]
145+
Year
146+
}
147+
}

src/Nest/Ingest/Processors/SortProcessor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq.Expressions;
43
using Newtonsoft.Json;
54

src/Nest/Ingest/ProcessorsDescriptor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ public ProcessorsDescriptor Convert<T>(Func<ConvertProcessorDescriptor<T>, IConv
2626
public ProcessorsDescriptor Date<T>(Func<DateProcessorDescriptor<T>, IDateProcessor> selector) where T : class =>
2727
Assign(a => a.AddIfNotNull(selector?.Invoke(new DateProcessorDescriptor<T>())));
2828

29+
/// <summary>
30+
/// The purpose of this processor is to point documents to the right time
31+
/// based index based on a date or timestamp field in a document
32+
/// by using the date math index name support.
33+
/// </summary>
34+
public ProcessorsDescriptor DateIndexName<T>(Func<DateIndexNameProcessorDescriptor<T>, IDateIndexNameProcessor> selector) where T : class =>
35+
Assign(a => a.AddIfNotNull(selector?.Invoke(new DateIndexNameProcessorDescriptor<T>())));
36+
2937
public ProcessorsDescriptor Fail(Func<FailProcessorDescriptor, IFailProcessor> selector) =>
3038
Assign(a => a.AddIfNotNull(selector?.Invoke(new FailProcessorDescriptor())));
3139

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@
765765
<Compile Include="Indices\StatusManagement\Upgrade\UpgradeStatus\UpgradeStatusResponseJsonConverter.cs" />
766766
<Compile Include="Ingest\Processors\GeoIpProcessor.cs" />
767767
<Compile Include="Ingest\Processors\AttachmentProcessor.cs" />
768+
<Compile Include="Ingest\Processors\DateIndexNameProcessor.cs" />
768769
<Compile Include="Ingest\Processors\SortProcessor.cs" />
769770
<Compile Include="Mapping\AttributeBased\ElasticsearchCorePropertyAttributeBase.cs" />
770771
<Compile Include="Ingest\DeletePipeline\DeletePipelineRequest.cs" />

0 commit comments

Comments
 (0)