Mikep86 opened a new issue, #17049:
URL: https://github.com/apache/lucene/issues/17049

   ### Description
   
   `KnnVectorValues#iterator()` is documented as "Create an iterator for this 
instance", and on dense fields each call does return a new, independent 
iterator (`DenseOffHeapVectorValues#iterator()` calls `createDenseIterator()`).
   On sparse fields, where not every document has a vector, 
`SparseOffHeapVectorValues#iterator()` returns 
`IndexedDISI.asDocIndexIterator(disi)` over the instance's single `IndexedDISI`.
   Every iterator obtained from the same instance therefore shares one 
position: advancing one advances the others, and a new iterator does not start 
before the first document.
   
   The same applies to `OffHeapByteVectorValues`.
   `SparseOffHeapVectorValues#scorer` calls `copy()` before `iterator()`, which 
avoids the sharing, but nothing in the public contract tells callers to do the 
same.
   
   The failure is silent.
   Code that calls `iterator()` once per lookup on the same instance, for 
example to read the vectors of several documents in doc id order with 
`advance(target)`, gets correct results on dense fields and missing vectors on 
sparse ones: once the shared position has passed a target, `advance` returns a 
later document, and the lookup finds nothing.
   We hit this reading the chunk vectors of nested documents, where the vector 
field is always sparse; most lookups on a small index returned no vectors.
   
   Reproduction with the default codec:
   
   ```java
   try (Directory dir = new ByteBuffersDirectory();
        IndexWriter w = new IndexWriter(dir, new IndexWriterConfig())) {
     for (int i = 0; i < 10; i++) {
       Document doc = new Document();
       if (i % 2 == 0) { // without this condition the field is dense
         doc.add(new KnnFloatVectorField("v", new float[] {i, 1}));
       }
       w.addDocument(doc);
     }
     w.forceMerge(1);
     try (DirectoryReader r = DirectoryReader.open(w)) {
       FloatVectorValues values = 
r.leaves().get(0).reader().getFloatVectorValues("v");
       KnnVectorValues.DocIndexIterator first = values.iterator();
       first.nextDoc(); // 0
       KnnVectorValues.DocIndexIterator second = values.iterator();
       System.out.println(second.docID());   // dense: -1, sparse: 0
       System.out.println(second.nextDoc()); // dense: 0,  sparse: 2
     }
   }
   ```
   
   On Lucene 10.5.2 this prints `-1` and `0` for the dense field 
(`DenseOffHeapVectorValues`) and `0` and `2` for the sparse field 
(`SparseOffHeapVectorValues`).
   
   Possible resolutions, without a preference: document that iterators from one 
instance share state and that `copy()` gives an independent one, or make the 
sparse `iterator()` create its own `IndexedDISI`, as the dense one creates its 
own iterator.
   
   ### Version and environment details
   
   Lucene 10.5.2.
   The same code is on `main`: 
`lucene/core/src/java/org/apache/lucene/codecs/lucene95/OffHeapFloatVectorValues.java`
 and `OffHeapByteVectorValues.java`.
   Not specific to an OS or JDK.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to