/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ package org.opensearch.knn.index; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexableFieldType; import org.apache.lucene.util.BytesRef; import org.opensearch.knn.index.codec.util.KNNVectorSerializer; import org.opensearch.knn.index.codec.util.KNNVectorSerializerFactory; public class VectorField extends Field { public VectorField(String name, float[] value, IndexableFieldType type) { super(name, new BytesRef(), type); try { final KNNVectorSerializer vectorSerializer = KNNVectorSerializerFactory.getDefaultSerializer(); final byte[] floatToByte = vectorSerializer.floatToByteArray(value); this.setBytesValue(floatToByte); } catch (Exception e) { throw new RuntimeException(e); } } /** * @param name FieldType name * @param value an array of byte vector values * @param type FieldType to build DocValues */ public VectorField(String name, byte[] value, IndexableFieldType type) { super(name, new BytesRef(), type); try { this.setBytesValue(value); } catch (Exception e) { throw new RuntimeException(e); } } }