Skip to content

Commit d2b7f32

Browse files
russcamcodebrain
authored andcommitted
Change Fields to be optional on MultiMatchQuery (#3195)
This commit adds the ability to send multi_match query without providing any fields. When no fields are provided the multi_match query will use the fields defined in the index setting index.query.default_field (which in turns defaults to *). Closes #3159
1 parent b8c0db5 commit d2b7f32

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/Nest/QueryDsl/FullText/MultiMatch/MultiMatchQuery.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public interface IMultiMatchQuery : IQuery
142142
[JsonProperty("zero_terms_query")]
143143
ZeroTermsQuery? ZeroTermsQuery { get; set; }
144144

145-
/// <summary></summary>
145+
/// <summary></summary>
146146
[JsonProperty("auto_generate_synonyms_phrase_query")]
147147
bool? AutoGenerateSynonymsPhraseQuery { get; set; }
148148
}
@@ -185,11 +185,11 @@ public class MultiMatchQuery : QueryBase, IMultiMatchQuery
185185
public ZeroTermsQuery? ZeroTermsQuery { get; set; }
186186
/// <inheritdoc />
187187
public bool? FuzzyTranspositions { get; set; }
188-
/// <inheritdoc />
188+
/// <inheritdoc />
189189
public bool? AutoGenerateSynonymsPhraseQuery { get; set; }
190190

191191
internal override void InternalWrapInContainer(IQueryContainer c) => c.MultiMatch = this;
192-
internal static bool IsConditionless(IMultiMatchQuery q) => q.Fields.IsConditionless() || q.Query.IsNullOrEmpty();
192+
internal static bool IsConditionless(IMultiMatchQuery q) => q.Query.IsNullOrEmpty();
193193
}
194194

195195
/// <inheritdoc cref="IMultiMatchQuery"/>
@@ -276,7 +276,7 @@ public MultiMatchQueryDescriptor<T> FuzzyTranspositions(bool? fuzzyTranpositions
276276
/// <inheritdoc cref="IMultiMatchQuery.ZeroTermsQuery"/>
277277
public MultiMatchQueryDescriptor<T> ZeroTermsQuery(ZeroTermsQuery? zeroTermsQuery) => Assign(a => a.ZeroTermsQuery = zeroTermsQuery);
278278

279-
/// <inheritdoc cref="IMultiMatchQuery.AutoGenerateSynonymsPhraseQuery"/>
279+
/// <inheritdoc cref="IMultiMatchQuery.AutoGenerateSynonymsPhraseQuery"/>
280280
public MultiMatchQueryDescriptor<T> AutoGenerateSynonymsPhraseQuery(bool? autoGenerateSynonymsPhraseQuery = true) =>
281281
Assign(a => a.AutoGenerateSynonymsPhraseQuery = autoGenerateSynonymsPhraseQuery);
282282
}

src/Tests/QueryDsl/FullText/MultiMatch/MultiMatchUsageTests.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
8282
protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<IMultiMatchQuery>(a => a.MultiMatch)
8383
{
8484
q => q.Query = null,
85-
q => q.Query = string.Empty,
86-
q => q.Fields = null
85+
q => q.Query = string.Empty
8786
};
8887
}
8988

@@ -118,4 +117,34 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
118117
.Query("hello world")
119118
);
120119
}
120+
121+
/**[float]
122+
* === Multi match with no fields specified
123+
*
124+
* Starting with Elasticsearch 6.1.0+, it's possible to send a Multi Match query without providing any fields.
125+
* When no fields are provided the Multi Match query will use the fields defined in the index setting `index.query.default_field`
126+
* (which in turns defaults to `*`).
127+
*/
128+
public class MultiMatchWithNoFieldsSpecifiedUsageTests : QueryDslUsageTestsBase
129+
{
130+
public MultiMatchWithNoFieldsSpecifiedUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
131+
132+
protected override object QueryJson => new
133+
{
134+
multi_match = new
135+
{
136+
query = "hello world"
137+
}
138+
};
139+
140+
protected override QueryContainer QueryInitializer => new MultiMatchQuery
141+
{
142+
Query = "hello world",
143+
};
144+
145+
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
146+
.MultiMatch(c => c
147+
.Query("hello world")
148+
);
149+
}
121150
}

0 commit comments

Comments
 (0)