From 8b7f42eabc6d10e7e566fdd054cf1e05b90fba76 Mon Sep 17 00:00:00 2001 From: Alex Klibisz <8015228+alexklibisz@users.noreply.github.com> Date: Sun, 26 Nov 2023 21:31:41 -0800 Subject: [PATCH] Refactor: Add EmptyHitCounter for use in MatchHashesAndScoreQuerySuite (#601) --- .../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;