diff --git a/server/src/main/java/org/elasticsearch/index/search/MatchQuery.java b/server/src/main/java/org/elasticsearch/index/search/MatchQuery.java index 72ffe019324f9..d42466b5ebe66 100644 --- a/server/src/main/java/org/elasticsearch/index/search/MatchQuery.java +++ b/server/src/main/java/org/elasticsearch/index/search/MatchQuery.java @@ -59,6 +59,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Set; import java.util.function.Supplier; import static org.elasticsearch.common.lucene.search.Queries.newLenientFieldQuery; @@ -235,6 +236,14 @@ public Query parse(Type type, String fieldName, Object value) throws IOException if (fieldType == null) { return newUnmappedFieldQuery(fieldName); } + Set fields = context.simpleMatchToIndexNames(fieldName); + if (fields.contains(fieldName)) { + assert fields.size() == 1; + // this field is a concrete field or an alias so we use the + // field type name directly + fieldName = fieldType.name(); + } + Analyzer analyzer = getAnalyzer(fieldType, type == Type.PHRASE || type == Type.PHRASE_PREFIX); assert analyzer != null; @@ -248,9 +257,9 @@ public Query parse(Type type, String fieldName, Object value) throws IOException */ if (analyzer == Lucene.KEYWORD_ANALYZER && type != Type.PHRASE_PREFIX) { final Term term = new Term(fieldName, value.toString()); - if ((fieldType instanceof TextFieldMapper.TextFieldType || fieldType instanceof KeywordFieldMapper.KeywordFieldType) - && type == Type.BOOLEAN_PREFIX) { - return builder.newPrefixQuery(fieldName, term); + if (type == Type.BOOLEAN_PREFIX + && (fieldType instanceof TextFieldMapper.TextFieldType || fieldType instanceof KeywordFieldMapper.KeywordFieldType)) { + return builder.newPrefixQuery(term); } else { return builder.newTermQuery(term); } @@ -540,12 +549,12 @@ protected Query newTermQuery(Term term) { /** * Builds a new prefix query instance. */ - protected Query newPrefixQuery(String field, Term term) { + protected Query newPrefixQuery(Term term) { try { return fieldType.prefixQuery(term.text(), null, context); } catch (RuntimeException e) { if (lenient) { - return newLenientFieldQuery(field, e); + return newLenientFieldQuery(term.field(), e); } throw e; } @@ -562,7 +571,7 @@ private Query analyzeTerm(String field, TokenStream stream, boolean isPrefix) th final Term term = new Term(field, termAtt.getBytesRef()); int lastOffset = offsetAtt.endOffset(); stream.end(); - return isPrefix && lastOffset == offsetAtt.endOffset() ? newPrefixQuery(field, term) : newTermQuery(term); + return isPrefix && lastOffset == offsetAtt.endOffset() ? newPrefixQuery(term) : newTermQuery(term); } private void add(BooleanQuery.Builder q, String field, List current, BooleanClause.Occur operator, boolean isPrefix) { @@ -571,7 +580,7 @@ private void add(BooleanQuery.Builder q, String field, List current, Boole } if (current.size() == 1) { if (isPrefix) { - q.add(newPrefixQuery(field, current.get(0)), operator); + q.add(newPrefixQuery(current.get(0)), operator); } else { q.add(newTermQuery(current.get(0)), operator); } @@ -688,7 +697,7 @@ public Query next() { Term[] terms = graph.getTerms(field, start); assert terms.length > 0; if (terms.length == 1) { - queryPos = usePrefix ? newPrefixQuery(field, terms[0]) : newTermQuery(terms[0]); + queryPos = usePrefix ? newPrefixQuery(terms[0]) : newTermQuery(terms[0]); } else { // We don't apply prefix on synonyms queryPos = newSynonymQuery(terms); diff --git a/server/src/main/java/org/elasticsearch/index/search/MultiMatchQuery.java b/server/src/main/java/org/elasticsearch/index/search/MultiMatchQuery.java index cbc06a6ff081d..8e33425b3981e 100644 --- a/server/src/main/java/org/elasticsearch/index/search/MultiMatchQuery.java +++ b/server/src/main/java/org/elasticsearch/index/search/MultiMatchQuery.java @@ -194,7 +194,7 @@ protected Query newTermQuery(Term term) { } @Override - protected Query newPrefixQuery(String field, Term term) { + protected Query newPrefixQuery(Term term) { List disjunctions = new ArrayList<>(); for (FieldAndBoost fieldType : blendedFields) { Query query = fieldType.fieldType.prefixQuery(term.text(), null, context); diff --git a/server/src/main/java/org/elasticsearch/index/search/QueryParserHelper.java b/server/src/main/java/org/elasticsearch/index/search/QueryParserHelper.java index 8d6198e17e2cc..e4b397c9a538b 100644 --- a/server/src/main/java/org/elasticsearch/index/search/QueryParserHelper.java +++ b/server/src/main/java/org/elasticsearch/index/search/QueryParserHelper.java @@ -52,7 +52,7 @@ public static Map parseFieldsAndWeights(List fields) { float boost = 1.0f; if (boostIndex != -1) { fieldName = field.substring(0, boostIndex); - boost = Float.parseFloat(field.substring(boostIndex+1, field.length())); + boost = Float.parseFloat(field.substring(boostIndex+1)); } else { fieldName = field; } diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java index 7d15c98af6a18..3425ce0a5e268 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java @@ -31,6 +31,7 @@ import org.apache.lucene.search.PhraseQuery; import org.apache.lucene.search.PointRangeQuery; import org.apache.lucene.search.Query; +import org.apache.lucene.search.SynonymQuery; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.spans.SpanNearQuery; import org.apache.lucene.search.spans.SpanOrQuery; @@ -475,6 +476,18 @@ public void testMultiWordSynonymsPhrase() throws Exception { assertEquals(expected, actual); } + + public void testAliasWithSynonyms() throws Exception { + final MatchQuery matchQuery = new MatchQuery(createShardContext()); + matchQuery.setAnalyzer(new MockSynonymAnalyzer()); + final Query actual = matchQuery.parse(Type.PHRASE, STRING_ALIAS_FIELD_NAME, "dogs"); + Query expected = new SynonymQuery.Builder(STRING_FIELD_NAME) + .addTerm(new Term(STRING_FIELD_NAME, "dogs")) + .addTerm(new Term(STRING_FIELD_NAME, "dog")) + .build(); + assertEquals(expected, actual); + } + public void testMaxBooleanClause() { MatchQuery query = new MatchQuery(createShardContext()); query.setAnalyzer(new MockGraphAnalyzer(createGiantGraph(40)));