1919 */
2020package org .elasticsearch .index .codec .vectors .es92 ;
2121
22- import org .apache .lucene .codecs .KnnVectorsFormat ;
2322import org .apache .lucene .codecs .KnnVectorsReader ;
2423import org .apache .lucene .codecs .KnnVectorsWriter ;
2524import org .apache .lucene .codecs .hnsw .FlatVectorsFormat ;
26- import org .apache .lucene .codecs .lucene99 .Lucene99HnswVectorsFormat ;
2725import org .apache .lucene .codecs .lucene99 .Lucene99HnswVectorsReader ;
2826import org .apache .lucene .codecs .lucene99 .Lucene99HnswVectorsWriter ;
2927import org .apache .lucene .index .SegmentReadState ;
3028import org .apache .lucene .index .SegmentWriteState ;
31- import org .apache .lucene .search .TaskExecutor ;
32- import org .apache .lucene .util .hnsw .HnswGraph ;
29+ import org .elasticsearch .index .codec .vectors .AbstractHnswVectorsFormat ;
3330
3431import java .io .IOException ;
3532import java .util .concurrent .ExecutorService ;
3633
3734import static org .apache .lucene .codecs .lucene99 .Lucene99HnswVectorsFormat .DEFAULT_BEAM_WIDTH ;
3835import static org .apache .lucene .codecs .lucene99 .Lucene99HnswVectorsFormat .DEFAULT_MAX_CONN ;
3936import static org .apache .lucene .codecs .lucene99 .Lucene99HnswVectorsFormat .DEFAULT_NUM_MERGE_WORKER ;
40- import static org .apache .lucene .codecs .lucene99 .Lucene99HnswVectorsFormat .MAXIMUM_BEAM_WIDTH ;
41- import static org .apache .lucene .codecs .lucene99 .Lucene99HnswVectorsFormat .MAXIMUM_MAX_CONN ;
42- import static org .elasticsearch .index .mapper .vectors .DenseVectorFieldMapper .MAX_DIMS_COUNT ;
4337
4438/**
4539 * Copied from Lucene, replace with Lucene's implementation sometime after Lucene 10
4640 */
47- public class ES92HnswBinaryQuantizedBFloat16VectorsFormat extends KnnVectorsFormat {
41+ public class ES92HnswBinaryQuantizedBFloat16VectorsFormat extends AbstractHnswVectorsFormat {
4842
4943 public static final String NAME = "ES92HnswBinaryQuantizedBFloat16VectorsFormat" ;
5044
51- /**
52- * Controls how many of the nearest neighbor candidates are connected to the new node. Defaults to
53- * {@link Lucene99HnswVectorsFormat#DEFAULT_MAX_CONN}. See {@link HnswGraph} for more details.
54- */
55- private final int maxConn ;
56-
57- /**
58- * The number of candidate neighbors to track while searching the graph for each newly inserted
59- * node. Defaults to {@link Lucene99HnswVectorsFormat#DEFAULT_BEAM_WIDTH}. See {@link HnswGraph}
60- * for details.
61- */
62- private final int beamWidth ;
63-
6445 /** The format for storing, reading, merging vectors on disk */
6546 private static final FlatVectorsFormat flatVectorsFormat = new ES92BinaryQuantizedBFloat16VectorsFormat ();
6647
67- private final int numMergeWorkers ;
68- private final TaskExecutor mergeExec ;
69-
7048 /** Constructs a format using default graph construction parameters */
7149 public ES92HnswBinaryQuantizedBFloat16VectorsFormat () {
7250 this (DEFAULT_MAX_CONN , DEFAULT_BEAM_WIDTH , DEFAULT_NUM_MERGE_WORKER , null );
@@ -93,28 +71,12 @@ public ES92HnswBinaryQuantizedBFloat16VectorsFormat(int maxConn, int beamWidth)
9371 * generated by this format to do the merge
9472 */
9573 public ES92HnswBinaryQuantizedBFloat16VectorsFormat (int maxConn , int beamWidth , int numMergeWorkers , ExecutorService mergeExec ) {
96- super (NAME );
97- if (maxConn <= 0 || maxConn > MAXIMUM_MAX_CONN ) {
98- throw new IllegalArgumentException (
99- "maxConn must be positive and less than or equal to " + MAXIMUM_MAX_CONN + "; maxConn=" + maxConn
100- );
101- }
102- if (beamWidth <= 0 || beamWidth > MAXIMUM_BEAM_WIDTH ) {
103- throw new IllegalArgumentException (
104- "beamWidth must be positive and less than or equal to " + MAXIMUM_BEAM_WIDTH + "; beamWidth=" + beamWidth
105- );
106- }
107- this .maxConn = maxConn ;
108- this .beamWidth = beamWidth ;
109- if (numMergeWorkers == 1 && mergeExec != null ) {
110- throw new IllegalArgumentException ("No executor service is needed as we'll use single thread to merge" );
111- }
112- this .numMergeWorkers = numMergeWorkers ;
113- if (mergeExec != null ) {
114- this .mergeExec = new TaskExecutor (mergeExec );
115- } else {
116- this .mergeExec = null ;
117- }
74+ super (NAME , maxConn , beamWidth , numMergeWorkers , mergeExec );
75+ }
76+
77+ @ Override
78+ protected FlatVectorsFormat flatVectorsFormat () {
79+ return flatVectorsFormat ;
11880 }
11981
12082 @ Override
@@ -126,20 +88,4 @@ public KnnVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException
12688 public KnnVectorsReader fieldsReader (SegmentReadState state ) throws IOException {
12789 return new Lucene99HnswVectorsReader (state , flatVectorsFormat .fieldsReader (state ));
12890 }
129-
130- @ Override
131- public int getMaxDimensions (String fieldName ) {
132- return MAX_DIMS_COUNT ;
133- }
134-
135- @ Override
136- public String toString () {
137- return "ES92HnswBinaryQuantizedBFloat16VectorsFormat(name=ES92HnswBinaryQuantizedBFloat16VectorsFormat, maxConn="
138- + maxConn
139- + ", beamWidth="
140- + beamWidth
141- + ", flatVectorFormat="
142- + flatVectorsFormat
143- + ")" ;
144- }
14591}
0 commit comments