/* 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.Linq.Expressions;
using System.Runtime.Serialization;
using OpenSearch.Net.Utf8Json;
namespace OpenSearch.Client
{
///
/// A query that uses a query parser in order to parse its content
///
[InterfaceDataContract]
[ReadAs(typeof(QueryStringQuery))]
public interface IQueryStringQuery : IQuery
{
///
/// When set, * or ? are allowed as the first character. Defaults to true.
///
[DataMember(Name = "allow_leading_wildcard")]
bool? AllowLeadingWildcard { get; set; }
///
/// 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 field for query terms if no prefix field is specified.
/// 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.
/// All extracted fields are then combined to build a query when no prefix field is provided.
///
[DataMember(Name = "default_field")]
Field DefaultField { get; set; }
///
/// The default operator used if no explicit operator is specified.
/// The default operator is
///
[DataMember(Name = "default_operator")]
Operator? DefaultOperator { get; set; }
///
/// Set to true to enable position increments in result queries. Defaults to true.
///
[DataMember(Name = "enable_position_increments")]
bool? EnablePositionIncrements { get; set; }
///
/// Enables escaping of the query
///
[DataMember(Name = "escape")]
bool? Escape { 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; }
///
/// Set the fuzziness for fuzzy queries. Defaults to
///
[DataMember(Name = "fuzziness")]
Fuzziness Fuzziness { 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; }
///
/// 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; }
///
/// Limit on how many automaton states regexp queries are allowed to create.
/// This protects against too-difficult (e.g. exponentially hard) regexps.
/// Defaults to 10000.
///
[DataMember(Name = "max_determinized_states")]
int? MaximumDeterminizedStates { 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; }
///
/// Sets the default slop for phrases. If zero, then exact phrase matches are required.
/// Default value is 0.
///
[DataMember(Name = "phrase_slop")]
double? PhraseSlop { get; set; }
///
/// The query to be parsed
///
[DataMember(Name = "query")]
string Query { get; set; }
///
/// The name of the analyzer that is used to analyze quoted phrases in the query string.
/// For those parts, it overrides other analyzers that are set using the analyzer parameter
/// or the search_quote_analyzer setting.
///
[DataMember(Name = "quote_analyzer")]
string QuoteAnalyzer { 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; }
///
/// Controls how a multi term query such as a wildcard or prefix query, is rewritten.
///
[DataMember(Name = "rewrite")]
MultiTermQueryRewrite Rewrite { get; set; }
///
/// The disjunction max tie breaker for multi fields. Defaults to 0
///
[DataMember(Name = "tie_breaker")]
double? TieBreaker { get; set; }
///
/// Time Zone to be applied to any range query related to dates.
///
[DataMember(Name = "time_zone")]
string TimeZone { get; set; }
///
/// How the fields should be combined to build the text query.
/// Default is
///
[DataMember(Name = "type")]
TextQueryType? Type { get; set; }
}
///
[DataContract]
public class QueryStringQuery : QueryBase, IQueryStringQuery
{
///
public bool? AllowLeadingWildcard { get; set; }
///
public string Analyzer { get; set; }
///
public bool? AnalyzeWildcard { get; set; }
///
public bool? AutoGenerateSynonymsPhraseQuery { get; set; }
///
public Field DefaultField { get; set; }
///
public Operator? DefaultOperator { get; set; }
///
public bool? EnablePositionIncrements { get; set; }
///
public bool? Escape { get; set; }
///
public Fields Fields { get; set; }
///
public Fuzziness Fuzziness { get; set; }
///
public int? FuzzyMaxExpansions { get; set; }
///
public int? FuzzyPrefixLength { get; set; }
///
public MultiTermQueryRewrite FuzzyRewrite { get; set; }
///
public bool? FuzzyTranspositions { get; set; }
///
public bool? Lenient { get; set; }
///
public int? MaximumDeterminizedStates { get; set; }
///
public MinimumShouldMatch MinimumShouldMatch { get; set; }
///
public double? PhraseSlop { get; set; }
///
public string Query { get; set; }
///
public string QuoteAnalyzer { get; set; }
///
public string QuoteFieldSuffix { get; set; }
///
public MultiTermQueryRewrite Rewrite { get; set; }
///
public double? TieBreaker { get; set; }
///
public string TimeZone { get; set; }
///
public TextQueryType? Type { get; set; }
protected override bool Conditionless => IsConditionless(this);
internal override void InternalWrapInContainer(IQueryContainer c) => c.QueryString = this;
internal static bool IsConditionless(IQueryStringQuery q) => q.Query.IsNullOrEmpty();
}
///
[DataContract]
public class QueryStringQueryDescriptor
: QueryDescriptorBase, IQueryStringQuery>
, IQueryStringQuery where T : class
{
protected override bool Conditionless => QueryStringQuery.IsConditionless(this);
bool? IQueryStringQuery.AllowLeadingWildcard { get; set; }
string IQueryStringQuery.Analyzer { get; set; }
bool? IQueryStringQuery.AnalyzeWildcard { get; set; }
bool? IQueryStringQuery.AutoGenerateSynonymsPhraseQuery { get; set; }
Field IQueryStringQuery.DefaultField { get; set; }
Operator? IQueryStringQuery.DefaultOperator { get; set; }
bool? IQueryStringQuery.EnablePositionIncrements { get; set; }
bool? IQueryStringQuery.Escape { get; set; }
Fields IQueryStringQuery.Fields { get; set; }
Fuzziness IQueryStringQuery.Fuzziness { get; set; }
int? IQueryStringQuery.FuzzyMaxExpansions { get; set; }
int? IQueryStringQuery.FuzzyPrefixLength { get; set; }
MultiTermQueryRewrite IQueryStringQuery.FuzzyRewrite { get; set; }
bool? IQueryStringQuery.FuzzyTranspositions { get; set; }
bool? IQueryStringQuery.Lenient { get; set; }
int? IQueryStringQuery.MaximumDeterminizedStates { get; set; }
MinimumShouldMatch IQueryStringQuery.MinimumShouldMatch { get; set; }
double? IQueryStringQuery.PhraseSlop { get; set; }
string IQueryStringQuery.Query { get; set; }
string IQueryStringQuery.QuoteAnalyzer { get; set; }
string IQueryStringQuery.QuoteFieldSuffix { get; set; }
MultiTermQueryRewrite IQueryStringQuery.Rewrite { get; set; }
double? IQueryStringQuery.TieBreaker { get; set; }
string IQueryStringQuery.TimeZone { get; set; }
TextQueryType? IQueryStringQuery.Type { get; set; }
///
public QueryStringQueryDescriptor DefaultField(Field field) => Assign(field, (a, v) => a.DefaultField = v);
///
public QueryStringQueryDescriptor DefaultField(Expression> field) => Assign(field, (a, v) => a.DefaultField = v);
///
public QueryStringQueryDescriptor Fields(Func, IPromise> fields) =>
Assign(fields, (a, v) => a.Fields = v?.Invoke(new FieldsDescriptor())?.Value);
///
public QueryStringQueryDescriptor Fields(Fields fields) => Assign(fields, (a, v) => a.Fields = v);
///
public QueryStringQueryDescriptor Type(TextQueryType? type) => Assign(type, (a, v) => a.Type = v);
///
public QueryStringQueryDescriptor Query(string query) => Assign(query, (a, v) => a.Query = v);
///
public QueryStringQueryDescriptor DefaultOperator(Operator? op) => Assign(op, (a, v) => a.DefaultOperator = v);
///
public QueryStringQueryDescriptor Analyzer(string analyzer) => Assign(analyzer, (a, v) => a.Analyzer = v);
///
public QueryStringQueryDescriptor QuoteAnalyzer(string analyzer) => Assign(analyzer, (a, v) => a.QuoteAnalyzer = v);
///
public QueryStringQueryDescriptor AllowLeadingWildcard(bool? allowLeadingWildcard = true) =>
Assign(allowLeadingWildcard, (a, v) => a.AllowLeadingWildcard = v);
///
public QueryStringQueryDescriptor Fuzziness(Fuzziness fuzziness) => Assign(fuzziness, (a, v) => a.Fuzziness = v);
///
public QueryStringQueryDescriptor FuzzyPrefixLength(int? fuzzyPrefixLength) => Assign(fuzzyPrefixLength, (a, v) => a.FuzzyPrefixLength = v);
///
public QueryStringQueryDescriptor FuzzyMaxExpansions(int? fuzzyMaxExpansions) => Assign(fuzzyMaxExpansions, (a, v) => a.FuzzyMaxExpansions = v);
///
public QueryStringQueryDescriptor FuzzyTranspositions(bool? fuzzyTranspositions = true) =>
Assign(fuzzyTranspositions, (a, v) => a.FuzzyTranspositions = v);
///
public QueryStringQueryDescriptor PhraseSlop(double? phraseSlop) => Assign(phraseSlop, (a, v) => a.PhraseSlop = v);
///
public QueryStringQueryDescriptor MinimumShouldMatch(MinimumShouldMatch minimumShouldMatch) =>
Assign(minimumShouldMatch, (a, v) => a.MinimumShouldMatch = v);
///
public QueryStringQueryDescriptor Lenient(bool? lenient = true) => Assign(lenient, (a, v) => a.Lenient = v);
///
public QueryStringQueryDescriptor AnalyzeWildcard(bool? analyzeWildcard = true) => Assign(analyzeWildcard, (a, v) => a.AnalyzeWildcard = v);
///
public QueryStringQueryDescriptor TieBreaker(double? tieBreaker) => Assign(tieBreaker, (a, v) => a.TieBreaker = v);
///
public QueryStringQueryDescriptor MaximumDeterminizedStates(int? maxDeterminizedStates) =>
Assign(maxDeterminizedStates, (a, v) => a.MaximumDeterminizedStates = v);
///
public QueryStringQueryDescriptor FuzzyRewrite(MultiTermQueryRewrite rewrite) => Assign(rewrite, (a, v) => a.FuzzyRewrite = v);
///
public QueryStringQueryDescriptor Rewrite(MultiTermQueryRewrite rewrite) => Assign(rewrite, (a, v) => a.Rewrite = v);
///
public QueryStringQueryDescriptor QuoteFieldSuffix(string quoteFieldSuffix) =>
Assign(quoteFieldSuffix, (a, v) => a.QuoteFieldSuffix = v);
///
public QueryStringQueryDescriptor Escape(bool? escape = true) => Assign(escape, (a, v) => a.Escape = v);
///
public QueryStringQueryDescriptor EnablePositionIncrements(bool? enablePositionIncrements = true) =>
Assign(enablePositionIncrements, (a, v) => a.EnablePositionIncrements = v);
///
public QueryStringQueryDescriptor TimeZone(string timezone) => Assign(timezone, (a, v) => a.TimeZone = v);
///
public QueryStringQueryDescriptor AutoGenerateSynonymsPhraseQuery(bool? autoGenerateSynonymsPhraseQuery = true) =>
Assign(autoGenerateSynonymsPhraseQuery, (a, v) => a.AutoGenerateSynonymsPhraseQuery = v);
}
}