diff --git a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryParser.java index fcf1a12eba7..c191e1996b3 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryParser.java @@ -30,6 +30,7 @@ import org.apache.lucene.search.FuzzyQuery; import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.index.mapper.MappedFieldType; import java.io.IOException; import java.util.Locale; @@ -43,11 +44,13 @@ import java.util.Objects; public class SimpleQueryParser extends org.apache.lucene.queryparser.simple.SimpleQueryParser { private final Settings settings; + private QueryShardContext context; /** Creates a new parser with custom flags used to enable/disable certain features. */ - public SimpleQueryParser(Analyzer analyzer, Map weights, int flags, Settings settings) { + public SimpleQueryParser(Analyzer analyzer, Map weights, int flags, Settings settings, QueryShardContext context) { super(analyzer, weights, flags); this.settings = settings; + this.context = context; } /** @@ -60,6 +63,15 @@ public class SimpleQueryParser extends org.apache.lucene.queryparser.simple.Simp throw e; } + @Override + protected Query newTermQuery(Term term) { + MappedFieldType currentFieldType = context.fieldMapper(term.field()); + if (currentFieldType == null || currentFieldType.tokenized()) { + return super.newTermQuery(term); + } + return currentFieldType.termQuery(term.bytes(), context); + } + @Override public Query newDefaultQuery(String text) { BooleanQuery.Builder bq = new BooleanQuery.Builder(); diff --git a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java index 187f2c33607..86c32ea3670 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java @@ -363,7 +363,7 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder 0) { + assertThat(query, instanceOf(MatchAllDocsQuery.class)); + } + } } diff --git a/core/src/test/java/org/elasticsearch/search/query/SimpleQueryStringIT.java b/core/src/test/java/org/elasticsearch/search/query/SimpleQueryStringIT.java index 5bb20d0dd67..ffaebecf89a 100644 --- a/core/src/test/java/org/elasticsearch/search/query/SimpleQueryStringIT.java +++ b/core/src/test/java/org/elasticsearch/search/query/SimpleQueryStringIT.java @@ -359,4 +359,15 @@ public class SimpleQueryStringIT extends ESIntegTestCase { assertHitCount(searchResponse, 1L); assertSearchHits(searchResponse, "1"); } + + public void testSimpleQueryStringOnIndexMetaField() throws Exception { + client().prepareIndex("test", "type1", "1").setSource("foo", 123, "bar", "abc").get(); + client().prepareIndex("test", "type1", "2").setSource("foo", 234, "bar", "bcd").get(); + + refresh(); + + SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("test").field("_index")).get(); + assertHitCount(searchResponse, 2L); + assertSearchHits(searchResponse, "1", "2"); + } }