Skip to content
Merged
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 @@ -633,7 +633,12 @@ public String get(int index) {

public BytesRef getBytesValue() {
if (size() > 0) {
return values[0].get();
/**
* We need to make a copy here because {@link BinaryScriptDocValues} might reuse the
* returned value and the same instance might be used to
* return values from multiple documents.
**/
return values[0].toBytesRef();
} else {
return null;
}
Expand All @@ -658,14 +663,19 @@ public BytesRefs(SortedBinaryDocValues in) {

@Override
public BytesRef get(int index) {
return values[index].get();
/**
* We need to make a copy here because {@link BinaryScriptDocValues} might reuse the
* returned value and the same instance might be used to
* return values from multiple documents.
**/
return values[index].toBytesRef();
}

public BytesRef getValue() {
if (count == 0) {
return new BytesRef();
}
return values[0].get();
return values[0].toBytesRef();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public void testDocValue() throws Exception {

final DocumentMapper mapper = mapperService.documentMapperParser().parse("test", new CompressedXContent(mapping));


List<BytesRef> bytesList1 = new ArrayList<>(2);
bytesList1.add(randomBytes());
bytesList1.add(randomBytes());
Expand Down Expand Up @@ -123,22 +122,26 @@ public void testDocValue() throws Exception {
// Test whether ScriptDocValues.BytesRefs makes a deepcopy
fieldData = indexFieldData.load(reader);
ScriptDocValues<?> scriptValues = fieldData.getScriptValues();
scriptValues.setNextDocId(0);
assertEquals(2, scriptValues.size());
assertEquals(bytesList1.get(0), scriptValues.get(0));
assertEquals(bytesList1.get(1), scriptValues.get(1));

scriptValues.setNextDocId(1);
assertEquals(1, scriptValues.size());
assertEquals(bytes1, scriptValues.get(0));

scriptValues.setNextDocId(2);
assertEquals(0, scriptValues.size());

scriptValues.setNextDocId(3);
assertEquals(2, scriptValues.size());
assertEquals(bytesList2.get(0), scriptValues.get(0));
assertEquals(bytesList2.get(1), scriptValues.get(1));
Object[][] retValues = new BytesRef[4][0];
for (int i = 0; i < 4; i++) {
scriptValues.setNextDocId(i);
retValues[i] = new BytesRef[scriptValues.size()];
for (int j = 0; j < retValues[i].length; j++) {
retValues[i][j] = scriptValues.get(j);
}
}
assertEquals(2, retValues[0].length);
assertEquals(bytesList1.get(0), retValues[0][0]);
assertEquals(bytesList1.get(1), retValues[0][1]);

assertEquals(1, retValues[1].length);
assertEquals(bytes1, retValues[1][0]);

assertEquals(0, retValues[2].length);

assertEquals(2, retValues[3].length);
assertEquals(bytesList2.get(0), retValues[3][0]);
assertEquals(bytesList2.get(1), retValues[3][1]);
}

private static BytesRef randomBytes() {
Expand Down