1010package org .elasticsearch .index .mapper ;
1111
1212import org .apache .lucene .index .BinaryDocValues ;
13+ import org .apache .lucene .index .ByteVectorValues ;
1314import org .apache .lucene .index .DocValues ;
1415import org .apache .lucene .index .FloatVectorValues ;
1516import org .apache .lucene .index .KnnVectorValues ;
@@ -508,10 +509,10 @@ public String toString() {
508509 }
509510 }
510511
511- public static class DenseVectorBlockLoader extends DocValuesBlockLoader {
512+ public static class DenseVectorFloatBlockLoader extends DocValuesBlockLoader {
512513 private final String fieldName ;
513514
514- public DenseVectorBlockLoader (String fieldName ) {
515+ public DenseVectorFloatBlockLoader (String fieldName ) {
515516 this .fieldName = fieldName ;
516517 }
517518
@@ -583,6 +584,83 @@ public String toString() {
583584 }
584585 }
585586
587+ public static class DenseVectorByteBlockLoader extends DocValuesBlockLoader {
588+ private final String fieldName ;
589+
590+ public DenseVectorByteBlockLoader (String fieldName ) {
591+ this .fieldName = fieldName ;
592+ }
593+
594+ @ Override
595+ public Builder builder (BlockFactory factory , int expectedCount ) {
596+ return factory .bytesRefs (expectedCount );
597+ }
598+
599+ @ Override
600+ public AllReader reader (LeafReaderContext context ) throws IOException {
601+ ByteVectorValues byteVectorValues = context .reader ().getByteVectorValues (fieldName );
602+ if (byteVectorValues != null ) {
603+ return new ByteVectorValuesBlockReader (byteVectorValues );
604+ }
605+ return new ConstantNullsReader ();
606+ }
607+ }
608+
609+ private static class ByteVectorValuesBlockReader extends BlockDocValuesReader {
610+ private final ByteVectorValues byteVectorValues ;
611+ private final KnnVectorValues .DocIndexIterator iterator ;
612+
613+ ByteVectorValuesBlockReader (ByteVectorValues byteVectorValues ) {
614+ this .byteVectorValues = byteVectorValues ;
615+ iterator = byteVectorValues .iterator ();
616+ }
617+
618+ @ Override
619+ public BlockLoader .Block read (BlockFactory factory , Docs docs ) throws IOException {
620+ // Doubles from doc values ensures that the values are in order
621+ try (BlockLoader .BytesRefBuilder builder = factory .bytesRefsFromDocValues (docs .count ())) {
622+ for (int i = 0 ; i < docs .count (); i ++) {
623+ int doc = docs .get (i );
624+ if (doc < iterator .docID ()) {
625+ throw new IllegalStateException ("docs within same block must be in order" );
626+ }
627+ read (doc , builder );
628+ }
629+ return builder .build ();
630+ }
631+ }
632+
633+ @ Override
634+ public void read (int docId , BlockLoader .StoredFields storedFields , Builder builder ) throws IOException {
635+ read (docId , (BlockLoader .BytesRefBuilder ) builder );
636+ }
637+
638+ private void read (int doc , BlockLoader .BytesRefBuilder builder ) throws IOException {
639+ if (iterator .advance (doc ) == doc ) {
640+ builder .beginPositionEntry ();
641+ byte [] bytes = byteVectorValues .vectorValue (iterator .index ());
642+ BytesRef scratch = new BytesRef (bytes , 0 , 1 );
643+ for (int i = 0 ; i < bytes .length ; i ++) {
644+ scratch .offset = i ;
645+ builder .appendBytesRef (scratch );
646+ }
647+ builder .endPositionEntry ();
648+ } else {
649+ builder .appendNull ();
650+ }
651+ }
652+
653+ @ Override
654+ public int docId () {
655+ return iterator .docID ();
656+ }
657+
658+ @ Override
659+ public String toString () {
660+ return "BlockDocValuesReader.FloatVectorValuesBlockReader" ;
661+ }
662+ }
663+
586664 public static class BytesRefsFromOrdsBlockLoader extends DocValuesBlockLoader {
587665 private final String fieldName ;
588666
@@ -831,12 +909,12 @@ public String toString() {
831909 }
832910 }
833911
834- public static class DenseVectorFromBinaryBlockLoader extends DocValuesBlockLoader {
912+ public static class DenseVectorFromFloatsBinaryBlockLoader extends DocValuesBlockLoader {
835913 private final String fieldName ;
836914 private final int dims ;
837915 private final IndexVersion indexVersion ;
838916
839- public DenseVectorFromBinaryBlockLoader (String fieldName , int dims , IndexVersion indexVersion ) {
917+ public DenseVectorFromFloatsBinaryBlockLoader (String fieldName , int dims , IndexVersion indexVersion ) {
840918 this .fieldName = fieldName ;
841919 this .dims = dims ;
842920 this .indexVersion = indexVersion ;
@@ -853,18 +931,18 @@ public AllReader reader(LeafReaderContext context) throws IOException {
853931 if (docValues == null ) {
854932 return new ConstantNullsReader ();
855933 }
856- return new DenseVectorFromBinary (docValues , dims , indexVersion );
934+ return new DenseVectorFromFloatsBinary (docValues , dims , indexVersion );
857935 }
858936 }
859937
860- private static class DenseVectorFromBinary extends BlockDocValuesReader {
938+ private static class DenseVectorFromFloatsBinary extends BlockDocValuesReader {
861939 private final BinaryDocValues docValues ;
862940 private final IndexVersion indexVersion ;
863941 private final float [] scratch ;
864942
865943 private int docID = -1 ;
866944
867- DenseVectorFromBinary (BinaryDocValues docValues , int dims , IndexVersion indexVersion ) {
945+ DenseVectorFromFloatsBinary (BinaryDocValues docValues , int dims , IndexVersion indexVersion ) {
868946 this .docValues = docValues ;
869947 this .scratch = new float [dims ];
870948 this .indexVersion = indexVersion ;
@@ -913,7 +991,90 @@ public int docId() {
913991
914992 @ Override
915993 public String toString () {
916- return "DenseVectorFromBinary.Bytes" ;
994+ return "DenseVectorFromFloatsBinary.Bytes" ;
995+ }
996+ }
997+
998+ public static class DenseVectorFromFBytesBinaryBlockLoader extends DocValuesBlockLoader {
999+ private final String fieldName ;
1000+ private final int dims ;
1001+
1002+ public DenseVectorFromFBytesBinaryBlockLoader (String fieldName , int dims ) {
1003+ this .fieldName = fieldName ;
1004+ this .dims = dims ;
1005+ }
1006+
1007+ @ Override
1008+ public Builder builder (BlockFactory factory , int expectedCount ) {
1009+ return factory .bytesRefs (expectedCount );
1010+ }
1011+
1012+ @ Override
1013+ public AllReader reader (LeafReaderContext context ) throws IOException {
1014+ BinaryDocValues docValues = context .reader ().getBinaryDocValues (fieldName );
1015+ if (docValues == null ) {
1016+ return new ConstantNullsReader ();
1017+ }
1018+ return new DenseVectorFromBytesBinary (docValues , dims );
1019+ }
1020+ }
1021+
1022+ private static class DenseVectorFromBytesBinary extends BlockDocValuesReader {
1023+ private final BinaryDocValues docValues ;
1024+ private final int dims ;
1025+
1026+ private int docID = -1 ;
1027+
1028+ DenseVectorFromBytesBinary (BinaryDocValues docValues , int dims ) {
1029+ this .docValues = docValues ;
1030+ this .dims = dims ;
1031+ }
1032+
1033+ @ Override
1034+ public BlockLoader .Block read (BlockFactory factory , Docs docs ) throws IOException {
1035+ try (BlockLoader .BytesRefBuilder builder = factory .bytesRefs (docs .count ())) {
1036+ for (int i = 0 ; i < docs .count (); i ++) {
1037+ int doc = docs .get (i );
1038+ if (doc < docID ) {
1039+ throw new IllegalStateException ("docs within same block must be in order" );
1040+ }
1041+ read (doc , builder );
1042+ }
1043+ return builder .build ();
1044+ }
1045+ }
1046+
1047+ @ Override
1048+ public void read (int docId , BlockLoader .StoredFields storedFields , Builder builder ) throws IOException {
1049+ read (docId , (BlockLoader .BytesRefBuilder ) builder );
1050+ }
1051+
1052+ private void read (int doc , BlockLoader .BytesRefBuilder builder ) throws IOException {
1053+ this .docID = doc ;
1054+ if (false == docValues .advanceExact (doc )) {
1055+ builder .appendNull ();
1056+ return ;
1057+ }
1058+ BytesRef bytesRef = docValues .binaryValue ();
1059+ assert bytesRef .length > 0 ;
1060+
1061+ builder .beginPositionEntry ();
1062+ BytesRef scratch = new BytesRef (bytesRef .bytes , 0 , 1 );
1063+ for (int i = 0 ; i < dims ; i ++) {
1064+ scratch .offset = i ;
1065+ builder .appendBytesRef (scratch );
1066+ }
1067+ builder .endPositionEntry ();
1068+ }
1069+
1070+ @ Override
1071+ public int docId () {
1072+ return docID ;
1073+ }
1074+
1075+ @ Override
1076+ public String toString () {
1077+ return "DenseVectorFromBytesBinary.Bytes" ;
9171078 }
9181079 }
9191080
0 commit comments