/* 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 query that uses the SimpleQueryParser to parse its context.
/// Unlike the regular , the query will
/// never throw an exception, and discards invalid parts of the query.
///
[InterfaceDataContract]
[ReadAs(typeof(SimpleQueryStringQueryDescriptor))]
public interface ISimpleQueryStringQuery : IQuery
{
///
/// The analyzer name used to analyze the query
///
[DataMember(Name = "analyzer")]
string Analyzer { get; set; }
///
/// By default, wildcards terms in a query are not analyzed.
/// By setting this value to true , a best effort will be made to analyze those as well.
///
[DataMember(Name = "analyze_wildcard")]
bool? AnalyzeWildcard { get; set; }
///
[DataMember(Name = "auto_generate_synonyms_phrase_query")]
bool? AutoGenerateSynonymsPhraseQuery { get; set; }
///
/// The default operator used if no explicit operator is specified
/// The default operator is
///
[DataMember(Name = "default_operator")]
Operator? DefaultOperator { get; set; }
///
/// The fields to perform the parsed query against.
/// Defaults to the index.query.default_field index settings, which in turn defaults to * .
/// * extracts all fields in the mapping that are eligible to term queries and filters the metadata fields.
///
[DataMember(Name = "fields")]
Fields Fields { get; set; }
///
/// Flags specifying which features to enable.
/// Defaults to .
///
[DataMember(Name = "flags")]
SimpleQueryStringFlags? Flags { get; set; }
///
/// Controls the number of terms fuzzy queries will expand to. Defaults to 50
///
[DataMember(Name = "fuzzy_max_expansions")]
int? FuzzyMaxExpansions { get; set; }
///
/// Set the prefix length for fuzzy queries. Default is 0
///
[DataMember(Name = "fuzzy_prefix_length")]
int? FuzzyPrefixLength { get; set; }
///
/// Sets whether transpositions are supported in fuzzy queries. Default is true .
///
/// 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; }
///
/// 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 query to be parsed
///
[DataMember(Name = "query")]
string Query { get; set; }
///
/// A suffix to append to fields for quoted parts of the query string.
/// This allows to use a field that has a different analysis chain for exact matching.
///
[DataMember(Name = "quote_field_suffix")]
string QuoteFieldSuffix { get; set; }
}
///
public class SimpleQueryStringQuery : QueryBase, ISimpleQueryStringQuery
{
///
public string Analyzer { get; set; }
///
public bool? AnalyzeWildcard { get; set; }
///
public bool? AutoGenerateSynonymsPhraseQuery { get; set; }
///
public Operator? DefaultOperator { get; set; }
///
public Fields Fields { get; set; }
///
public SimpleQueryStringFlags? Flags { get; set; }
///
public int? FuzzyMaxExpansions { get; set; }
///
public int? FuzzyPrefixLength { get; set; }
///
public bool? FuzzyTranspositions { get; set; }
///
public bool? Lenient { get; set; }
///
public MinimumShouldMatch MinimumShouldMatch { get; set; }
///
public string Query { get; set; }
///
public string QuoteFieldSuffix { get; set; }
protected override bool Conditionless => IsConditionless(this);
internal override void InternalWrapInContainer(IQueryContainer c) => c.SimpleQueryString = this;
internal static bool IsConditionless(ISimpleQueryStringQuery q) => q.Query.IsNullOrEmpty();
}
///
public class SimpleQueryStringQueryDescriptor
: QueryDescriptorBase, ISimpleQueryStringQuery>
, ISimpleQueryStringQuery where T : class
{
protected override bool Conditionless => SimpleQueryStringQuery.IsConditionless(this);
string ISimpleQueryStringQuery.Analyzer { get; set; }
bool? ISimpleQueryStringQuery.AnalyzeWildcard { get; set; }
bool? ISimpleQueryStringQuery.AutoGenerateSynonymsPhraseQuery { get; set; }
Operator? ISimpleQueryStringQuery.DefaultOperator { get; set; }
Fields ISimpleQueryStringQuery.Fields { get; set; }
SimpleQueryStringFlags? ISimpleQueryStringQuery.Flags { get; set; }
int? ISimpleQueryStringQuery.FuzzyMaxExpansions { get; set; }
int? ISimpleQueryStringQuery.FuzzyPrefixLength { get; set; }
bool? ISimpleQueryStringQuery.FuzzyTranspositions { get; set; }
bool? ISimpleQueryStringQuery.Lenient { get; set; }
MinimumShouldMatch ISimpleQueryStringQuery.MinimumShouldMatch { get; set; }
string ISimpleQueryStringQuery.Query { get; set; }
string ISimpleQueryStringQuery.QuoteFieldSuffix { get; set; }
///
public SimpleQueryStringQueryDescriptor Fields(Func, IPromise> fields) =>
Assign(fields, (a, v) => a.Fields = v?.Invoke(new FieldsDescriptor())?.Value);
///
public SimpleQueryStringQueryDescriptor Fields(Fields fields) => Assign(fields, (a, v) => a.Fields = v);
///
public SimpleQueryStringQueryDescriptor Query(string query) => Assign(query, (a, v) => a.Query = v);
///
public SimpleQueryStringQueryDescriptor Analyzer(string analyzer) => Assign(analyzer, (a, v) => a.Analyzer = v);
///
public SimpleQueryStringQueryDescriptor DefaultOperator(Operator? op) => Assign(op, (a, v) => a.DefaultOperator = v);
///
public SimpleQueryStringQueryDescriptor Flags(SimpleQueryStringFlags? flags) => Assign(flags, (a, v) => a.Flags = v);
///
public SimpleQueryStringQueryDescriptor AnalyzeWildcard(bool? analyzeWildcard = true) =>
Assign(analyzeWildcard, (a, v) => a.AnalyzeWildcard = v);
///
public SimpleQueryStringQueryDescriptor Lenient(bool? lenient = true) => Assign(lenient, (a, v) => a.Lenient = v);
///
public SimpleQueryStringQueryDescriptor MinimumShouldMatch(MinimumShouldMatch minimumShouldMatch) =>
Assign(minimumShouldMatch, (a, v) => a.MinimumShouldMatch = v);
///
public SimpleQueryStringQueryDescriptor QuoteFieldSuffix(string quoteFieldSuffix) =>
Assign(quoteFieldSuffix, (a, v) => a.QuoteFieldSuffix = v);
///
public SimpleQueryStringQueryDescriptor FuzzyPrefixLength(int? fuzzyPrefixLength) => Assign(fuzzyPrefixLength, (a, v) => a.FuzzyPrefixLength = v);
///
public SimpleQueryStringQueryDescriptor FuzzyMaxExpansions(int? fuzzyMaxExpansions) =>
Assign(fuzzyMaxExpansions, (a, v) => a.FuzzyMaxExpansions = v);
///
public SimpleQueryStringQueryDescriptor FuzzyTranspositions(bool? fuzzyTranspositions = true) =>
Assign(fuzzyTranspositions, (a, v) => a.FuzzyTranspositions = v);
///
public SimpleQueryStringQueryDescriptor AutoGenerateSynonymsPhraseQuery(bool? autoGenerateSynonymsPhraseQuery = true) =>
Assign(autoGenerateSynonymsPhraseQuery, (a, v) => a.AutoGenerateSynonymsPhraseQuery = v);
}
}