/* 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.Utf8Json;
namespace OpenSearch.Client
{
///
/// A match query for a single field
///
[InterfaceDataContract]
[JsonFormatter(typeof(FieldNameQueryFormatter))]
public interface IMatchQuery : IFieldNameQuery
{
///
/// The analyzer name used to analyze the query
///
[DataMember(Name = "analyzer")]
string Analyzer { get; set; }
///
[DataMember(Name = "auto_generate_synonyms_phrase_query")]
bool? AutoGenerateSynonymsPhraseQuery { get; set; }
///
/// Allows fuzzy matching based on the type of field being queried.
///
[DataMember(Name = "fuzziness")]
IFuzziness Fuzziness { get; set; }
///
/// Controls how the query is rewritten if is set.
/// In this scenario, the default is .
///
[DataMember(Name = "fuzzy_rewrite")]
MultiTermQueryRewrite FuzzyRewrite { get; set; }
///
/// Sets whether transpositions are supported in fuzzy queries.
///
/// The default metric used by fuzzy queries to determine a match is the Damerau-Levenshtein
/// distance formula which supports transpositions. Setting transposition to false will
/// switch to classic Levenshtein distance.
/// If not set, Damerau-Levenshtein distance metric will be used.
///
[DataMember(Name = "fuzzy_transpositions")]
bool? FuzzyTranspositions { get; set; }
///
/// If set to true will cause format based failures (like providing text to a numeric field)
/// to be ignored
///
[DataMember(Name = "lenient")]
bool? Lenient { get; set; }
///
/// Controls the number of terms fuzzy queries will expand to. Defaults to 50
///
[DataMember(Name = "max_expansions")]
int? MaxExpansions { get; set; }
///
/// A value controlling how many "should" clauses in the resulting boolean query should match.
/// It can be an absolute value, a percentage or a combination of both.
///
[DataMember(Name = "minimum_should_match")]
MinimumShouldMatch MinimumShouldMatch { get; set; }
///
/// The operator used if no explicit operator is specified.
/// The default operator is
///
[DataMember(Name = "operator")]
Operator? Operator { get; set; }
///
/// Set the prefix length for fuzzy queries. Default is 0.
///
[DataMember(Name = "prefix_length")]
int? PrefixLength { get; set; }
///
/// The query to execute
///
[DataMember(Name = "query")]
string Query { get; set; }
///
/// If the analyzer used removes all tokens in a query like a stop filter does, the default behavior is
/// to match no documents at all. In order to change that, can be used,
/// which accepts (default) and
/// which corresponds to a match_all query.
///
[DataMember(Name = "zero_terms_query")]
ZeroTermsQuery? ZeroTermsQuery { get; set; }
}
///
public class MatchQuery : FieldNameQueryBase, IMatchQuery
{
///
public string Analyzer { get; set; }
///
public bool? AutoGenerateSynonymsPhraseQuery { get; set; }
///
public IFuzziness Fuzziness { get; set; }
///
public MultiTermQueryRewrite FuzzyRewrite { get; set; }
///
public bool? FuzzyTranspositions { get; set; }
///
public bool? Lenient { get; set; }
///
public int? MaxExpansions { get; set; }
///
public MinimumShouldMatch MinimumShouldMatch { get; set; }
///
public Operator? Operator { get; set; }
///
public int? PrefixLength { get; set; }
///
public string Query { get; set; }
///
public ZeroTermsQuery? ZeroTermsQuery { get; set; }
protected override bool Conditionless => IsConditionless(this);
internal override void InternalWrapInContainer(IQueryContainer c) => c.Match = this;
internal static bool IsConditionless(IMatchQuery q) => q.Field.IsConditionless() || q.Query.IsNullOrEmpty();
}
///
[DataContract]
public class MatchQueryDescriptor
: FieldNameQueryDescriptorBase, IMatchQuery, T>
, IMatchQuery where T : class
{
protected override bool Conditionless => MatchQuery.IsConditionless(this);
protected virtual string MatchQueryType => null;
string IMatchQuery.Analyzer { get; set; }
bool? IMatchQuery.AutoGenerateSynonymsPhraseQuery { get; set; }
IFuzziness IMatchQuery.Fuzziness { get; set; }
MultiTermQueryRewrite IMatchQuery.FuzzyRewrite { get; set; }
bool? IMatchQuery.FuzzyTranspositions { get; set; }
bool? IMatchQuery.Lenient { get; set; }
int? IMatchQuery.MaxExpansions { get; set; }
MinimumShouldMatch IMatchQuery.MinimumShouldMatch { get; set; }
Operator? IMatchQuery.Operator { get; set; }
int? IMatchQuery.PrefixLength { get; set; }
string IMatchQuery.Query { get; set; }
ZeroTermsQuery? IMatchQuery.ZeroTermsQuery { get; set; }
///
public MatchQueryDescriptor Query(string query) => Assign(query, (a, v) => a.Query = v);
///
public MatchQueryDescriptor Lenient(bool? lenient = true) => Assign(lenient, (a, v) => a.Lenient = v);
///
public MatchQueryDescriptor Analyzer(string analyzer) => Assign(analyzer, (a, v) => a.Analyzer = v);
///
public MatchQueryDescriptor Fuzziness(Fuzziness fuzziness) => Assign(fuzziness, (a, v) => a.Fuzziness = v);
///
public MatchQueryDescriptor FuzzyTranspositions(bool? fuzzyTranspositions = true) =>
Assign(fuzzyTranspositions, (a, v) => a.FuzzyTranspositions = v);
///
public MatchQueryDescriptor FuzzyRewrite(MultiTermQueryRewrite rewrite) => Assign(rewrite, (a, v) => a.FuzzyRewrite = v);
///
public MatchQueryDescriptor MinimumShouldMatch(MinimumShouldMatch minimumShouldMatch) =>
Assign(minimumShouldMatch, (a, v) => a.MinimumShouldMatch = v);
///
public MatchQueryDescriptor Operator(Operator? op) => Assign(op, (a, v) => a.Operator = v);
///
public MatchQueryDescriptor ZeroTermsQuery(ZeroTermsQuery? zeroTermsQuery) => Assign(zeroTermsQuery, (a, v) => a.ZeroTermsQuery = v);
///
public MatchQueryDescriptor PrefixLength(int? prefixLength) => Assign(prefixLength, (a, v) => a.PrefixLength = v);
///
public MatchQueryDescriptor MaxExpansions(int? maxExpansions) => Assign(maxExpansions, (a, v) => a.MaxExpansions = v);
///
public MatchQueryDescriptor AutoGenerateSynonymsPhraseQuery(bool? autoGenerateSynonymsPhraseQuery = true) =>
Assign(autoGenerateSynonymsPhraseQuery, (a, v) => a.AutoGenerateSynonymsPhraseQuery = v);
}
}