Skip to content

Commit

Permalink
fixed getAllChildren tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuti-G committed Aug 18, 2022
1 parent 8b3303b commit 6bc6950
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 143 deletions.
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Expand Up @@ -119,6 +119,8 @@ Bug Fixes
* LUCENE-10678: Fix potential overflow when building a BKD tree with more than 4 billion points. The overflow
occurs when computing the partition point. (Ignacio Vera)

* LUCENE-10644: Facets#getAllChildren testing should ignore child order. (Yuting Gan)

Build
---------------------

Expand Down
34 changes: 29 additions & 5 deletions lucene/facet/src/test/org/apache/lucene/facet/FacetTestCase.java
Expand Up @@ -254,14 +254,38 @@ protected void assertFloatValuesEquals(FacetResult a, FacetResult b) {
assertEquals(a.dim, b.dim);
assertTrue(Arrays.equals(a.path, b.path));
assertEquals(a.childCount, b.childCount);
assertEquals(a.value.floatValue(), b.value.floatValue(), a.value.floatValue() / 1e5);
assertNumericValuesEquals(a.value, b.value);
assertEquals(a.labelValues.length, b.labelValues.length);
for (int i = 0; i < a.labelValues.length; i++) {
assertEquals(a.labelValues[i].label, b.labelValues[i].label);
assertEquals(
a.labelValues[i].value.floatValue(),
b.labelValues[i].value.floatValue(),
a.labelValues[i].value.floatValue() / 1e5);
assertNumericValuesEquals(a.labelValues[i].value, b.labelValues[i].value);
}
}

protected void assertNumericValuesEquals(Number a, Number b) {
assertTrue(a.getClass().isInstance(b));
if (a instanceof Float) {
assertEquals(a.floatValue(), b.floatValue(), a.floatValue() / 1e5);
} else if (a instanceof Double) {
assertEquals(a.doubleValue(), b.doubleValue(), a.doubleValue() / 1e5);
} else {
assertEquals(a, b);
}
}

protected void assertFacetResult(
FacetResult result,
String expectedDim,
String[] expectedPath,
int expectedChildCount,
Number expectedValue,
LabelAndValue... expectedChildren) {
assertEquals(expectedDim, result.dim);
assertArrayEquals(expectedPath, result.path);
assertEquals(expectedChildCount, result.childCount);
assertNumericValuesEquals(expectedValue, result.value);
assertEquals(expectedChildren.length, result.labelValues.length);
// assert children equal with no assumption of the children ordering
assertTrue(Arrays.asList(result.labelValues).containsAll(Arrays.asList(expectedChildren)));
}
}
Expand Up @@ -36,11 +36,10 @@
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.tests.util.TestUtil;

/** Tests long value facets. */
public class TestLongValueFacetCounts extends LuceneTestCase {
public class TestLongValueFacetCounts extends FacetTestCase {

public void testBasic() throws Exception {
Directory d = newDirectory();
Expand Down Expand Up @@ -75,11 +74,20 @@ public void testBasic() throws Exception {
"dim=field path=[] value=101 childCount=6\n 0 (20)\n 1 (20)\n",
topChildrenResult.toString());

FacetResult allChildrenResult = facets.getAllChildren("field");
assertEquals(
"dim=field path=[] value=101 childCount=6\n 0 (20)\n 1 (20)\n 2 (20)\n 3 (20)\n "
+ "4 (20)\n 9223372036854775807 (1)\n",
allChildrenResult.toString());
assertFacetResult(
facets.getAllChildren("field"),
"field",
new String[0],
6,
101,
new LabelAndValue[] {
new LabelAndValue("0", 20),
new LabelAndValue("1", 20),
new LabelAndValue("2", 20),
new LabelAndValue("3", 20),
new LabelAndValue("4", 20),
new LabelAndValue("9223372036854775807", 1)
});

r.close();
d.close();
Expand Down Expand Up @@ -108,8 +116,16 @@ public void testCountAll() throws Exception {
assertEquals("dim=field path=[] value=9 childCount=2\n 0 (4)\n 1 (5)\n", result.toString());
result = facets.getTopChildren(10, "field");
assertEquals("dim=field path=[] value=9 childCount=2\n 1 (5)\n 0 (4)\n", result.toString());
result = facets.getAllChildren("field");
assertEquals("dim=field path=[] value=9 childCount=2\n 0 (4)\n 1 (5)\n", result.toString());

assertFacetResult(
facets.getAllChildren("field"),
"field",
new String[0],
2,
9,
new LabelAndValue[] {
new LabelAndValue("0", 4), new LabelAndValue("1", 5),
});

r.close();
d.close();
Expand All @@ -133,13 +149,18 @@ public void testOnlyBigLongs() throws Exception {
LongValueFacetCounts facets = new LongValueFacetCounts("field", fc);

FacetResult result = facets.getAllChildrenSortByValue();
assertEquals(
"dim=field path=[] value=3 childCount=3\n 9223372036854775805 (1)\n "
+ "9223372036854775806 (1)\n 9223372036854775807 (1)\n",
result.toString());

// test getAllChildren
result = facets.getAllChildren("field");
assertFacetResult(
facets.getAllChildren("field"),
"field",
new String[0],
3,
3,
new LabelAndValue[] {
new LabelAndValue("9223372036854775805", 1),
new LabelAndValue("9223372036854775806", 1),
new LabelAndValue("9223372036854775807", 1)
});

// since we have no insight into the value order in the hashMap, we sort labels by value and
// count in
Expand Down Expand Up @@ -805,8 +826,16 @@ public void testDuplicateLongValues() throws Exception {
for (LabelAndValue labelAndValue : fr.labelValues) {
assert labelAndValue.value.equals(1);
}
FacetResult result = facetCounts.getAllChildren("field");
assertEquals("dim=field path=[] value=2 childCount=2\n 42 (1)\n 43 (1)\n", result.toString());

assertFacetResult(
facetCounts.getAllChildren("field"),
"field",
new String[0],
2,
2,
new LabelAndValue[] {
new LabelAndValue("42", 1), new LabelAndValue("43", 1),
});

r.close();
dir.close();
Expand Down
Expand Up @@ -17,8 +17,6 @@
package org.apache.lucene.facet;

import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.document.Document;
Expand Down Expand Up @@ -296,21 +294,45 @@ private void assertCorrectResults(Facets facets) throws IOException {
assertEquals(
"dim=Band path=[] value=5 childCount=2\n Rock & Pop (4)\n Punk (1)\n",
facets.getTopChildren(10, "Band").toString());
assertEquals(
"dim=Band path=[] value=5 childCount=2\n Punk (1)\n Rock & Pop (4)\n",
sortAllChildren(facets.getAllChildren("Band")).toString());
assertFacetResult(
facets.getAllChildren("Band"),
"Band",
new String[0],
2,
5,
new LabelAndValue[] {
new LabelAndValue("Punk", 1), new LabelAndValue("Rock & Pop", 4),
});
assertEquals(
"dim=Band path=[Rock & Pop] value=4 childCount=4\n The Beatles (1)\n U2 (1)\n REM (1)\n Dave Matthews Band (1)\n",
facets.getTopChildren(10, "Band", "Rock & Pop").toString());
assertEquals(
"dim=Band path=[Rock & Pop] value=4 childCount=4\n Dave Matthews Band (1)\n REM (1)\n The Beatles (1)\n U2 (1)\n",
sortAllChildren(facets.getAllChildren("Band", "Rock & Pop")).toString());
assertFacetResult(
facets.getAllChildren("Band", "Rock & Pop"),
"Band",
new String[] {"Rock & Pop"},
4,
4,
new LabelAndValue[] {
new LabelAndValue("Dave Matthews Band", 1),
new LabelAndValue("REM", 1),
new LabelAndValue("The Beatles", 1),
new LabelAndValue("U2", 1),
});

assertEquals(
"dim=Author path=[] value=3 childCount=3\n Mark Twain (1)\n Stephen King (1)\n Kurt Vonnegut (1)\n",
facets.getTopChildren(10, "Author").toString());
assertEquals(
"dim=Author path=[] value=3 childCount=3\n Kurt Vonnegut (1)\n Mark Twain (1)\n Stephen King (1)\n",
sortAllChildren(facets.getAllChildren("Author")).toString());
assertFacetResult(
facets.getAllChildren("Author"),
"Author",
new String[0],
3,
3,
new LabelAndValue[] {
new LabelAndValue("Kurt Vonnegut", 1),
new LabelAndValue("Mark Twain", 1),
new LabelAndValue("Stephen King", 1),
});
}

private FacetsCollector performSearch(TaxonomyReader tr, IndexReader ir, IndexSearcher searcher)
Expand All @@ -329,15 +351,4 @@ private void seedIndex(TaxonomyWriter tw, RandomIndexWriter iw, FacetsConfig con
iw.addDocument(config.build(tw, doc));
}
}

// since we have no insight into the ordinals assigned to the values, we sort labels by value and
// count in
// ascending order in order to compare with expected results
private static FacetResult sortAllChildren(FacetResult allChildrenResult) {
Arrays.sort(
allChildrenResult.labelValues,
Comparator.comparing((LabelAndValue a) -> a.label)
.thenComparingLong(a -> a.value.longValue()));
return allChildrenResult;
}
}
Expand Up @@ -118,9 +118,15 @@ public void testCountAll() throws Exception {
"dim=field path=[] value=2 childCount=2\n bar (1)\n foo (1)",
facets.getTopChildren(10, "field").toString().trim());

assertEquals(
"dim=field path=[] value=2 childCount=2\n bar (1)\n foo (1)",
facets.getAllChildren("field").toString().trim());
assertFacetResult(
facets.getAllChildren("field"),
"field",
new String[0],
2,
2,
new LabelAndValue[] {
new LabelAndValue("bar", 1), new LabelAndValue("foo", 1),
});

IOUtils.close(searcher.getIndexReader(), dir);
}
Expand Down Expand Up @@ -502,8 +508,7 @@ private void checkAllChildrenFacetResult(
assertEquals(expectedTotalDocsWithValue, facetResult.value);

// since we have no insight into the ordinals assigned to the values, we sort labels by value
// and count in
// ascending order in order to compare with expected results
// and count in ascending order in order to compare with expected results
Arrays.sort(
facetResult.labelValues,
Comparator.comparing((LabelAndValue a) -> a.label)
Expand Down

0 comments on commit 6bc6950

Please sign in to comment.