simple query string: remove (not working) support for alternate formats

Removed attempt of parsing of `field` rather than `fields` and attempted support of the following syntax:

```
{
  "simple_query_string": {
    "body" : {
      "query": "foo bar"
    }
  }
}
```

Both these two syntaxes were undocumented, untested and not working.

Added test for case when `fields` is not specified, then the default field is queried.

Closes #12794
Closes #12798
This commit is contained in:
javanna 2015-08-11 11:50:27 +02:00 committed by Luca Cavanna
parent a172c50a5e
commit db7219901d
3 changed files with 24 additions and 30 deletions

View File

@ -27,10 +27,8 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.LocaleUtils; import org.elasticsearch.common.util.LocaleUtils;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MappedFieldType;
import java.io.IOException; import java.io.IOException;
@ -55,7 +53,7 @@ import java.util.Map;
* <li>'{@code ~}N' at the end of phrases specifies near/slop query: <tt>"term1 term2"~5</tt> * <li>'{@code ~}N' at the end of phrases specifies near/slop query: <tt>"term1 term2"~5</tt>
* </ul> * </ul>
* <p/> * <p/>
* See: {@link XSimpleQueryParser} for more information. * See: {@link SimpleQueryParser} for more information.
* <p/> * <p/>
* This query supports these options: * This query supports these options:
* <p/> * <p/>
@ -75,7 +73,7 @@ public class SimpleQueryStringParser implements QueryParser {
public static final String NAME = "simple_query_string"; public static final String NAME = "simple_query_string";
@Inject @Inject
public SimpleQueryStringParser(Settings settings) { public SimpleQueryStringParser() {
} }
@ -92,7 +90,6 @@ public class SimpleQueryStringParser implements QueryParser {
String queryBody = null; String queryBody = null;
float boost = 1.0f; float boost = 1.0f;
String queryName = null; String queryName = null;
String field = null;
String minimumShouldMatch = null; String minimumShouldMatch = null;
Map<String, Float> fieldsAndWeights = null; Map<String, Float> fieldsAndWeights = null;
BooleanClause.Occur defaultOperator = null; BooleanClause.Occur defaultOperator = null;
@ -141,9 +138,7 @@ public class SimpleQueryStringParser implements QueryParser {
} }
} }
} else { } else {
throw new QueryParsingException(parseContext, throw new QueryParsingException(parseContext, "[" + NAME + "] query does not support [" + currentFieldName + "]");
"[" + NAME + "] query does not support [" + currentFieldName
+ "]");
} }
} else if (token.isValue()) { } else if (token.isValue()) {
if ("query".equals(currentFieldName)) { if ("query".equals(currentFieldName)) {
@ -155,8 +150,6 @@ public class SimpleQueryStringParser implements QueryParser {
if (analyzer == null) { if (analyzer == null) {
throw new QueryParsingException(parseContext, "[" + NAME + "] analyzer [" + parser.text() + "] not found"); 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)) { } else if ("default_operator".equals(currentFieldName) || "defaultOperator".equals(currentFieldName)) {
String op = parser.text(); String op = parser.text();
if ("or".equalsIgnoreCase(op)) { if ("or".equalsIgnoreCase(op)) {
@ -201,16 +194,6 @@ public class SimpleQueryStringParser implements QueryParser {
if (queryBody == null) { if (queryBody == null) {
throw new QueryParsingException(parseContext, "[" + NAME + "] query text missing"); 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 // Use standard analyzer by default
if (analyzer == null) { if (analyzer == null) {
@ -218,7 +201,7 @@ public class SimpleQueryStringParser implements QueryParser {
} }
if (fieldsAndWeights == null) { if (fieldsAndWeights == null) {
fieldsAndWeights = Collections.singletonMap(field, 1.0F); fieldsAndWeights = Collections.singletonMap(parseContext.defaultField(), 1.0F);
} }
SimpleQueryParser sqp = new SimpleQueryParser(analyzer, fieldsAndWeights, flags, sqsSettings); SimpleQueryParser sqp = new SimpleQueryParser(analyzer, fieldsAndWeights, flags, sqsSettings);

View File

@ -21,7 +21,6 @@ package org.elasticsearch.index.query;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer; import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.index.*; import org.apache.lucene.index.*;
import org.apache.lucene.index.memory.MemoryIndex; 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.NumericUtils;
import org.apache.lucene.util.automaton.TooComplexToDeterminizeException; import org.apache.lucene.util.automaton.TooComplexToDeterminizeException;
import org.elasticsearch.action.termvectors.*; import org.elasticsearch.action.termvectors.*;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.compress.CompressedXContent;
@ -73,11 +73,12 @@ import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; 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.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.*; 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.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
@ -2379,7 +2380,7 @@ public class SimpleIndexQueryParserTests extends ESSingleNodeTestCase {
IndexQueryParserService queryParser = queryParser(); IndexQueryParserService queryParser = queryParser();
String query = jsonBuilder().startObject().startObject("function_score") String query = jsonBuilder().startObject().startObject("function_score")
.startArray("functions") .startArray("functions")
.startObject().field("weight", 2).field("boost_factor",2).endObject() .startObject().field("weight", 2).field("boost_factor", 2).endObject()
.endArray() .endArray()
.endObject().endObject().string(); .endObject().endObject().string();
try { try {
@ -2480,4 +2481,19 @@ public class SimpleIndexQueryParserTests extends ESSingleNodeTestCase {
assertThat(prefixQuery.getRewriteMethod(), instanceOf(MultiTermQuery.TopTermsBlendedFreqScoringRewrite.class)); 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)));
}
} }

View File

@ -93,11 +93,6 @@ public class SimpleQueryStringIT extends ESIntegTestCase {
searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("spaghetti").field("*body")).get(); searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("spaghetti").field("*body")).get();
assertHitCount(searchResponse, 2l); assertHitCount(searchResponse, 2l);
assertSearchHits(searchResponse, "5", "6"); 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 @Test