Skip to content

Apache Lucene open-source search software

Avoid unnecessary getGraph() call (#15560)

Shubham Chaudhary00b9533f28fa
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 6fa42526f92..fc9c93c614c 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -292,6 +292,8 @@ Optimizations
* GITHUB#15511: Dynamic pruning for SORTED(_SET) fields with doc values skipper (Pan Guixin)
+* GITHUB#15560: Avoid unnecessary getGraph() call. (Shubham Chaudhary)
+
Bug Fixes
---------------------
* GITHUB#14161: PointInSetQuery's constructor now throws IllegalArgumentException
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java
index d7d95d45486..e55e015652c 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java
@@ -335,15 +335,15 @@ public final class Lucene99HnswVectorsReader extends KnnVectorsReader
// Take into account if quantized? E.g. some scorer cost?
// Use approximate cardinality as this is good enough, but ensure we don't exceed the graph
// size as that is illogical
- HnswGraph graph = getGraph(fieldEntry);
- int filteredDocCount = Math.min(acceptDocs.cost(), graph.size());
+ int graphSize = (fieldEntry.vectorIndexLength() == 0) ? 0 : fieldEntry.size();
+ int filteredDocCount = Math.min(acceptDocs.cost(), graphSize);
Bits accepted = acceptDocs.bits();
final Bits acceptedOrds = scorer.getAcceptOrds(accepted);
int numVectors = scorer.maxOrd();
boolean doHnsw = knnCollector.k() < numVectors;
// The approximate number of vectors that would be visited if we did not filter
- int unfilteredVisit = HnswGraphSearcher.expectedVisitedNodes(knnCollector.k(), graph.size());
- if (unfilteredVisit >= filteredDocCount || graph.size() == 0) {
+ int unfilteredVisit = HnswGraphSearcher.expectedVisitedNodes(knnCollector.k(), graphSize);
+ if (unfilteredVisit >= filteredDocCount || graphSize == 0) {
doHnsw = false;
}
if (doHnsw) {