Skip to content

Commit 37634b9

Browse files
committed
Store offsets in sorted doc values field instead of binary doc values field
1 parent 5b6b05c commit 37634b9

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
package org.elasticsearch.index.mapper;
1111

12-
import org.apache.lucene.document.BinaryDocValuesField;
12+
import org.apache.lucene.document.SortedDocValuesField;
1313
import org.apache.lucene.util.BitUtil;
1414
import org.elasticsearch.common.io.stream.BytesStreamOutput;
1515

@@ -65,7 +65,7 @@ void addToLuceneDocument(DocumentParserContext context) throws IOException {
6565
for (int ord : offsetToOrd) {
6666
streamOutput.writeVInt(BitUtil.zigZagEncode(ord));
6767
}
68-
context.doc().add(new BinaryDocValuesField(fieldName, streamOutput.bytes().toBytesRef()));
68+
context.doc().add(new SortedDocValuesField(fieldName, streamOutput.bytes().toBytesRef()));
6969
}
7070
}
7171
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.index.BinaryDocValues;
1313
import org.apache.lucene.index.DocValues;
1414
import org.apache.lucene.index.LeafReader;
15+
import org.apache.lucene.index.SortedDocValues;
1516
import org.apache.lucene.index.SortedSetDocValues;
1617
import org.apache.lucene.util.BitUtil;
1718
import org.apache.lucene.util.BytesRef;
@@ -46,7 +47,7 @@ public String fieldName() {
4647
@Override
4748
public DocValuesLoader docValuesLoader(LeafReader leafReader, int[] docIdsInLeaf) throws IOException {
4849
SortedSetDocValues valueDocValues = DocValues.getSortedSet(leafReader, name);
49-
BinaryDocValues offsetDocValues = DocValues.getBinary(leafReader, offsetsFieldName);
50+
SortedDocValues offsetDocValues = DocValues.getSorted(leafReader, offsetsFieldName);
5051

5152
return docValues = new DocValuesWithOffsetsLoader(valueDocValues, offsetDocValues);
5253
}
@@ -77,15 +78,15 @@ public void write(XContentBuilder b) throws IOException {
7778
}
7879

7980
static final class DocValuesWithOffsetsLoader implements DocValuesLoader {
80-
private final BinaryDocValues offsetDocValues;
81+
private final SortedDocValues offsetDocValues;
8182
private final SortedSetDocValues valueDocValues;
8283
private final ByteArrayStreamInput scratch = new ByteArrayStreamInput();
8384

8485
private boolean hasValue;
8586
private boolean hasOffset;
8687
private int[] offsetToOrd;
8788

88-
DocValuesWithOffsetsLoader(SortedSetDocValues valueDocValues, BinaryDocValues offsetDocValues) {
89+
DocValuesWithOffsetsLoader(SortedSetDocValues valueDocValues, SortedDocValues offsetDocValues) {
8990
this.valueDocValues = valueDocValues;
9091
this.offsetDocValues = offsetDocValues;
9192
}
@@ -96,7 +97,8 @@ public boolean advanceToDoc(int docId) throws IOException {
9697
hasOffset = offsetDocValues.advanceExact(docId);
9798
if (hasValue || hasOffset) {
9899
if (hasOffset) {
99-
var encodedValue = offsetDocValues.binaryValue();
100+
int offsetOrd = offsetDocValues.ordValue();
101+
var encodedValue = offsetDocValues.lookupOrd(offsetOrd);
100102
scratch.reset(encodedValue.bytes, encodedValue.offset, encodedValue.length);
101103
offsetToOrd = parseOffsetArray(scratch);
102104
} else {

server/src/test/java/org/elasticsearch/index/mapper/SyntheticSourceNativeArrayIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private void verifySyntheticArray(Object[][] arrays, XContentBuilder mapping, In
186186
assertThat(storedFieldNames, contains(expectedStoredFields));
187187
}
188188
var fieldInfo = FieldInfos.getMergedFieldInfos(reader).fieldInfo("field.offsets");
189-
assertThat(fieldInfo.getDocValuesType(), equalTo(DocValuesType.BINARY));
189+
assertThat(fieldInfo.getDocValuesType(), equalTo(DocValuesType.SORTED));
190190
}
191191
}
192192

@@ -324,7 +324,7 @@ private void verifySyntheticArrayInObject(List<Object[]> documents) throws IOExc
324324
assertThat(storedFieldNames, contains("_id"));
325325
}
326326
var fieldInfo = FieldInfos.getMergedFieldInfos(reader).fieldInfo("object.field.offsets");
327-
assertThat(fieldInfo.getDocValuesType(), equalTo(DocValuesType.BINARY));
327+
assertThat(fieldInfo.getDocValuesType(), equalTo(DocValuesType.SORTED));
328328
}
329329
}
330330

0 commit comments

Comments
 (0)