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

Fix range queries on _type field for singe type indices #31756

Merged
merged 5 commits into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -35,6 +35,8 @@
import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand Down Expand Up @@ -90,6 +92,8 @@ public MetadataFieldMapper getDefault(MappedFieldType fieldType, ParserContext c

static final class TypeFieldType extends StringFieldType {

private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger(ESLoggerFactory.getLogger(TypeFieldType.class));

TypeFieldType() {
}

Expand Down Expand Up @@ -154,6 +158,36 @@ public Query termsQuery(List<?> values, QueryShardContext context) {
}
}

@Override
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) {
DEPRECATION_LOGGER.deprecatedAndMaybeLog("range_single_type",
"Running [range] query on [_type] field for an index with a single type. As types are deprecated, this functionality will be removed in future releases.");
if ((lowerTerm != null && lowerTerm instanceof BytesRef == false)) {
throw new IllegalArgumentException("lower term should be BytesRef but was: " + lowerTerm.getClass().getName());
}
if ((upperTerm != null && upperTerm instanceof BytesRef == false)) {
throw new IllegalArgumentException("upper term should be BytesRef but was: " + upperTerm.getClass().getName());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Conversion to bytesref is done elsewhere with indexedValueForSearch. I'm unsure of the impact of rejecting anything but bytesrefs.


Query result = new MatchAllDocsQuery();
String type = context.getMapperService().documentMapper().type();
if (type != null) {
BytesRef typeBytes = new BytesRef(type);
if (lowerTerm != null) {
int comp = ((BytesRef) lowerTerm).compareTo(typeBytes);
if (comp > 0 || (comp == 0 && includeLower == false)) {
result = new MatchNoDocsQuery("[_type] was less then lower bound of range");
}
}
if (upperTerm != null) {
int comp = ((BytesRef) upperTerm).compareTo(typeBytes);
if (comp < 0 || (comp == 0 && includeUpper == false)) {
result = new MatchNoDocsQuery("[_type] was higher then upper bound of range");
}
}
}
return result;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1818,4 +1818,48 @@ public void testRangeQueryRangeFields_24744() throws Exception {
SearchResponse searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 1);
}

public void testRangeQueryTypeField_31476() throws Exception {
assertAcked(prepareCreate("test").addMapping("foo", "field", "type=keyword"));

client().prepareIndex("test", "foo", "1").setSource("field", "value").get();
refresh();

RangeQueryBuilder range = new RangeQueryBuilder("_type").from("ape").to("zebra");
SearchResponse searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 1);

range = new RangeQueryBuilder("_type").from("monkey").to("zebra");
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 0);

range = new RangeQueryBuilder("_type").from("ape").to("donkey");
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 0);

range = new RangeQueryBuilder("_type").from("ape").to("foo").includeUpper(false);
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 0);

range = new RangeQueryBuilder("_type").from("ape").to("foo").includeUpper(true);
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 1);

range = new RangeQueryBuilder("_type").from("foo").to("zebra").includeLower(false);
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 0);

range = new RangeQueryBuilder("_type").from("foo").to("zebra").includeLower(true);
searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 1);

Exception ex = expectThrows(Exception.class, () -> client().prepareSearch("test")
.setQuery(new RangeQueryBuilder("_type").from(123L).to("zebra").includeLower(true)).get());
assertEquals("lower term should be BytesRef but was: java.lang.Long", ex.getCause().getCause().getMessage());

ex = expectThrows(Exception.class, () -> client().prepareSearch("test")
.setQuery(new RangeQueryBuilder("_type").from("ape").to(true).includeLower(true)).get());
assertEquals("upper term should be BytesRef but was: java.lang.Boolean", ex.getCause().getCause().getMessage());
}

}