2727import org .apache .lucene .index .SegmentReadState ;
2828import org .apache .lucene .index .SegmentWriteState ;
2929import org .elasticsearch .index .codec .vectors .OptimizedScalarQuantizer ;
30- import org .elasticsearch .index .codec .vectors .es818 .DirectIOLucene99FlatVectorsFormat ;
3130
3231import java .io .IOException ;
3332
@@ -102,38 +101,37 @@ public class ES910BinaryQuantizedVectorsFormat extends FlatVectorsFormat {
102101 private static final FlatVectorsFormat rawVectorFormat = new Lucene99FlatVectorsFormat (
103102 FlatVectorScorerUtil .getLucene99FlatVectorsScorer ()
104103 );
105- private static byte DEFAULT_INDEX_BITS = (byte ) 1 ;
106- private static byte DEFAULT_QUERY_BITS = (byte ) 4 ;
107104
108- private final ES910BinaryFlatVectorsScorer scorer ;
109-
110- private final byte indexBits ;
111- private final byte queryBits ;
105+ // index and query bits for quantization. They are static as we have no mapping, and thus can't create a format with specific bits.
106+ // we use static setters to change the quantization bits and scorers.
107+ private static byte INDEX_BITS = (byte ) 1 ;
108+ private static byte QUERY_BITS = (byte ) 4 ;
109+ private static ES910BinaryFlatVectorsScorer SCORER ;
112110
113111 public ES910BinaryQuantizedVectorsFormat () {
114- this (DEFAULT_INDEX_BITS , DEFAULT_QUERY_BITS ); // Default to 4 bits for index and 2 bits for query
112+ this (INDEX_BITS , QUERY_BITS ); // Default to 4 bits for index and 2 bits for query
115113 }
116114
117115 /** Creates a new instance with the default number of vectors per cluster. */
118116 public ES910BinaryQuantizedVectorsFormat (byte indexBits , byte queryBits ) {
119117 super (NAME );
120- this . indexBits = indexBits ;
121- this . queryBits = queryBits ;
122- // Set the default bits for index and query vectors. I know, I know, this is a hack, but we
123- // don't have the possibility of doing a PerFieldMapperCodec yet on KnnSearcher
124- DEFAULT_QUERY_BITS = queryBits ;
125- DEFAULT_INDEX_BITS = indexBits ;
126- this . scorer = new ES910BinaryFlatVectorsScorer (FlatVectorScorerUtil .getLucene99FlatVectorsScorer (), indexBits , queryBits );
118+ setQuantizationBits ( indexBits , queryBits ) ;
119+ }
120+
121+ public static void setQuantizationBits ( byte indexBits , byte queryBits ) {
122+ INDEX_BITS = indexBits ;
123+ QUERY_BITS = queryBits ;
124+ SCORER = new ES910BinaryFlatVectorsScorer (FlatVectorScorerUtil .getLucene99FlatVectorsScorer (), indexBits , queryBits );
127125 }
128126
129127 @ Override
130128 public FlatVectorsWriter fieldsWriter (SegmentWriteState state ) throws IOException {
131- return new ES910BinaryQuantizedVectorsWriter (scorer , rawVectorFormat .fieldsWriter (state ), state , indexBits , queryBits );
129+ return new ES910BinaryQuantizedVectorsWriter (SCORER , rawVectorFormat .fieldsWriter (state ), state , INDEX_BITS , QUERY_BITS );
132130 }
133131
134132 @ Override
135133 public FlatVectorsReader fieldsReader (SegmentReadState state ) throws IOException {
136- return new ES910BinaryQuantizedVectorsReader (state , rawVectorFormat .fieldsReader (state ), scorer );
134+ return new ES910BinaryQuantizedVectorsReader (state , rawVectorFormat .fieldsReader (state ), SCORER );
137135 }
138136
139137 @ Override
@@ -146,11 +144,11 @@ public String toString() {
146144 return "ES910BinaryQuantizedVectorsFormat(name="
147145 + NAME
148146 + ", flatVectorScorer="
149- + scorer
147+ + SCORER
150148 + ", indexBits="
151- + indexBits
149+ + INDEX_BITS
152150 + ", queryBits="
153- + queryBits
151+ + QUERY_BITS
154152 + ")" ;
155153 }
156154}
0 commit comments