|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Runtime.Serialization; |
| 7 | +using Elasticsearch.Net.Utf8Json; |
| 8 | + |
| 9 | +namespace Nest |
| 10 | +{ |
| 11 | + [InterfaceDataContract] |
| 12 | + [ReadAs(typeof(CombinedFieldsQuery))] |
| 13 | + public interface ICombinedFieldsQuery : IQuery |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// The query to execute |
| 17 | + /// </summary> |
| 18 | + [DataMember(Name = "query")] |
| 19 | + string Query { get; set; } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// The fields to perform the query against. |
| 23 | + /// </summary> |
| 24 | + [DataMember(Name = "fields")] |
| 25 | + Fields Fields { get; set; } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// A value controlling how many "should" clauses in the resulting boolean query should match. |
| 29 | + /// It can be an absolute value, a percentage or a combination of both. |
| 30 | + /// </summary> |
| 31 | + [DataMember(Name = "minimum_should_match")] |
| 32 | + MinimumShouldMatch MinimumShouldMatch { get; set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// If `true`, match phrase queries are automatically created for multi-term synonyms. |
| 36 | + /// </summary> |
| 37 | + [DataMember(Name = "auto_generate_synonyms_phrase_query")] |
| 38 | + bool? AutoGenerateSynonymsPhraseQuery { get; set; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// The operator used if no explicit operator is specified. |
| 42 | + /// The default operator is <see cref="Nest.Operator.Or" /> |
| 43 | + /// </summary> |
| 44 | + /// <remarks> |
| 45 | + /// <see cref="TextQueryType.BestFields" /> and <see cref="TextQueryType.MostFields" /> types are field-centric?; |
| 46 | + /// they generate a match query per field. This means that <see cref="Operator" /> and <see cref="MinimumShouldMatch" /> |
| 47 | + /// are applied to each field individually, which is probably not what you want. |
| 48 | + /// Consider using <see cref="TextQueryType.CrossFields" />. |
| 49 | + /// </remarks> |
| 50 | + [DataMember(Name = "operator")] |
| 51 | + Operator? Operator { get; set; } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// If the analyzer used removes all tokens in a query like a stop filter does, the default behavior is |
| 55 | + /// to match no documents at all. In order to change that, <see cref="Nest.ZeroTermsQuery" /> can be used, |
| 56 | + /// which accepts <see cref="Nest.ZeroTermsQuery.None" /> (default) and <see cref="Nest.ZeroTermsQuery.All" /> |
| 57 | + /// which corresponds to a match_all query. |
| 58 | + /// </summary> |
| 59 | + [DataMember(Name = "zero_terms_query")] |
| 60 | + ZeroTermsQuery? ZeroTermsQuery { get; set; } |
| 61 | + } |
| 62 | + |
| 63 | + /// <inheritdoc cref="ICombinedFieldsQuery" /> |
| 64 | + [DataContract] |
| 65 | + public class CombinedFieldsQuery : QueryBase, ICombinedFieldsQuery |
| 66 | + { |
| 67 | + /// <inheritdoc /> |
| 68 | + public string Query { get; set; } |
| 69 | + /// <inheritdoc /> |
| 70 | + public Fields Fields { get; set; } |
| 71 | + /// <inheritdoc /> |
| 72 | + public MinimumShouldMatch MinimumShouldMatch { get; set; } |
| 73 | + /// <inheritdoc /> |
| 74 | + public bool? AutoGenerateSynonymsPhraseQuery { get; set; } |
| 75 | + /// <inheritdoc /> |
| 76 | + public Operator? Operator { get; set; } |
| 77 | + /// <inheritdoc /> |
| 78 | + public ZeroTermsQuery? ZeroTermsQuery { get; set; } |
| 79 | + |
| 80 | + protected override bool Conditionless => IsConditionless(this); |
| 81 | + |
| 82 | + internal override void InternalWrapInContainer(IQueryContainer c) => c.CombinedFields = this; |
| 83 | + |
| 84 | + internal static bool IsConditionless(ICombinedFieldsQuery q) => q.Fields.IsConditionless() || q.Query.IsNullOrEmpty(); |
| 85 | + } |
| 86 | + |
| 87 | + public class CombinedFieldsQueryDescriptor<T> |
| 88 | + : QueryDescriptorBase<CombinedFieldsQueryDescriptor<T>, ICombinedFieldsQuery>, ICombinedFieldsQuery where T : class |
| 89 | + { |
| 90 | + protected override bool Conditionless => CombinedFieldsQuery.IsConditionless(this); |
| 91 | + |
| 92 | + string ICombinedFieldsQuery.Query { get; set; } |
| 93 | + Fields ICombinedFieldsQuery.Fields { get; set; } |
| 94 | + MinimumShouldMatch ICombinedFieldsQuery.MinimumShouldMatch { get; set; } |
| 95 | + bool? ICombinedFieldsQuery.AutoGenerateSynonymsPhraseQuery { get; set; } |
| 96 | + Operator? ICombinedFieldsQuery.Operator { get; set; } |
| 97 | + ZeroTermsQuery? ICombinedFieldsQuery.ZeroTermsQuery { get; set; } |
| 98 | + |
| 99 | + /// <inheritdoc cref="ICombinedFieldsQuery.Query" /> |
| 100 | + public CombinedFieldsQueryDescriptor<T> Query(string query) => Assign(query, (a, v) => a.Query = v); |
| 101 | + |
| 102 | + /// <inheritdoc cref="ICombinedFieldsQuery.Fields" /> |
| 103 | + public CombinedFieldsQueryDescriptor<T> Fields(Func<FieldsDescriptor<T>, IPromise<Fields>> fields) => |
| 104 | + Assign(fields, (a, v) => a.Fields = v?.Invoke(new FieldsDescriptor<T>())?.Value); |
| 105 | + |
| 106 | + /// <inheritdoc cref="ICombinedFieldsQuery.Fields" /> |
| 107 | + public CombinedFieldsQueryDescriptor<T> Fields(Fields fields) => Assign(fields, (a, v) => a.Fields = v); |
| 108 | + |
| 109 | + /// <inheritdoc cref="ICombinedFieldsQuery.MinimumShouldMatch" /> |
| 110 | + public CombinedFieldsQueryDescriptor<T> MinimumShouldMatch(MinimumShouldMatch minimumShouldMatch) |
| 111 | + => Assign(minimumShouldMatch, (a, v) => a.MinimumShouldMatch = v); |
| 112 | + |
| 113 | + /// <inheritdoc cref="ICombinedFieldsQuery.Operator" /> |
| 114 | + public CombinedFieldsQueryDescriptor<T> Operator(Operator? op) => Assign(op, (a, v) => a.Operator = v); |
| 115 | + |
| 116 | + /// <inheritdoc cref="ICombinedFieldsQuery.ZeroTermsQuery" /> |
| 117 | + public CombinedFieldsQueryDescriptor<T> ZeroTermsQuery(ZeroTermsQuery? zeroTermsQuery) => Assign(zeroTermsQuery, (a, v) => a.ZeroTermsQuery = v); |
| 118 | + |
| 119 | + /// <inheritdoc cref="ICombinedFieldsQuery.AutoGenerateSynonymsPhraseQuery" /> |
| 120 | + public CombinedFieldsQueryDescriptor<T> AutoGenerateSynonymsPhraseQuery(bool? autoGenerateSynonymsPhraseQuery = true) => |
| 121 | + Assign(autoGenerateSynonymsPhraseQuery, (a, v) => a.AutoGenerateSynonymsPhraseQuery = v); |
| 122 | + } |
| 123 | +} |
0 commit comments