/* 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.Runtime.Serialization;
using OpenSearch.Net;
using OpenSearch.Net.Utf8Json;
namespace OpenSearch.Client
{
///
/// A Dynamic template that defines custom mappings to be applied
/// to dynamically added fields based on:
///
/// - the datatype detected by OpenSearch, with .
///
/// - the name of the field, with and or .
///
/// - the full dotted path to the field, with and .
///
[InterfaceDataContract]
[ReadAs(typeof(DynamicTemplate))]
public interface IDynamicTemplate
{
///
/// The mapping to apply to matching fields
///
[DataMember(Name ="mapping")]
IProperty Mapping { get; set; }
///
/// A pattern to match on the field name
///
[DataMember(Name ="match")]
string Match { get; set; }
///
/// Matches on the datatype detected by dynamic field mapping,
/// in other words, the datatype that OpenSearch thinks the field should have.
/// Only the following datatypes can be automatically detected: boolean, date, double,
/// long, object, string. It also accepts * to match all datatypes.
///
[DataMember(Name ="match_mapping_type")]
string MatchMappingType { get; set; }
///
/// Adjusts the behavior of such that it supports full
/// Java regular expression matching on the field name instead of simple wildcards
///
[DataMember(Name ="match_pattern")]
MatchType? MatchPattern { get; set; }
///
/// A pattern to match on the field name, which may be the full dotted path
/// to the field name
///
[DataMember(Name ="path_match")]
string PathMatch { get; set; }
///
/// A pattern to exclude fields matched by
///
[DataMember(Name ="path_unmatch")]
string PathUnmatch { get; set; }
///
/// A pattern to exclude fields matched by
///
[DataMember(Name ="unmatch")]
string Unmatch { get; set; }
}
///
public class DynamicTemplate : IDynamicTemplate
{
///
public IProperty Mapping { get; set; }
///
public string Match { get; set; }
///
public string MatchMappingType { get; set; }
///
public MatchType? MatchPattern { get; set; }
///
public string PathMatch { get; set; }
///
public string PathUnmatch { get; set; }
///
public string Unmatch { get; set; }
}
///
public class DynamicTemplateDescriptor : DescriptorBase, IDynamicTemplate>, IDynamicTemplate
where T : class
{
IProperty IDynamicTemplate.Mapping { get; set; }
string IDynamicTemplate.Match { get; set; }
string IDynamicTemplate.MatchMappingType { get; set; }
MatchType? IDynamicTemplate.MatchPattern { get; set; }
string IDynamicTemplate.PathMatch { get; set; }
string IDynamicTemplate.PathUnmatch { get; set; }
string IDynamicTemplate.Unmatch { get; set; }
///
public DynamicTemplateDescriptor Match(string match) => Assign(match, (a, v) => a.Match = v);
///
public DynamicTemplateDescriptor MatchPattern(MatchType? matchPattern) => Assign(matchPattern, (a, v) => a.MatchPattern = v);
///
public DynamicTemplateDescriptor Unmatch(string unMatch) => Assign(unMatch, (a, v) => a.Unmatch = v);
///
public DynamicTemplateDescriptor MatchMappingType(string matchMappingType) => Assign(matchMappingType, (a, v) => a.MatchMappingType = v);
///
public DynamicTemplateDescriptor PathMatch(string pathMatch) => Assign(pathMatch, (a, v) => a.PathMatch = v);
///
public DynamicTemplateDescriptor PathUnmatch(string pathUnmatch) => Assign(pathUnmatch, (a, v) => a.PathUnmatch = v);
///
public DynamicTemplateDescriptor Mapping(Func, IProperty> mappingSelector) =>
Assign(mappingSelector, (a, v) => a.Mapping = v?.Invoke(new SingleMappingSelector()));
}
///
/// Dynamic match pattern type
///
[StringEnum]
public enum MatchType
{
///
/// Simple matching with wildcards
///
[EnumMember(Value = "simple")]
Simple,
///
/// Regular expression matching
///
[EnumMember(Value = "regex")]
Regex
}
}