|
10 | 10 |
|
11 | 11 | import org.apache.lucene.util.SetOnce;
|
12 | 12 | import org.elasticsearch.TransportVersions;
|
13 |
| -import org.elasticsearch.common.ParsingException; |
14 | 13 | import org.elasticsearch.common.io.stream.StreamInput;
|
15 | 14 | import org.elasticsearch.common.io.stream.StreamOutput;
|
16 | 15 | import org.elasticsearch.common.io.stream.Writeable;
|
|
29 | 28 | import java.io.IOException;
|
30 | 29 | import java.util.ArrayList;
|
31 | 30 | import java.util.Arrays;
|
32 |
| -import java.util.HexFormat; |
33 | 31 | import java.util.List;
|
34 | 32 | import java.util.Objects;
|
35 | 33 | import java.util.function.Supplier;
|
|
38 | 36 | import static org.elasticsearch.common.Strings.format;
|
39 | 37 | import static org.elasticsearch.index.query.AbstractQueryBuilder.DEFAULT_BOOST;
|
40 | 38 | import static org.elasticsearch.search.SearchService.DEFAULT_SIZE;
|
| 39 | +import static org.elasticsearch.search.vectors.KnnVectorQueryBuilder.parseQueryVector; |
41 | 40 | import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg;
|
42 | 41 | import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg;
|
43 | 42 |
|
@@ -68,52 +67,6 @@ public class KnnSearchBuilder implements Writeable, ToXContentFragment, Rewritea
|
68 | 67 | .similarity((Float) args[5]);
|
69 | 68 | });
|
70 | 69 |
|
71 |
| - private static float[] parseQueryVector(XContentParser parser) throws IOException { |
72 |
| - XContentParser.Token token = parser.currentToken(); |
73 |
| - final float[] vector; |
74 |
| - if (token == XContentParser.Token.START_ARRAY) { |
75 |
| - vector = parseQueryVectorArray(parser); |
76 |
| - } else if (token == XContentParser.Token.VALUE_STRING) { |
77 |
| - vector = parseHexEncodedVector(parser); |
78 |
| - } else if (token == XContentParser.Token.VALUE_NUMBER) { |
79 |
| - vector = parseNumberVector(parser); |
80 |
| - } else { |
81 |
| - throw new ParsingException(parser.getTokenLocation(), format("Unknown type for provided value [%s]", parser.text())); |
82 |
| - } |
83 |
| - return vector; |
84 |
| - } |
85 |
| - |
86 |
| - private static float[] parseQueryVectorArray(XContentParser parser) throws IOException { |
87 |
| - XContentParser.Token token; |
88 |
| - List<Float> vectorArr = new ArrayList<>(); |
89 |
| - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { |
90 |
| - if (token == XContentParser.Token.VALUE_NUMBER || token == XContentParser.Token.VALUE_STRING) { |
91 |
| - vectorArr.add(parser.floatValue()); |
92 |
| - } else { |
93 |
| - throw new ParsingException(parser.getTokenLocation(), format("Type [%s] not supported for query vector")); |
94 |
| - } |
95 |
| - } |
96 |
| - float[] floatVector = new float[vectorArr.size()]; |
97 |
| - for (int i = 0; i < vectorArr.size(); i++) { |
98 |
| - floatVector[i] = vectorArr.get(i); |
99 |
| - } |
100 |
| - return floatVector; |
101 |
| - } |
102 |
| - |
103 |
| - private static float[] parseNumberVector(XContentParser parser) throws IOException { |
104 |
| - return new float[] { parser.floatValue() }; |
105 |
| - } |
106 |
| - |
107 |
| - private static float[] parseHexEncodedVector(XContentParser parser) throws IOException { |
108 |
| - // TODO optimize this as the array returned will be recomputed later again as a byte array |
109 |
| - byte[] decodedByteQueryVector = HexFormat.of().parseHex(parser.text()); |
110 |
| - float[] floatVector = new float[decodedByteQueryVector.length]; |
111 |
| - for (int i = 0; i < decodedByteQueryVector.length; i++) { |
112 |
| - floatVector[i] = decodedByteQueryVector[i]; |
113 |
| - } |
114 |
| - return floatVector; |
115 |
| - } |
116 |
| - |
117 | 70 | static {
|
118 | 71 | PARSER.declareString(constructorArg(), FIELD_FIELD);
|
119 | 72 | PARSER.declareField(
|
|
0 commit comments