From c9f8ba7c020b1fad2298d203ac89b2a747cc911e Mon Sep 17 00:00:00 2001 From: Alex Klibisz <8015228+alexklibisz@users.noreply.github.com> Date: Sat, 25 Nov 2023 21:06:56 -0800 Subject: [PATCH] Add EmptyHitCounter for use in MatchHashesAndScoreQuerySuite --- .../elastiknn/search/EmptyHitCounter.java | 46 +++++++++++++++++++ .../search/MatchHashesAndScoreQuery.java | 3 +- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 elastiknn-lucene/src/main/java/com/klibisz/elastiknn/search/EmptyHitCounter.java diff --git a/elastiknn-lucene/src/main/java/com/klibisz/elastiknn/search/EmptyHitCounter.java b/elastiknn-lucene/src/main/java/com/klibisz/elastiknn/search/EmptyHitCounter.java new file mode 100644 index 000000000..d4160887e --- /dev/null +++ b/elastiknn-lucene/src/main/java/com/klibisz/elastiknn/search/EmptyHitCounter.java @@ -0,0 +1,46 @@ +package com.klibisz.elastiknn.search; + +import org.apache.lucene.search.KthGreatest; + +public final class EmptyHitCounter implements HitCounter { + @Override + public void increment(int key, short count) {} + + @Override + public void increment(int key, int count) {} + + @Override + public boolean isEmpty() { + return true; + } + + @Override + public short get(int key) { + return 0; + } + + @Override + public int numHits() { + return 0; + } + + @Override + public int capacity() { + return 0; + } + + @Override + public int minKey() { + return 0; + } + + @Override + public int maxKey() { + return 0; + } + + @Override + public KthGreatest.Result kthGreatest(int k) { + return new KthGreatest.Result((short) 0, 0, 0); + } +} diff --git a/elastiknn-lucene/src/main/java/org/apache/lucene/search/MatchHashesAndScoreQuery.java b/elastiknn-lucene/src/main/java/org/apache/lucene/search/MatchHashesAndScoreQuery.java index 5132f10d0..4962ca253 100644 --- a/elastiknn-lucene/src/main/java/org/apache/lucene/search/MatchHashesAndScoreQuery.java +++ b/elastiknn-lucene/src/main/java/org/apache/lucene/search/MatchHashesAndScoreQuery.java @@ -2,6 +2,7 @@ import com.klibisz.elastiknn.models.HashAndFreq; import com.klibisz.elastiknn.search.ArrayHitCounter; +import com.klibisz.elastiknn.search.EmptyHitCounter; import com.klibisz.elastiknn.search.HitCounter; import org.apache.lucene.index.*; import org.apache.lucene.util.BytesRef; @@ -58,7 +59,7 @@ private HitCounter countHits(LeafReader reader) throws IOException { Terms terms = reader.terms(field); // terms seem to be null after deleting docs. https://github.com/alexklibisz/elastiknn/issues/158 if (terms == null) { - return new ArrayHitCounter(0); + return new EmptyHitCounter(); } else { TermsEnum termsEnum = terms.iterator(); PostingsEnum docs = null;