Merge pull request #14910 from camilojd/fix-better-exception-query-num-field-regex
Return a better exception message when `regexp` query is used on a numeric field
This commit is contained in:
commit
9e4a0cba0b
|
@ -34,6 +34,7 @@ import org.elasticsearch.common.unit.Fuzziness;
|
||||||
import org.elasticsearch.index.analysis.NamedAnalyzer;
|
import org.elasticsearch.index.analysis.NamedAnalyzer;
|
||||||
import org.elasticsearch.index.fielddata.FieldDataType;
|
import org.elasticsearch.index.fielddata.FieldDataType;
|
||||||
import org.elasticsearch.index.query.QueryShardContext;
|
import org.elasticsearch.index.query.QueryShardContext;
|
||||||
|
import org.elasticsearch.index.query.QueryShardException;
|
||||||
import org.elasticsearch.index.similarity.SimilarityProvider;
|
import org.elasticsearch.index.similarity.SimilarityProvider;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -481,6 +482,10 @@ public abstract class MappedFieldType extends FieldType {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Query regexpQuery(String value, int flags, int maxDeterminizedStates, @Nullable MultiTermQuery.RewriteMethod method, @Nullable QueryShardContext context) {
|
public Query regexpQuery(String value, int flags, int maxDeterminizedStates, @Nullable MultiTermQuery.RewriteMethod method, @Nullable QueryShardContext context) {
|
||||||
|
if (numericType() != null) {
|
||||||
|
throw new QueryShardException(context, "Cannot use regular expression to filter numeric field [" + names.fullName + "]");
|
||||||
|
}
|
||||||
|
|
||||||
RegexpQuery query = new RegexpQuery(createTerm(value), flags, maxDeterminizedStates);
|
RegexpQuery query = new RegexpQuery(createTerm(value), flags, maxDeterminizedStates);
|
||||||
if (method != null) {
|
if (method != null) {
|
||||||
query.setRewriteMethod(method);
|
query.setRewriteMethod(method);
|
||||||
|
|
|
@ -46,6 +46,7 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFail
|
||||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
||||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
|
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
public class SimpleSearchIT extends ESIntegTestCase {
|
public class SimpleSearchIT extends ESIntegTestCase {
|
||||||
public void testSearchNullIndex() {
|
public void testSearchNullIndex() {
|
||||||
|
@ -336,6 +337,18 @@ public class SimpleSearchIT extends ESIntegTestCase {
|
||||||
.setFrom(DefaultSearchContext.Defaults.MAX_RESULT_WINDOW * 10).get(), 1);
|
.setFrom(DefaultSearchContext.Defaults.MAX_RESULT_WINDOW * 10).get(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testQueryNumericFieldWithRegex() throws Exception {
|
||||||
|
createIndex("idx");
|
||||||
|
indexRandom(true, client().prepareIndex("idx", "type").setSource("num", 34));
|
||||||
|
|
||||||
|
try {
|
||||||
|
client().prepareSearch("idx").setQuery(QueryBuilders.regexpQuery("num", "34")).get();
|
||||||
|
fail("SearchPhaseExecutionException should have been thrown");
|
||||||
|
} catch (SearchPhaseExecutionException ex) {
|
||||||
|
assertThat(ex.getCause().getCause().getMessage(), equalTo("Cannot use regular expression to filter numeric field [num]"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void assertWindowFails(SearchRequestBuilder search) {
|
private void assertWindowFails(SearchRequestBuilder search) {
|
||||||
try {
|
try {
|
||||||
search.get();
|
search.get();
|
||||||
|
|
Loading…
Reference in New Issue