File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -43,6 +43,47 @@ give it a try.
4343```
4444* The vector can be of any dimension
4545
46+ ### Converting a vector to Base64
47+ to convert an array of doubles to a base64 string we use these example methods:
48+
49+ ** Java**
50+ ```
51+ public static final String convertArrayToBase64(double[] array) {
52+ final int capacity = 8 * array.length;
53+ final ByteBuffer bb = ByteBuffer.allocate(capacity);
54+ for (int i = 0; i < array.length; i++) {
55+ bb.putDouble(array[i]);
56+ }
57+ bb.rewind();
58+ final ByteBuffer encodedBB = Base64.getEncoder().encode(bb);
59+ return new String(encodedBB.array());
60+ }
61+
62+ public static double[] convertBase64ToArray(String base64Str) {
63+ final byte[] decode = Base64.getDecoder().decode(base64Str.getBytes());
64+ final DoubleBuffer doubleBuffer = ByteBuffer.wrap(decode).asDoubleBuffer();
65+
66+ final double[] dims = new double[doubleBuffer.capacity()];
67+ doubleBuffer.get(dims);
68+ return dims;
69+ }
70+ ```
71+ ** Python**
72+ ```
73+ import base64
74+ import numpy as np
75+
76+ dbig = np.dtype('>f8')
77+
78+ def decode_float_list(base64_string):
79+ bytes = base64.b64decode(base64_string)
80+ return np.frombuffer(bytes, dtype=dbig).tolist()
81+
82+ def encode_array(arr):
83+ base64_str = base64.b64encode(np.array(arr).astype(dbig)).decode("utf-8")
84+ return base64_str
85+ ```
86+
4687### Querying
4788* For querying the 100 KNN documents use this POST message on your ES index:
4889
You can’t perform that action at this time.
0 commit comments