Skip to content

Fix simple_query_string on invalid input #28219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SynonymQuery;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.AbstractQueryBuilder;
Expand Down Expand Up @@ -86,11 +87,11 @@ private Analyzer getAnalyzer(MappedFieldType ft) {
}

/**
* Rethrow the runtime exception, unless the lenient flag has been set, returns null
* Rethrow the runtime exception, unless the lenient flag has been set, returns {@link MatchNoDocsQuery}
*/
private Query rethrowUnlessLenient(RuntimeException e) {
if (settings.lenient()) {
return null;
return Queries.newMatchNoDocsQuery("failed query, caused by " + e.getMessage());
Copy link
Member

@cbuescher cbuescher Jan 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you update the comment to reflect that we return MatchNoDocs instead of null now?

}
throw e;
}
Expand All @@ -115,7 +116,7 @@ public Query newDefaultQuery(String text) {
try {
return queryBuilder.parse(MultiMatchQueryBuilder.Type.MOST_FIELDS, weights, text, null);
} catch (IOException e) {
return rethrowUnlessLenient(new IllegalArgumentException(e.getMessage()));
return rethrowUnlessLenient(new IllegalStateException(e.getMessage()));
}
}

Expand All @@ -135,7 +136,7 @@ public Query newFuzzyQuery(String text, int fuzziness) {
settings.fuzzyMaxExpansions, settings.fuzzyTranspositions);
disjuncts.add(wrapWithBoost(query, entry.getValue()));
} catch (RuntimeException e) {
rethrowUnlessLenient(e);
disjuncts.add(rethrowUnlessLenient(e));
}
}
if (disjuncts.size() == 1) {
Expand All @@ -156,7 +157,7 @@ public Query newPhraseQuery(String text, int slop) {
}
return queryBuilder.parse(MultiMatchQueryBuilder.Type.PHRASE, phraseWeights, text, null);
} catch (IOException e) {
return rethrowUnlessLenient(new IllegalArgumentException(e.getMessage()));
return rethrowUnlessLenient(new IllegalStateException(e.getMessage()));
} finally {
queryBuilder.setPhraseSlop(0);
}
Expand Down Expand Up @@ -184,7 +185,7 @@ public Query newPrefixQuery(String text) {
disjuncts.add(wrapWithBoost(query, entry.getValue()));
}
} catch (RuntimeException e) {
return rethrowUnlessLenient(e);
disjuncts.add(rethrowUnlessLenient(e));
}
}
if (disjuncts.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@
import org.elasticsearch.test.AbstractQueryTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.either;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
Expand Down Expand Up @@ -607,6 +610,21 @@ public void testToFuzzyQuery() throws Exception {
assertEquals(expected, query);
}

public void testLenientToPrefixQuery() throws Exception {
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);

Query query = new SimpleQueryStringBuilder("t*")
.field(DATE_FIELD_NAME)
.field(STRING_FIELD_NAME)
.lenient(true)
.toQuery(createShardContext());
List<Query> expectedQueries = new ArrayList<>();
expectedQueries.add(new MatchNoDocsQuery(""));
expectedQueries.add(new PrefixQuery(new Term(STRING_FIELD_NAME, "t")));
DisjunctionMaxQuery expected = new DisjunctionMaxQuery(expectedQueries, 1.0f);
assertEquals(expected, query);
}

private static IndexMetaData newIndexMeta(String name, Settings oldIndexSettings, Settings indexSettings) {
Settings build = Settings.builder().put(oldIndexSettings)
.put(indexSettings)
Expand Down