Skip to content

Commit

Permalink
Test non-positive aggregation values
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanvodita committed Sep 23, 2023
1 parent 36432fa commit d1455fd
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.apache.lucene.util.BitUtil;
Expand Down Expand Up @@ -514,6 +515,67 @@ public void testIntSumAssociationDrillDown() throws Exception {
"Wrong count for category 'b'!", 150, facets.getSpecificValue("int", "b").intValue());
}

public void testNonPositiveAggregations() throws IOException {
Directory dir = newDirectory();
Directory taxoDir = newDirectory();

TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);

FacetsConfig config = new FacetsConfig();
config.setIndexFieldName("a", FacetsConfig.DEFAULT_INDEX_FIELD_NAME);

RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document d;

d = new Document();
// A value of 1 for a/1. This is ok.
d.add(new FloatAssociationFacetField(1f, "a", "1"));
writer.addDocument(config.build(taxoWriter, d));

d = new Document();
// A value of 0 for a/2. This will get discarded because it not greater than zero.
// See line 217 in FloatTaxonomyFacets.
d.add(new FloatAssociationFacetField(0f, "a", "2"));
writer.addDocument(config.build(taxoWriter, d));

d = new Document();
// A value of -1 for a/3. This gets discarded like 0.
// Even if it weren't discarded in that initial check, it would not be placed in the queue
// because the bottom value of the queue is initialized with 0.
// See line 222 in FloatTaxonomyFacets.
d.add(new FloatAssociationFacetField(-1f, "a", "3"));
writer.addDocument(config.build(taxoWriter, d));

IndexReader reader = writer.getReader();
IOUtils.close(taxoWriter, writer);

IndexSearcher searcher = newSearcher(reader);
Query q = new MatchAllDocsQuery();
FacetsCollector fc = searcher.search(q, new FacetsCollectorManager());

TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
FloatTaxonomyFacets facets =
new TaxonomyFacetFloatAssociations(
FacetsConfig.DEFAULT_INDEX_FIELD_NAME,
taxoReader,
config,
fc,
AssociationAggregationFunction.SUM);

// This is the result we get. "2" and "3" are discarded from the result because
// they have non-positive values associated to them.
assertEquals(
"dim=a path=[] value=1.0 childCount=1\n 1 (1.0)\n",
facets.getTopChildren(10, "a").toString());

// This is the result we would get if we didn't discard non-positive aggregation values.
assertEquals(
"dim=a path=[] value=0.0 childCount=3\n 1 (1.0)\n 2 (0.0)\n 3 (-1.0)\n",
facets.getTopChildren(10, "a").toString());

IOUtils.close(taxoReader, reader, taxoDir, dir);
}

private void validateInts(
String dim,
Map<String, Integer> expected,
Expand Down

0 comments on commit d1455fd

Please sign in to comment.