|
| 1 | +package io.qdrant.client; |
| 2 | + |
| 3 | +import io.qdrant.client.grpc.Points.DenseVector; |
| 4 | +import io.qdrant.client.grpc.Points.MultiDenseVector; |
| 5 | +import io.qdrant.client.grpc.Points.SparseIndices; |
| 6 | +import io.qdrant.client.grpc.Points.SparseVector; |
| 7 | +import io.qdrant.client.grpc.Points.VectorOutput; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +/** Helper methods for extracting vector data from {@link VectorOutput}. */ |
| 12 | +public final class VectorOutputHelper { |
| 13 | + private VectorOutputHelper() {} |
| 14 | + |
| 15 | + /** |
| 16 | + * Returns the DenseVector from the VectorOutput. |
| 17 | + * |
| 18 | + * @param vectorOutput the VectorOutput to extract from |
| 19 | + * @return the DenseVector if available, null otherwise |
| 20 | + */ |
| 21 | + public static DenseVector getDenseVector(VectorOutput vectorOutput) { |
| 22 | + if (vectorOutput == null) { |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + List<Float> data = vectorOutput.getDataList(); |
| 27 | + if (!data.isEmpty()) { |
| 28 | + return DenseVector.newBuilder().addAllData(data).build(); |
| 29 | + } |
| 30 | + |
| 31 | + if (vectorOutput.hasDense()) { |
| 32 | + return vectorOutput.getDense(); |
| 33 | + } |
| 34 | + |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Returns the SparseVector from the VectorOutput. |
| 40 | + * |
| 41 | + * @param vectorOutput the VectorOutput to extract from |
| 42 | + * @return the SparseVector if available, null otherwise |
| 43 | + */ |
| 44 | + public static SparseVector getSparseVector(VectorOutput vectorOutput) { |
| 45 | + if (vectorOutput == null) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + List<Float> data = vectorOutput.getDataList(); |
| 50 | + if (!data.isEmpty()) { |
| 51 | + if (vectorOutput.hasIndices()) { |
| 52 | + SparseIndices indices = vectorOutput.getIndices(); |
| 53 | + return SparseVector.newBuilder() |
| 54 | + .addAllValues(data) |
| 55 | + .addAllIndices(indices.getDataList()) |
| 56 | + .build(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (vectorOutput.hasSparse()) { |
| 61 | + return vectorOutput.getSparse(); |
| 62 | + } |
| 63 | + |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns the MultiDenseVector from the VectorOutput. |
| 69 | + * |
| 70 | + * @param vectorOutput the VectorOutput to extract from |
| 71 | + * @return the MultiDenseVector if available, null otherwise |
| 72 | + */ |
| 73 | + public static MultiDenseVector getMultiVector(VectorOutput vectorOutput) { |
| 74 | + if (vectorOutput == null) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + List<Float> data = vectorOutput.getDataList(); |
| 79 | + if (!data.isEmpty()) { |
| 80 | + int vectorsCount = vectorOutput.getVectorsCount(); |
| 81 | + if (vectorsCount > 0) { |
| 82 | + int vectorSize = data.size() / vectorsCount; |
| 83 | + List<DenseVector> vectors = new ArrayList<>(vectorsCount); |
| 84 | + |
| 85 | + for (int i = 0; i < vectorsCount; i++) { |
| 86 | + int start = i * vectorSize; |
| 87 | + int end = start + vectorSize; |
| 88 | + List<Float> vectorData = data.subList(start, end); |
| 89 | + |
| 90 | + vectors.add(DenseVector.newBuilder().addAllData(vectorData).build()); |
| 91 | + } |
| 92 | + |
| 93 | + return MultiDenseVector.newBuilder().addAllVectors(vectors).build(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (vectorOutput.hasMultiDense()) { |
| 98 | + return vectorOutput.getMultiDense(); |
| 99 | + } |
| 100 | + |
| 101 | + return null; |
| 102 | + } |
| 103 | +} |
0 commit comments