diff --git a/core/src/main/java/org/elasticsearch/common/lucene/all/AllTermQuery.java b/core/src/main/java/org/elasticsearch/common/lucene/all/AllTermQuery.java index 500fa206c96..269c1c55eec 100644 --- a/core/src/main/java/org/elasticsearch/common/lucene/all/AllTermQuery.java +++ b/core/src/main/java/org/elasticsearch/common/lucene/all/AllTermQuery.java @@ -64,6 +64,10 @@ public final class AllTermQuery extends Query { this.term = term; } + public Term getTerm() { + return term; + } + @Override public boolean equals(Object obj) { if (sameClassAs(obj) == false) { diff --git a/core/src/main/java/org/elasticsearch/index/search/MatchQuery.java b/core/src/main/java/org/elasticsearch/index/search/MatchQuery.java index f2161013de7..cf8be75306f 100644 --- a/core/src/main/java/org/elasticsearch/index/search/MatchQuery.java +++ b/core/src/main/java/org/elasticsearch/index/search/MatchQuery.java @@ -37,6 +37,7 @@ import org.elasticsearch.common.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.lucene.all.AllTermQuery; import org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.unit.Fuzziness; @@ -324,6 +325,9 @@ public class MatchQuery { } else if (query instanceof TermQuery) { prefixQuery.add(((TermQuery) query).getTerm()); return prefixQuery; + } else if (query instanceof AllTermQuery) { + prefixQuery.add(((AllTermQuery) query).getTerm()); + return prefixQuery; } return query; } diff --git a/core/src/test/java/org/elasticsearch/index/search/MultiMatchQueryTests.java b/core/src/test/java/org/elasticsearch/index/search/MultiMatchQueryTests.java index 884e1d45829..88d22de6e27 100644 --- a/core/src/test/java/org/elasticsearch/index/search/MultiMatchQueryTests.java +++ b/core/src/test/java/org/elasticsearch/index/search/MultiMatchQueryTests.java @@ -22,15 +22,16 @@ package org.elasticsearch.index.search; import org.apache.lucene.index.Term; import org.apache.lucene.queries.BlendedTermQuery; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BoostQuery; import org.apache.lucene.search.DisjunctionMaxQuery; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; -import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.compress.CompressedXContent; +import org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.mapper.MapperService; @@ -45,6 +46,8 @@ import java.io.IOException; import java.util.Arrays; import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; public class MultiMatchQueryTests extends ESSingleNodeTestCase { @@ -154,4 +157,13 @@ public class MultiMatchQueryTests extends ESSingleNodeTestCase { Query actual = MultiMatchQuery.blendTerm(new BytesRef("baz"), null, 1f, new FieldAndFieldType(ft1, 2), new FieldAndFieldType(ft2, 3)); assertEquals(expected, actual); } + + public void testMultiMatchPrefixWithAllField() throws IOException { + QueryShardContext queryShardContext = indexService.newQueryShardContext(); + queryShardContext.setAllowUnmappedFields(true); + Query parsedQuery = + multiMatchQuery("foo").field("_all").type(MultiMatchQueryBuilder.Type.PHRASE_PREFIX).toQuery(queryShardContext); + assertThat(parsedQuery, instanceOf(MultiPhrasePrefixQuery.class)); + assertThat(parsedQuery.toString(), equalTo("_all:\"foo*\"")); + } }