Skip to content

Commit

Permalink
Fix alias resolution in boolean match query (#76688)
Browse files Browse the repository at this point in the history
When backporting #68795, we introduced a regression around `match` queries on
field aliases. If the `match` query parses to a `synonym` query (because its
analyzer can produce multiple tokens at a single position), then we don't
resolve the field alias to its concrete field. This means the query will not
have any results.

We already had a test for this case but it was too narrow. Note this regression
only affects 7.x, there is no bug on master.
  • Loading branch information
jtibshirani authored Aug 23, 2021
1 parent 477b6a8 commit d0bfad8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ public Query parse(Type type, String fieldName, Object value) throws IOException
switch (type) {
case BOOLEAN:
if (commonTermsCutoff == null) {
query = builder.createBooleanQuery(fieldName, value.toString(), occur);
query = builder.createBooleanQuery(resolvedFieldName, value.toString(), occur);
} else {
query = createCommonTermsQuery(builder, fieldName, value.toString(), occur, occur, commonTermsCutoff);
query = createCommonTermsQuery(builder, resolvedFieldName, value.toString(), occur, occur, commonTermsCutoff);
}
break;
case BOOLEAN_PREFIX:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -488,16 +489,18 @@ public void testMultiWordSynonymsPhrase() throws Exception {
assertEquals(expected, actual);
}


public void testAliasWithSynonyms() throws Exception {
final MatchQueryParser matchQueryParser = new MatchQueryParser(createSearchExecutionContext());
MatchQueryParser matchQueryParser = new MatchQueryParser(createSearchExecutionContext());
matchQueryParser.setAnalyzer(new MockSynonymAnalyzer());
final Query actual = matchQueryParser.parse(Type.PHRASE, TEXT_ALIAS_FIELD_NAME, "dogs");
Query expected = new SynonymQuery.Builder(TEXT_FIELD_NAME)
.addTerm(new Term(TEXT_FIELD_NAME, "dogs"))
.addTerm(new Term(TEXT_FIELD_NAME, "dog"))
.build();
assertEquals(expected, actual);

for (Type type : Arrays.asList(Type.BOOLEAN, Type.PHRASE)) {
Query actual = matchQueryParser.parse(type, TEXT_ALIAS_FIELD_NAME, "dogs");
Query expected = new SynonymQuery.Builder(TEXT_FIELD_NAME)
.addTerm(new Term(TEXT_FIELD_NAME, "dogs"))
.addTerm(new Term(TEXT_FIELD_NAME, "dog"))
.build();
assertEquals(expected, actual);
}
}

public void testMaxBooleanClause() {
Expand Down

0 comments on commit d0bfad8

Please sign in to comment.