Skip to content

Commit ab28946

Browse files
committed
Moved ignore values doc value field fetcher inside of existing fetcher function
1 parent 9bac251 commit ab28946

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldMapper.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
import org.apache.lucene.search.PrefixQuery;
3030
import org.apache.lucene.search.Query;
3131
import org.apache.lucene.search.TermQuery;
32-
import org.apache.lucene.store.ByteArrayDataInput;
3332
import org.apache.lucene.util.BytesRef;
3433
import org.apache.lucene.util.IOFunction;
3534
import org.elasticsearch.common.CheckedIntFunction;
35+
import org.elasticsearch.common.io.stream.ByteArrayStreamInput;
3636
import org.elasticsearch.common.lucene.Lucene;
3737
import org.elasticsearch.common.text.UTF8DecodingReader;
3838
import org.elasticsearch.common.unit.Fuzziness;
@@ -786,31 +786,24 @@ protected void writeValue(Object value, XContentBuilder b) throws IOException {
786786
return fieldLoader;
787787
}
788788

789-
790789
/**
791790
* A wrapper around {@link BinaryDocValues} that exposes some quality of life functions.
792791
*/
793792
private static class CustomBinaryDocValues extends AbstractBinaryDocValues {
794793

795794
private final BinaryDocValues binaryDocValues;
795+
private final ByteArrayStreamInput stream;
796796

797-
private ByteArrayDataInput data;
798797
private int docValueCount = 0;
799798

800799
CustomBinaryDocValues(BinaryDocValues binaryDocValues) {
801800
this.binaryDocValues = binaryDocValues;
801+
this.stream = new ByteArrayStreamInput();
802802
}
803803

804-
public BytesRef nextValue() {
805-
// get the length of the value
806-
int length = data.readVInt();
807-
808-
// read that many bytes from the underlying bytes array
809-
// the read will automatically move the offset to the next value
810-
byte[] valueBytes = new byte[length];
811-
data.readBytes(valueBytes, 0, length);
812-
813-
return new BytesRef(valueBytes);
804+
public BytesRef nextValue() throws IOException {
805+
// this function already knows how to decode the underlying bytes array, so no need to explicitly call VInt()
806+
return stream.readBytesRef();
814807
}
815808

816809
@Override
@@ -823,8 +816,8 @@ public boolean advanceExact(int docId) throws IOException {
823816
// if document has a value, read underlying bytes
824817
if (binaryDocValues.advanceExact(docId)) {
825818
BytesRef docValuesBytes = binaryDocValues.binaryValue();
826-
data = new ByteArrayDataInput(docValuesBytes.bytes, docValuesBytes.offset, docValuesBytes.length);
827-
docValueCount = data.readVInt();
819+
stream.reset(docValuesBytes.bytes, docValuesBytes.offset, docValuesBytes.length);
820+
docValueCount = stream.readVInt();
828821
return true;
829822
}
830823

server/src/main/java/org/elasticsearch/index/mapper/BinaryDocValuesSyntheticFieldLoaderLayer.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
import org.apache.lucene.index.BinaryDocValues;
1313
import org.apache.lucene.index.LeafReader;
14-
import org.apache.lucene.store.ByteArrayDataInput;
1514
import org.apache.lucene.util.BytesRef;
15+
import org.elasticsearch.common.io.stream.ByteArrayStreamInput;
1616
import org.elasticsearch.xcontent.XContentBuilder;
1717

1818
import java.io.IOException;
@@ -23,11 +23,12 @@ public final class BinaryDocValuesSyntheticFieldLoaderLayer implements Composite
2323

2424
// the binary doc values for a document are all encoded in a single binary array, which this stream knows how to read
2525
// the doc values in the array take the form of [doc value count][length of value 1][value 1][length of value 2][value 2]...
26-
private final ByteArrayDataInput data = new ByteArrayDataInput();
26+
private final ByteArrayStreamInput stream;
2727
private int valueCount;
2828

2929
public BinaryDocValuesSyntheticFieldLoaderLayer(String fieldName) {
3030
this.fieldName = fieldName;
31+
this.stream = new ByteArrayStreamInput();
3132
}
3233

3334
@Override
@@ -49,8 +50,8 @@ public DocValuesLoader docValuesLoader(LeafReader leafReader, int[] docIdsInLeaf
4950

5051
// otherwise, extract the doc values into a stream to later read from
5152
BytesRef docValuesBytes = docValues.binaryValue();
52-
data.reset(docValuesBytes.bytes, docValuesBytes.offset, docValuesBytes.length);
53-
valueCount = data.readVInt();
53+
stream.reset(docValuesBytes.bytes, docValuesBytes.offset, docValuesBytes.length);
54+
valueCount = stream.readVInt();
5455

5556
return hasValue();
5657
};
@@ -59,16 +60,9 @@ public DocValuesLoader docValuesLoader(LeafReader leafReader, int[] docIdsInLeaf
5960
@Override
6061
public void write(XContentBuilder b) throws IOException {
6162
for (int i = 0; i < valueCount; i++) {
62-
// read the length of the value
63-
int length = data.readVInt();
64-
65-
// read that many bytes from the input
66-
// the read will automatically move the offset to the next value
67-
byte[] valueBytes = new byte[length];
68-
data.readBytes(valueBytes, 0, length);
69-
70-
// finally, write those bytes into XContentBuilder
71-
b.value(new BytesRef(valueBytes).utf8ToString());
63+
// this function already knows how to decode the underlying bytes array, so no need to explicitly call VInt()
64+
BytesRef valueBytes = stream.readBytesRef();
65+
b.value(valueBytes.utf8ToString());
7266
}
7367
}
7468

0 commit comments

Comments
 (0)