Fix NPE in StringValueFacetCounts over empty match-set (#13494)

This commit is contained in:
yadda yadda yadda 2024-06-21 11:18:39 +02:00 committed by GitHub
parent ab291210db
commit c7f4b8dee2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 49 additions and 3 deletions

View File

@ -283,6 +283,9 @@ Bug Fixes
* GITHUB#13463: Address bug in MultiLeafKnnCollector causing #minCompetitiveSimilarity to stay artificially low in * GITHUB#13463: Address bug in MultiLeafKnnCollector causing #minCompetitiveSimilarity to stay artificially low in
some corner cases. (Greg Miller) some corner cases. (Greg Miller)
* GITHUB#13493: StringValueFacetCunts stops throwing NPE when faceting over an empty match-set. (Grebennikov Roman,
Stefan Vodita)
Other Other
-------------------- --------------------
(No changes) (No changes)

View File

@ -154,7 +154,7 @@ public class StringValueFacetCounts extends Facets {
final BytesRef term = docValues.lookupOrd(sparseCount.key); final BytesRef term = docValues.lookupOrd(sparseCount.key);
labelValues.add(new LabelAndValue(term.utf8ToString(), count)); labelValues.add(new LabelAndValue(term.utf8ToString(), count));
} }
} else { } else if (denseCounts != null) {
for (int i = 0; i < denseCounts.length; i++) { for (int i = 0; i < denseCounts.length; i++) {
int count = denseCounts[i]; int count = denseCounts[i];
if (count != 0) { if (count != 0) {
@ -206,7 +206,7 @@ public class StringValueFacetCounts extends Facets {
} }
} }
} }
} else { } else if (denseCounts != null) {
for (int i = 0; i < denseCounts.length; i++) { for (int i = 0; i < denseCounts.length; i++) {
int count = denseCounts[i]; int count = denseCounts[i];
if (count != 0) { if (count != 0) {
@ -256,7 +256,13 @@ public class StringValueFacetCounts extends Facets {
return -1; return -1;
} }
return sparseCounts != null ? sparseCounts.get(ord) : denseCounts[ord]; if (sparseCounts != null) {
return sparseCounts.get(ord);
}
if (denseCounts != null) {
return denseCounts[ord];
}
return 0;
} }
@Override @Override

View File

@ -34,6 +34,7 @@ import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.index.RandomIndexWriter; import org.apache.lucene.tests.index.RandomIndexWriter;
import org.apache.lucene.tests.util.LuceneTestCase; import org.apache.lucene.tests.util.LuceneTestCase;
@ -80,6 +81,42 @@ public class TestStringValueFacetCounts extends FacetTestCase {
IOUtils.close(searcher.getIndexReader(), dir); IOUtils.close(searcher.getIndexReader(), dir);
} }
private void assertEmptyFacetResult(FacetResult result) {
assertEquals(0, result.path.length);
assertEquals(0, result.value);
assertEquals(0, result.childCount);
assertEquals(0, result.labelValues.length);
}
public void testEmptyMatchset() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document doc = new Document();
doc.add(new SortedSetDocValuesField("field", new BytesRef("foo")));
writer.addDocument(doc);
IndexSearcher searcher = newSearcher(writer.getReader());
writer.close();
FacetsCollector facetsCollector =
searcher.search(new MatchNoDocsQuery(), new FacetsCollectorManager());
StringDocValuesReaderState state =
new StringDocValuesReaderState(searcher.getIndexReader(), "field");
StringValueFacetCounts counts = new StringValueFacetCounts(state, facetsCollector);
FacetResult top = counts.getTopChildren(10, "field");
assertEmptyFacetResult(top);
FacetResult all = counts.getAllChildren("field");
assertEmptyFacetResult(all);
assertEquals(0, counts.getSpecificValue("field", "foo"));
IOUtils.close(searcher.getIndexReader(), dir);
}
// See: LUCENE-10070 // See: LUCENE-10070
public void testCountAll() throws Exception { public void testCountAll() throws Exception {