Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

support search_k (issue #3) #16

Merged
merged 1 commit into from
Oct 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/java/com/spotify/annoy/ANNIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ private static boolean isZeroVec(float[] v) {
@Override
public final List<Integer> getNearest(final float[] queryVector,
final int nResults) {
return getNearest(queryVector, nResults, -1);
}

public final List<Integer> getNearest(final float[] queryVector,
final int nResults, int searchK) {

if (queryVector.length != DIMENSION) {
throw new RuntimeException(String.format("queryVector must be size of %d, but was %d",
Expand All @@ -250,8 +255,12 @@ public final List<Integer> getNearest(final float[] queryVector,
pq.add(new PQEntry(kMaxPriority, r));
}

if (searchK == -1) {
searchK = roots.size() * nResults;
}

Set<Integer> nearestNeighbors = new HashSet<Integer>();
while (nearestNeighbors.size() < roots.size() * nResults && !pq.isEmpty()) {
while (nearestNeighbors.size() < searchK && !pq.isEmpty()) {
PQEntry top = pq.poll();
long topNodeOffset = top.nodeOffset;
int nDescendants = getIntInAnnBuf(topNodeOffset);
Expand Down