-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,10 @@ | |
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.TermInSetQuery; | ||
import org.apache.lucene.search.TermQuery; | ||
import org.apache.lucene.search.TermRangeQuery; | ||
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; | ||
|
@@ -90,6 +93,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() { | ||
} | ||
|
||
|
@@ -154,6 +159,33 @@ public Query termsQuery(List<?> values, QueryShardContext context) { | |
} | ||
} | ||
|
||
@Override | ||
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) { | ||
if (hasDocValues()) { | ||
return new TermRangeQuery(name(), lowerTerm == null ? null : indexedValueForSearch(lowerTerm), | ||
upperTerm == null ? null : indexedValueForSearch(upperTerm), includeLower, includeUpper); | ||
} else { | ||
// this means the index has a single type and the type field is implicit | ||
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."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we really need the deprecation, but since it was mentioned at some point I added it for discussion purposes. |
||
String type = context.getMapperService().documentMapper().type(); | ||
Query result = new MatchAllDocsQuery(); | ||
if (lowerTerm != null && lowerTerm instanceof BytesRef) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we shouldn't be lenient in case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering the same, do we want to throw an IAE in those cases? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would call |
||
int comp = type.compareTo(((BytesRef) lowerTerm).utf8ToString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if there is a better way of comparing BytesRef instances, maybe taking into account different lexicographical orderings? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BytesRef implements
|
||
if (comp < 0 || (comp == 0 && includeLower == false)) { | ||
result = new MatchNoDocsQuery("[_type] was less then lower bound of range"); | ||
} | ||
} | ||
if (upperTerm != null && upperTerm instanceof BytesRef) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we shouldn't be lenient in case |
||
int comp = type.compareTo(((BytesRef) upperTerm).utf8ToString()); | ||
if (comp > 0 || (comp == 0 && includeUpper == false)) { | ||
result = new MatchNoDocsQuery("[_type] was higher then upper bound of range"); | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't need this
if
block, do we? All 6.x and 7.x indices have a single type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we still support multi-type indices that have been created in 5.x on 6.x? I will remove this for the PR against master but I think we need it for 6.x in that case. Can you confirm?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍