Skip to content

Commit

Permalink
simple query string: remove (not working) support for alternate formats
Browse files Browse the repository at this point in the history
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
  • Loading branch information
javanna committed Aug 11, 2015
1 parent a172c50 commit db72199
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
Expand Up @@ -27,10 +27,8 @@
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;
Expand All @@ -55,7 +53,7 @@
* <li>'{@code ~}N' at the end of phrases specifies near/slop query: <tt>"term1 term2"~5</tt>
* </ul>
* <p/>
* See: {@link XSimpleQueryParser} for more information.
* See: {@link SimpleQueryParser} for more information.
* <p/>
* This query supports these options:
* <p/>
Expand All @@ -75,7 +73,7 @@ public class SimpleQueryStringParser implements QueryParser {
public static final String NAME = "simple_query_string";

@Inject
public SimpleQueryStringParser(Settings settings) {
public SimpleQueryStringParser() {

}

Expand All @@ -92,7 +90,6 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
String queryBody = null;
float boost = 1.0f;
String queryName = null;
String field = null;
String minimumShouldMatch = null;
Map<String, Float> fieldsAndWeights = null;
BooleanClause.Occur defaultOperator = null;
Expand Down Expand Up @@ -141,9 +138,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
}
} 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)) {
Expand All @@ -155,8 +150,6 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
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)) {
Expand Down Expand Up @@ -201,24 +194,14 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
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) {
analyzer = parseContext.mapperService().searchAnalyzer();
}

if (fieldsAndWeights == null) {
fieldsAndWeights = Collections.singletonMap(field, 1.0F);
fieldsAndWeights = Collections.singletonMap(parseContext.defaultField(), 1.0F);
}
SimpleQueryParser sqp = new SimpleQueryParser(analyzer, fieldsAndWeights, flags, sqsSettings);

Expand Down
Expand Up @@ -21,7 +21,6 @@

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;
Expand All @@ -39,6 +38,7 @@
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;
Expand Down Expand Up @@ -73,11 +73,12 @@
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.*;

Expand Down Expand Up @@ -2379,7 +2380,7 @@ public void testProperErrorMessagesForMisplacedWeightsAndFunctions() throws IOEx
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 {
Expand Down Expand Up @@ -2480,4 +2481,19 @@ public void testBlendedRewriteMethod() throws IOException {
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)));
}
}
Expand Up @@ -93,11 +93,6 @@ public void testSimpleQueryString() throws ExecutionException, InterruptedExcept
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
Expand Down

0 comments on commit db72199

Please sign in to comment.