Skip to content

WIP Add 'version_range' field #59786

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -64,7 +64,7 @@
import org.elasticsearch.index.mapper.ParametrizedFieldMapper;
import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.RangeFieldMapper;
import org.elasticsearch.index.mapper.RangeType;
import org.elasticsearch.index.mapper.CoreRangeType;
import org.elasticsearch.index.mapper.SourceValueFetcher;
import org.elasticsearch.index.mapper.TextSearchInfo;
import org.elasticsearch.index.mapper.ValueFetcher;
Expand Down Expand Up @@ -142,7 +142,7 @@ public PercolatorFieldMapper build(BuilderContext context) {
fieldType.queryBuilderField = queryBuilderField.fieldType();
// Range field is of type ip, because that matches closest with BinaryRange field. Otherwise we would
// have to introduce a new field type...
RangeFieldMapper rangeFieldMapper = createExtractedRangeFieldBuilder(RANGE_FIELD_NAME, RangeType.IP, context);
RangeFieldMapper rangeFieldMapper = createExtractedRangeFieldBuilder(RANGE_FIELD_NAME, CoreRangeType.IP, context);
fieldType.rangeField = rangeFieldMapper.fieldType();
NumberFieldMapper minimumShouldMatchFieldMapper = createMinimumShouldMatchField(context);
fieldType.minimumShouldMatchField = minimumShouldMatchFieldMapper.fieldType();
Expand Down Expand Up @@ -170,7 +170,7 @@ static BinaryFieldMapper createQueryBuilderFieldBuilder(BuilderContext context)
return builder.build(context);
}

static RangeFieldMapper createExtractedRangeFieldBuilder(String name, RangeType rangeType, BuilderContext context) {
static RangeFieldMapper createExtractedRangeFieldBuilder(String name, CoreRangeType rangeType, BuilderContext context) {
RangeFieldMapper.Builder builder = new RangeFieldMapper.Builder(name, rangeType, true);
// For now no doc values, because in processQuery(...) only the Lucene range fields get added:
builder.docValues(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public BinaryRange(String name, byte[] encodedRange) {
}

/**
* Create a query for matching indexed ip ranges that {@code INTERSECT} the defined range.
* Create a query for matching indexed ranges that {@code INTERSECT} the defined range.
* @param field field name. must not be null.
* @param encodedRange Encoded range
* @return query for matching intersecting encoded ranges (overlap, within, crosses, or contains)
Expand All @@ -59,6 +59,14 @@ public static Query newIntersectsQuery(String field, byte[] encodedRange) {
return newRelationQuery(field, encodedRange, RangeFieldQuery.QueryType.INTERSECTS);
}

public static Query newContainsQuery(String field, byte[] encodedRange) {
return newRelationQuery(field, encodedRange, RangeFieldQuery.QueryType.CONTAINS);
}

public static Query newWithinQuery(String field, byte[] encodedRange) {
return newRelationQuery(field, encodedRange, RangeFieldQuery.QueryType.WITHIN);
}

static Query newRelationQuery(String field, byte[] encodedRange, RangeFieldQuery.QueryType relation) {
return new RangeFieldQuery(field, encodedRange, 1, relation) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.apache.lucene.search.Weight;
import org.apache.lucene.store.ByteArrayDataInput;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.index.mapper.RangeType;
import org.elasticsearch.index.mapper.CoreRangeType;

import java.io.IOException;
import java.util.Objects;
Expand All @@ -41,13 +41,13 @@ public final class BinaryDocValuesRangeQuery extends Query {

private final String fieldName;
private final QueryType queryType;
private final RangeType.LengthType lengthType;
private final CoreRangeType.LengthType lengthType;
private final BytesRef from;
private final BytesRef to;
private final Object originalFrom;
private final Object originalTo;

public BinaryDocValuesRangeQuery(String fieldName, QueryType queryType, RangeType.LengthType lengthType,
public BinaryDocValuesRangeQuery(String fieldName, QueryType queryType, CoreRangeType.LengthType lengthType,
BytesRef from, BytesRef to,
Object originalFrom, Object originalTo) {
this.fieldName = fieldName;
Expand Down Expand Up @@ -87,11 +87,13 @@ public boolean matches() throws IOException {
int offset = in.getPosition();
for (int i = 0; i < numRanges; i++) {
int length = lengthType.readLength(bytes, offset);
offset += lengthType.advanceBy();
otherFrom.offset = offset;
otherFrom.length = length;
offset += length;

length = lengthType.readLength(bytes, offset);
offset += lengthType.advanceBy();
otherTo.offset = offset;
otherTo.length = length;
offset += length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,13 @@ private static boolean[] grow(boolean[] array, int minSize) {

}

abstract static class BinaryScriptDocValues<T> extends ScriptDocValues<T> {
public abstract static class BinaryScriptDocValues<T> extends ScriptDocValues<T> {

private final SortedBinaryDocValues in;
protected BytesRefBuilder[] values = new BytesRefBuilder[0];
protected int count;

BinaryScriptDocValues(SortedBinaryDocValues in) {
public BinaryScriptDocValues(SortedBinaryDocValues in) {
this.in = in;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static BytesRef encodeIPRanges(Set<RangeFieldMapper.Range> ranges) throws IOExce
}

static List<RangeFieldMapper.Range> decodeIPRanges(BytesRef encodedRanges) {
return decodeRanges(encodedRanges, RangeType.IP, BinaryRangeUtil::decodeIP);
return decodeRanges(encodedRanges, CoreRangeType.IP, BinaryRangeUtil::decodeIP);
}

private static InetAddress decodeIP(byte[] bytes, int offset, int length) {
Expand Down Expand Up @@ -83,7 +83,7 @@ static BytesRef encodeLongRanges(Set<RangeFieldMapper.Range> ranges) throws IOEx
}

static List<RangeFieldMapper.Range> decodeLongRanges(BytesRef encodedRanges) {
return decodeRanges(encodedRanges, RangeType.LONG,
return decodeRanges(encodedRanges, CoreRangeType.LONG,
BinaryRangeUtil::decodeLong);
}

Expand All @@ -106,19 +106,19 @@ static BytesRef encodeDoubleRanges(Set<RangeFieldMapper.Range> ranges) throws IO
}

static List<RangeFieldMapper.Range> decodeDoubleRanges(BytesRef encodedRanges) {
return decodeRanges(encodedRanges, RangeType.DOUBLE,
return decodeRanges(encodedRanges, CoreRangeType.DOUBLE,
BinaryRangeUtil::decodeDouble);
}

static List<RangeFieldMapper.Range> decodeFloatRanges(BytesRef encodedRanges) {
return decodeRanges(encodedRanges, RangeType.FLOAT,
return decodeRanges(encodedRanges, CoreRangeType.FLOAT,
BinaryRangeUtil::decodeFloat);
}

static List<RangeFieldMapper.Range> decodeRanges(BytesRef encodedRanges, RangeType rangeType,
TriFunction<byte[], Integer, Integer, Object> decodeBytes) {

RangeType.LengthType lengthType = rangeType.lengthType;
CoreRangeType.LengthType lengthType = rangeType.getLengthType();
ByteArrayDataInput in = new ByteArrayDataInput();
in.reset(encodedRanges.bytes, encodedRanges.offset, encodedRanges.length);
int numRanges = in.readVInt();
Expand All @@ -130,11 +130,11 @@ static List<RangeFieldMapper.Range> decodeRanges(BytesRef encodedRanges, RangeTy
for (int i = 0; i < numRanges; i++) {
int length = lengthType.readLength(bytes, offset);
Object from = decodeBytes.apply(bytes, offset, length);
offset += length;
offset += length + lengthType.advanceBy();

length = lengthType.readLength(bytes, offset);
Object to = decodeBytes.apply(bytes, offset, length);
offset += length;
offset += length + lengthType.advanceBy();
// TODO: Support for exclusive ranges, pending resolution of #40601
RangeFieldMapper.Range decodedRange = new RangeFieldMapper.Range(rangeType, from, to, true, true);
ranges.add(decodedRange);
Expand Down
Loading