diff --git a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringParser.java b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringParser.java index c696a7522f4..d80423d90fd 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringParser.java @@ -27,10 +27,8 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.regex.Regex; -import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.LocaleUtils; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import java.io.IOException; @@ -55,7 +53,7 @@ import java.util.Map; *
  • '{@code ~}N' at the end of phrases specifies near/slop query: "term1 term2"~5 * *

    - * See: {@link XSimpleQueryParser} for more information. + * See: {@link SimpleQueryParser} for more information. *

    * This query supports these options: *

    @@ -75,7 +73,7 @@ public class SimpleQueryStringParser implements QueryParser { public static final String NAME = "simple_query_string"; @Inject - public SimpleQueryStringParser(Settings settings) { + public SimpleQueryStringParser() { } @@ -92,7 +90,6 @@ public class SimpleQueryStringParser implements QueryParser { String queryBody = null; float boost = 1.0f; String queryName = null; - String field = null; String minimumShouldMatch = null; Map fieldsAndWeights = null; BooleanClause.Occur defaultOperator = null; @@ -141,9 +138,7 @@ public class SimpleQueryStringParser implements QueryParser { } } } else { - throw new QueryParsingException(parseContext, - "[" + NAME + "] query does not support [" + currentFieldName - + "]"); + throw new QueryParsingException(parseContext, "[" + NAME + "] query does not support [" + currentFieldName + "]"); } } else if (token.isValue()) { if ("query".equals(currentFieldName)) { @@ -155,8 +150,6 @@ public class SimpleQueryStringParser implements QueryParser { if (analyzer == null) { throw new QueryParsingException(parseContext, "[" + NAME + "] analyzer [" + parser.text() + "] not found"); } - } else if ("field".equals(currentFieldName)) { - field = parser.text(); } else if ("default_operator".equals(currentFieldName) || "defaultOperator".equals(currentFieldName)) { String op = parser.text(); if ("or".equalsIgnoreCase(op)) { @@ -201,16 +194,6 @@ public class SimpleQueryStringParser implements QueryParser { if (queryBody == null) { throw new QueryParsingException(parseContext, "[" + NAME + "] query text missing"); } - - // Support specifying only a field instead of a map - if (field == null) { - field = currentFieldName; - } - - // Use the default field (_all) if no fields specified - if (fieldsAndWeights == null) { - field = parseContext.defaultField(); - } // Use standard analyzer by default if (analyzer == null) { @@ -218,7 +201,7 @@ public class SimpleQueryStringParser implements QueryParser { } if (fieldsAndWeights == null) { - fieldsAndWeights = Collections.singletonMap(field, 1.0F); + fieldsAndWeights = Collections.singletonMap(parseContext.defaultField(), 1.0F); } SimpleQueryParser sqp = new SimpleQueryParser(analyzer, fieldsAndWeights, flags, sqsSettings); diff --git a/core/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java b/core/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java index 5381c92a3f9..c3a61c5a006 100644 --- a/core/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java @@ -21,7 +21,6 @@ package org.elasticsearch.index.query; import com.google.common.collect.Lists; import com.google.common.collect.Sets; - import org.apache.lucene.analysis.core.WhitespaceAnalyzer; import org.apache.lucene.index.*; import org.apache.lucene.index.memory.MemoryIndex; @@ -39,6 +38,7 @@ import org.apache.lucene.util.CharsRefBuilder; import org.apache.lucene.util.NumericUtils; import org.apache.lucene.util.automaton.TooComplexToDeterminizeException; import org.elasticsearch.action.termvectors.*; +import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.compress.CompressedXContent; @@ -73,11 +73,12 @@ import java.io.IOException; import java.util.Arrays; import java.util.EnumSet; import java.util.List; +import java.util.Locale; -import static org.elasticsearch.test.StreamsUtils.copyToBytesFromClasspath; -import static org.elasticsearch.test.StreamsUtils.copyToStringFromClasspath; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.*; +import static org.elasticsearch.test.StreamsUtils.copyToBytesFromClasspath; +import static org.elasticsearch.test.StreamsUtils.copyToStringFromClasspath; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery; import static org.hamcrest.Matchers.*; @@ -2379,7 +2380,7 @@ public class SimpleIndexQueryParserTests extends ESSingleNodeTestCase { IndexQueryParserService queryParser = queryParser(); String query = jsonBuilder().startObject().startObject("function_score") .startArray("functions") - .startObject().field("weight", 2).field("boost_factor",2).endObject() + .startObject().field("weight", 2).field("boost_factor", 2).endObject() .endArray() .endObject().endObject().string(); try { @@ -2480,4 +2481,19 @@ public class SimpleIndexQueryParserTests extends ESSingleNodeTestCase { assertThat(prefixQuery.getRewriteMethod(), instanceOf(MultiTermQuery.TopTermsBlendedFreqScoringRewrite.class)); } } + + @Test + public void testSimpleQueryStringNoFields() throws Exception { + IndexQueryParserService queryParser = queryParser(); + String queryText = randomAsciiOfLengthBetween(1, 10).toLowerCase(Locale.ROOT); + String query = "{\n" + + " \"simple_query_string\" : {\n" + + " \"query\" : \"" + queryText + "\"\n" + + " }\n" + + "}"; + Query parsedQuery = queryParser.parse(query).query(); + assertThat(parsedQuery, instanceOf(TermQuery.class)); + TermQuery termQuery = (TermQuery) parsedQuery; + assertThat(termQuery.getTerm(), equalTo(new Term(MetaData.ALL, queryText))); + } } 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 f660decbb9d..e41c451b981 100644 --- a/core/src/test/java/org/elasticsearch/search/query/SimpleQueryStringIT.java +++ b/core/src/test/java/org/elasticsearch/search/query/SimpleQueryStringIT.java @@ -93,11 +93,6 @@ public class SimpleQueryStringIT extends ESIntegTestCase { searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("spaghetti").field("*body")).get(); assertHitCount(searchResponse, 2l); assertSearchHits(searchResponse, "5", "6"); - - // Have to bypass the builder here because the builder always uses "fields" instead of "field" - searchResponse = client().prepareSearch().setQuery("{\"simple_query_string\": {\"query\": \"spaghetti\", \"field\": \"_all\"}}").get(); - assertHitCount(searchResponse, 2l); - assertSearchHits(searchResponse, "5", "6"); } @Test