|
8 | 8 | package org.opensearch.transport.grpc.proto.response.search; |
9 | 9 |
|
10 | 10 | import com.google.protobuf.ByteString; |
| 11 | +import com.google.protobuf.UnsafeByteOperations; |
11 | 12 | import org.apache.lucene.search.Explanation; |
| 13 | +import org.apache.lucene.util.BytesRef; |
| 14 | +import org.opensearch.common.bytes.BytesArray; |
12 | 15 | import org.opensearch.common.document.DocumentField; |
13 | 16 | import org.opensearch.core.common.bytes.BytesReference; |
14 | 17 | import org.opensearch.core.xcontent.ToXContent; |
@@ -194,7 +197,20 @@ private static void processMetadataFields(SearchHit hit, org.opensearch.protobuf |
194 | 197 | */ |
195 | 198 | private static void processSource(SearchHit hit, org.opensearch.protobufs.Hit.Builder hitBuilder) { |
196 | 199 | if (hit.getSourceRef() != null) { |
197 | | - hitBuilder.setSource(ByteString.copyFrom(BytesReference.toBytes(hit.getSourceRef()))); |
| 200 | + BytesReference sourceRef = hit.getSourceRef(); |
| 201 | + BytesRef bytesRef = sourceRef.toBytesRef(); |
| 202 | + |
| 203 | + if (sourceRef instanceof BytesArray) { |
| 204 | + if (bytesRef.offset == 0 && bytesRef.length == bytesRef.bytes.length) { |
| 205 | + hitBuilder.setSource(UnsafeByteOperations.unsafeWrap(bytesRef.bytes)); |
| 206 | + } else { |
| 207 | + hitBuilder.setSource(UnsafeByteOperations.unsafeWrap( |
| 208 | + bytesRef.bytes, bytesRef.offset, bytesRef.length)); |
| 209 | + } |
| 210 | + } else { |
| 211 | + hitBuilder.setSource(ByteString.copyFrom( |
| 212 | + bytesRef.bytes, bytesRef.offset, bytesRef.length)); |
| 213 | + } |
198 | 214 | } |
199 | 215 | } |
200 | 216 |
|
|
0 commit comments