|
| 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 | +} |
0 commit comments