/* 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.Runtime.Serialization;
using OpenSearch.Net.Utf8Json;
namespace OpenSearch.Client
{
///
/// Queries documents that contain terms matching a regular expression.
///
[InterfaceDataContract]
[JsonFormatter(typeof(FieldNameQueryFormatter))]
public interface IRegexpQuery : IFieldNameQuery
{
///
/// Enables optional operators for the regular expression.
///
[DataMember(Name = "flags")]
string Flags { get; set; }
///
/// Maximum number of automaton states required for the query. Default is 10000.
///
/// OpenSearch uses Apache Lucene internally to parse regular expressions.
/// Lucene converts each regular expression to a finite automaton containing a number of determinized states.
///
/// You can use this parameter to prevent that conversion from unintentionally consuming too
/// many resources. You may need to increase this limit to run complex regular expressions.
///
[DataMember(Name = "max_determinized_states")]
int? MaximumDeterminizedStates { get; set; }
///
/// Regular expression for terms you wish to find in the provided field
///
[DataMember(Name = "value")]
string Value { get; set; }
///
/// Method used to rewrite the query.
///
[DataMember(Name ="rewrite")]
MultiTermQueryRewrite Rewrite { get; set; }
}
///
public class RegexpQuery : FieldNameQueryBase, IRegexpQuery
{
///
public string Flags { get; set; }
///
public int? MaximumDeterminizedStates { get; set; }
///
public string Value { get; set; }
///
public MultiTermQueryRewrite Rewrite { get; set; }
protected override bool Conditionless => IsConditionless(this);
internal override void InternalWrapInContainer(IQueryContainer c) => c.Regexp = this;
internal static bool IsConditionless(IRegexpQuery q) => q.Field.IsConditionless() || q.Value.IsNullOrEmpty();
}
///
public class RegexpQueryDescriptor
: FieldNameQueryDescriptorBase, IRegexpQuery, T>
, IRegexpQuery where T : class
{
protected override bool Conditionless => RegexpQuery.IsConditionless(this);
string IRegexpQuery.Flags { get; set; }
int? IRegexpQuery.MaximumDeterminizedStates { get; set; }
string IRegexpQuery.Value { get; set; }
MultiTermQueryRewrite IRegexpQuery.Rewrite { get; set; }
///
public RegexpQueryDescriptor MaximumDeterminizedStates(int? maxDeterminizedStates) =>
Assign(maxDeterminizedStates, (a, v) => a.MaximumDeterminizedStates = v);
///
public RegexpQueryDescriptor Value(string regex) => Assign(regex, (a, v) => a.Value = v);
///
public RegexpQueryDescriptor Flags(string flags) => Assign(flags, (a, v) => a.Flags = v);
///
public RegexpQueryDescriptor Rewrite(MultiTermQueryRewrite rewrite) =>
Assign(rewrite, (a, v) => a.Rewrite = v);
}
}