benchaplin commented on code in PR #14160:
URL: https://github.com/apache/lucene/pull/14160#discussion_r1927609309


##########
lucene/core/src/java/org/apache/lucene/util/hnsw/FilteredHnswGraphSearcher.java:
##########
@@ -0,0 +1,332 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.util.hnsw;
+
+import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
+
+import java.io.IOException;
+import org.apache.lucene.search.KnnCollector;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.FixedBitSet;
+import org.apache.lucene.util.SparseFixedBitSet;
+
+/**
+ * Searches an HNSW graph to find nearest neighbors to a query vector. This 
particular
+ * implementation is optimized for a filtered search, inspired by the ACORN-1 
algorithm.
+ * https://arxiv.org/abs/2403.04871 However, this implementation is augmented 
in some ways, mainly:
+ *
+ * <ul>
+ *   <li>It dynamically determines when the optimized filter step should occur 
based on some
+ *       filtered lambda. This is done per small world
+ *   <li>The graph searcher doesn't always explore all the extended 
neighborhood and the number of
+ *       additional candidates is predicated on the original candidate's 
filtered percentage.
+ * </ul>
+ */
+public class FilteredHnswGraphSearcher {
+  // How many filtered candidates must be found to consider N-hop neighbors
+  private static final float EXPANDED_EXPLORATION_LAMBDA = 0.10f;
+
+  /**
+   * Scratch data structures that are used in each {@link #searchBaseLevel} 
call. These can be
+   * expensive to allocate, so they're cleared and reused across calls.
+   */
+  private final NeighborQueue candidates;
+
+  private final BitSet visited;
+  private final BitSet explorationVisited;
+  private final HnswGraph graph;
+
+  /**
+   * Creates a new graph searcher.
+   *
+   * @param candidates max heap that will track the candidate nodes to explore
+   * @param visited bit set that will track nodes that have already been 
visited
+   */
+  private FilteredHnswGraphSearcher(
+      NeighborQueue candidates, BitSet explorationVisited, BitSet visited, 
HnswGraph graph) {
+    assert graph.maxConn() > 0 : "graph must have known max connections";
+    this.candidates = candidates;
+    this.visited = visited;
+    this.explorationVisited = explorationVisited;
+    this.graph = graph;
+  }
+
+  /**
+   * Searches HNSW graph for the nearest neighbors of a query vector.
+   *
+   * @param scorer the scorer to compare the query with the nodes
+   * @param knnCollector a collector of top knn results to be returned
+   * @param graph the graph values. May represent the entire graph, or a level 
in a hierarchical
+   *     graph.
+   * @param acceptOrds {@link Bits} that represents the allowed document 
ordinals to match, or
+   *     {@code null} if they are all allowed to match.
+   */
+  public static void search(
+      RandomVectorScorer scorer,
+      KnnCollector knnCollector,
+      HnswGraph graph,
+      int filterSize,

Review Comment:
   FYI missed a javadoc param for `filterSize`



-- 
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: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to