/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using OpenSearch.Net;
using OpenSearch.Net.Utf8Json;
namespace OpenSearch.Client.Specification.IngestApi
{
///
/// The purpose of this processor is to point documents to the right time
/// based index based on a date or timestamp field in a document
/// by using the date math index name support.
///
[InterfaceDataContract]
public interface IDateIndexNameProcessor : IProcessor
{
///
/// An array of the expected date formats for parsing
/// dates / timestamps in the document being preprocessed.
/// Default is yyyy-MM-dd'T'HH:mm:ss.SSSZ
///
[DataMember(Name = "date_formats")]
IEnumerable DateFormats { get; set; }
///
/// How to round the date when formatting the date into the index name.
///
[DataMember(Name = "date_rounding")]
DateRounding? DateRounding { get; set; }
///
/// The field to get the date or timestamp from.
///
[DataMember(Name = "field")]
Field Field { get; set; }
///
/// The format to be used when printing the parsed date into
/// the index name.
///
[DataMember(Name = "index_name_format")]
string IndexNameFormat { get; set; }
///
/// A prefix of the index name to be prepended before the printed date.
///
[DataMember(Name = "index_name_prefix")]
string IndexNamePrefix { get; set; }
///
/// The locale to use when parsing the date from the document
/// being preprocessed, relevant when parsing month names or
/// week days.
///
[DataMember(Name = "locale")]
string Locale { get; set; }
///
/// The timezone to use when parsing the date and when date
/// math index supports resolves expressions into concrete
/// index names.
///
[DataMember(Name = "timezone")]
string TimeZone { get; set; }
}
public class DateIndexNameProcessor : ProcessorBase, IDateIndexNameProcessor
{
///
/// An array of the expected date formats for parsing
/// dates / timestamps in the document being preprocessed.
/// Default is yyyy-MM-dd'T'HH:mm:ss.SSSZ
///
public IEnumerable DateFormats { get; set; }
///
/// How to round the date when formatting the date into the index name.
///
public DateRounding? DateRounding { get; set; }
///
/// The field to get the date or timestamp from.
///
public Field Field { get; set; }
///
/// The format to be used when printing the parsed date into
/// the index name.
///
public string IndexNameFormat { get; set; }
///
/// A prefix of the index name to be prepended before the printed date.
///
public string IndexNamePrefix { get; set; }
///
/// The locale to use when parsing the date from the document
/// being preprocessed, relevant when parsing month names or
/// week days.
///
public string Locale { get; set; }
///
/// The timezone to use when parsing the date and when date
/// math index supports resolves expressions into concrete
/// index names.
///
public string TimeZone { get; set; }
protected override string Name => "date_index_name";
}
public class DateIndexNameProcessorDescriptor
: ProcessorDescriptorBase, IDateIndexNameProcessor>, IDateIndexNameProcessor
where T : class
{
protected override string Name => "date_index_name";
IEnumerable IDateIndexNameProcessor.DateFormats { get; set; }
DateRounding? IDateIndexNameProcessor.DateRounding { get; set; }
Field IDateIndexNameProcessor.Field { get; set; }
string IDateIndexNameProcessor.IndexNameFormat { get; set; }
string IDateIndexNameProcessor.IndexNamePrefix { get; set; }
string IDateIndexNameProcessor.Locale { get; set; }
string IDateIndexNameProcessor.TimeZone { get; set; }
///
/// The field to get the date or timestamp from.
///
public DateIndexNameProcessorDescriptor Field(Field field) => Assign(field, (a, v) => a.Field = v);
///
/// The field to get the date or timestamp from.
///
public DateIndexNameProcessorDescriptor Field(Expression> objectPath) =>
Assign(objectPath, (a, v) => a.Field = v);
///
/// A prefix of the index name to be prepended before the printed date.
///
public DateIndexNameProcessorDescriptor IndexNamePrefix(string indexNamePrefix) =>
Assign(indexNamePrefix, (a, v) => a.IndexNamePrefix = v);
///
/// How to round the date when formatting the date into the index name.
///
public DateIndexNameProcessorDescriptor DateRounding(DateRounding? dateRounding) =>
Assign(dateRounding, (a, v) => a.DateRounding = v);
///
/// An array of the expected date formats for parsing
/// dates / timestamps in the document being preprocessed.
/// Default is yyyy-MM-dd'T'HH:mm:ss.SSSZ
///
public DateIndexNameProcessorDescriptor DateFormats(IEnumerable dateFormats) =>
Assign(dateFormats, (a, v) => a.DateFormats = v);
///
/// An array of the expected date formats for parsing
/// dates / timestamps in the document being preprocessed.
/// Default is yyyy-MM-dd'T'HH:mm:ss.SSSZ
///
public DateIndexNameProcessorDescriptor DateFormats(params string[] dateFormats) =>
Assign(dateFormats, (a, v) => a.DateFormats = v);
///
/// The timezone to use when parsing the date and when date
/// math index supports resolves expressions into concrete
/// index names.
///
public DateIndexNameProcessorDescriptor TimeZone(string timeZone) =>
Assign(timeZone, (a, v) => a.TimeZone = v);
///
/// The locale to use when parsing the date from the document
/// being preprocessed, relevant when parsing month names or
/// week days.
///
public DateIndexNameProcessorDescriptor Locale(string locale) =>
Assign(locale, (a, v) => a.Locale = v);
///
/// The format to be used when printing the parsed date into
/// the index name.
///
public DateIndexNameProcessorDescriptor IndexNameFormat(string indexNameFormat) =>
Assign(indexNameFormat, (a, v) => a.IndexNameFormat = v);
}
[StringEnum]
public enum DateRounding
{
[EnumMember(Value = "s")]
Second,
[EnumMember(Value = "m")]
Minute,
[EnumMember(Value = "h")]
Hour,
[EnumMember(Value = "d")]
Day,
[EnumMember(Value = "w")]
Week,
[EnumMember(Value = "M")]
Month,
[EnumMember(Value = "y")]
Year
}
}