Skip to content
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

Error on bad shape relations in runtime fields #60463

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -23,6 +23,7 @@
import java.io.IOException;
import java.time.ZoneId;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES;
Expand Down Expand Up @@ -67,7 +68,8 @@ public final boolean isAggregatable() {

public abstract Query termsQuery(List<?> values, QueryShardContext context);

public abstract Query rangeQuery(
@Override
public final Query rangeQuery(
Object lowerTerm,
Object upperTerm,
boolean includeLower,
Expand All @@ -76,6 +78,22 @@ public abstract Query rangeQuery(
ZoneId timeZone,
DateMathParser parser,
QueryShardContext context
) {
if (relation == ShapeRelation.DISJOINT) {
String message = "Field [%s] of type [%s] with runtime type [%s] does not support DISJOINT ranges";
throw new IllegalArgumentException(String.format(Locale.ROOT, message, name(), typeName(), runtimeType()));
Copy link
Member Author

Choose a reason for hiding this comment

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

I went with this rather funny way of writing the message because it made the formatter happy. Most of the other options I tried caused the formatter to split the message over so many lines it wasn't readable. I don't generally like String.format for these but here it looks best, I think.

}
return rangeQuery(lowerTerm, upperTerm, includeLower, includeUpper, timeZone, parser, context);
}

protected abstract Query rangeQuery(
Object lowerTerm,
Object upperTerm,
boolean includeLower,
boolean includeUpper,
ZoneId timeZone,
DateMathParser parser,
QueryShardContext context
);

public Query fuzzyQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import org.apache.lucene.search.Query;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.DateMathParser;
Expand Down Expand Up @@ -94,7 +93,6 @@ public Query rangeQuery(
Object upperTerm,
boolean includeLower,
boolean includeUpper,
ShapeRelation relation,
ZoneId timeZone,
@Nullable DateMathParser parser,
QueryShardContext context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import com.carrotsearch.hppc.LongHashSet;
import com.carrotsearch.hppc.LongSet;

import org.apache.lucene.search.Query;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.time.DateMathParser;
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType;
Expand Down Expand Up @@ -67,7 +67,6 @@ public Query rangeQuery(
Object upperTerm,
boolean includeLower,
boolean includeUpper,
ShapeRelation relation,
ZoneId timeZone,
DateMathParser parser,
QueryShardContext context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.apache.lucene.search.MultiTermQuery.RewriteMethod;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.time.DateMathParser;
import org.elasticsearch.common.unit.Fuzziness;
Expand Down Expand Up @@ -111,7 +110,6 @@ public Query rangeQuery(
Object upperTerm,
boolean includeLower,
boolean includeUpper,
ShapeRelation relation,
ZoneId timeZone,
DateMathParser parser,
QueryShardContext context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.carrotsearch.hppc.LongSet;

import org.apache.lucene.search.Query;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.time.DateMathParser;
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType;
Expand Down Expand Up @@ -68,7 +67,6 @@ public Query rangeQuery(
Object upperTerm,
boolean includeLower,
boolean includeUpper,
ShapeRelation relation,
ZoneId timeZone,
DateMathParser parser,
QueryShardContext context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package org.elasticsearch.xpack.runtimefields.mapper;

import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.lookup.SearchLookup;
Expand Down Expand Up @@ -83,6 +84,17 @@ protected static QueryShardContext mockContext(boolean allowExpensiveQueries, Ab
return context;
}

public void testRangeQueryWithShapeRelationIsError() throws IOException {
Exception e = expectThrows(
IllegalArgumentException.class,
() -> simpleMappedFieldType().rangeQuery(1, 2, true, true, ShapeRelation.DISJOINT, null, null, null)
);
assertThat(
e.getMessage(),
equalTo("Field [test] of type [runtime_script] with runtime type [" + runtimeType() + "] does not support DISJOINT ranges")
);
}

public void testPhraseQueryIsError() {
assertQueryOnlyOnText("phrase", () -> simpleMappedFieldType().phraseQuery(null, 1, false));
}
Expand Down