Skip to content

Commit

Permalink
Minor cleanup in some Facet tests (apache#13489)
Browse files Browse the repository at this point in the history
  • Loading branch information
slow-J authored Jul 10, 2024
1 parent 428fdb5 commit 49e7810
Show file tree
Hide file tree
Showing 17 changed files with 240 additions and 479 deletions.
47 changes: 19 additions & 28 deletions lucene/facet/src/test/org/apache/lucene/facet/FacetTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -85,7 +84,8 @@ public List<List<FacetLabel>> getAllTaxonomyFacetLabels(
* @param docId docId for which facet labels are needed.
* @param dimension Retain facet labels for supplied dimension only. A null value fetches all
* facet labels.
* @param facetLabelReader {@FacetLabelReader} instance use to get facet labels for input docId.
* @param facetLabelReader {@link FacetLabelReader} instance use to get facet labels for input
* docId.
* @return {@code List<FacetLabel>} containing matching facet labels.
* @throws IOException when a low-level IO issue occurs while reading facet labels.
*/
Expand Down Expand Up @@ -178,12 +178,9 @@ protected void sortTies(LabelAndValue[] labelValues) {
labelValues,
i - numInRow,
i,
new Comparator<LabelAndValue>() {
@Override
public int compare(LabelAndValue a, LabelAndValue b) {
assert a.value.doubleValue() == b.value.doubleValue();
return new BytesRef(a.label).compareTo(new BytesRef(b.label));
}
(a, b) -> {
assert a.value.doubleValue() == b.value.doubleValue();
return new BytesRef(a.label).compareTo(new BytesRef(b.label));
});
}
numInRow = 1;
Expand All @@ -198,33 +195,27 @@ public int compare(LabelAndValue a, LabelAndValue b) {
protected void sortLabelValues(List<LabelAndValue> labelValues) {
Collections.sort(
labelValues,
new Comparator<LabelAndValue>() {
@Override
public int compare(LabelAndValue a, LabelAndValue b) {
if (a.value.doubleValue() > b.value.doubleValue()) {
return -1;
} else if (a.value.doubleValue() < b.value.doubleValue()) {
return 1;
} else {
return new BytesRef(a.label).compareTo(new BytesRef(b.label));
}
(a, b) -> {
if (a.value.doubleValue() > b.value.doubleValue()) {
return -1;
} else if (a.value.doubleValue() < b.value.doubleValue()) {
return 1;
} else {
return new BytesRef(a.label).compareTo(new BytesRef(b.label));
}
});
}

protected void sortFacetResults(List<FacetResult> results) {
Collections.sort(
results,
new Comparator<FacetResult>() {
@Override
public int compare(FacetResult a, FacetResult b) {
if (a.value.doubleValue() > b.value.doubleValue()) {
return -1;
} else if (b.value.doubleValue() > a.value.doubleValue()) {
return 1;
} else {
return a.dim.compareTo(b.dim);
}
(a, b) -> {
if (a.value.doubleValue() > b.value.doubleValue()) {
return -1;
} else if (b.value.doubleValue() > a.value.doubleValue()) {
return 1;
} else {
return a.dim.compareTo(b.dim);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,12 @@ public void testBasic() throws Exception {
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)
});
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 @@ -123,9 +121,8 @@ public void testCountAll() throws Exception {
new String[0],
2,
9,
new LabelAndValue[] {
new LabelAndValue("0", 4), new LabelAndValue("1", 5),
});
new LabelAndValue("0", 4),
new LabelAndValue("1", 5));

r.close();
d.close();
Expand Down Expand Up @@ -156,11 +153,9 @@ public void testOnlyBigLongs() throws Exception {
new String[0],
3,
3,
new LabelAndValue[] {
new LabelAndValue("9223372036854775805", 1),
new LabelAndValue("9223372036854775806", 1),
new LabelAndValue("9223372036854775807", 1)
});
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 @@ -221,11 +216,7 @@ public void testGetAllDims() throws Exception {
List<FacetResult> topDimsResults2 = facets.getTopDims(0, 1);
assertEquals(0, topDimsResults2.size());
// test getAllDims(0)
expectThrows(
IllegalArgumentException.class,
() -> {
facets.getAllDims(0);
});
expectThrows(IllegalArgumentException.class, () -> facets.getAllDims(0));

r.close();
d.close();
Expand Down Expand Up @@ -364,8 +355,7 @@ public void testRandomSingleValued() throws Exception {

// test getAllChildren
expectedCounts.sort(
Comparator.comparing((Map.Entry<Long, Integer> a) -> a.getKey())
.thenComparingLong(Map.Entry::getValue));
Map.Entry.<Long, Integer>comparingByKey().thenComparingLong(Map.Entry::getValue));
FacetResult allChildren = facetCounts.getAllChildren("field");
// sort labels by value, count in ascending order
Arrays.sort(
Expand Down Expand Up @@ -627,8 +617,7 @@ public void testRandomMultiValued() throws Exception {

// test getAllChildren
expectedCounts.sort(
Comparator.comparing((Map.Entry<Long, Integer> a) -> a.getKey())
.thenComparingLong(Map.Entry::getValue));
Map.Entry.<Long, Integer>comparingByKey().thenComparingLong(Map.Entry::getValue));
FacetResult allChildren = facetCounts.getAllChildren("field");
// sort labels by value, count in ascending order
Arrays.sort(
Expand Down Expand Up @@ -833,9 +822,8 @@ public void testDuplicateLongValues() throws Exception {
new String[0],
2,
2,
new LabelAndValue[] {
new LabelAndValue("42", 1), new LabelAndValue("43", 1),
});
new LabelAndValue("42", 1),
new LabelAndValue("43", 1));

r.close();
dir.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void testDefault() throws Exception {
// prepare searcher to search against
IndexSearcher searcher = newSearcher(ir);

FacetsCollector sfc = performSearch(tr, ir, searcher);
FacetsCollector sfc = performSearch(searcher);

// Obtain facets results and hand-test them
assertCorrectResults(getTaxonomyFacetCounts(tr, config, sfc));
Expand Down Expand Up @@ -124,7 +124,7 @@ public void testCustom() throws Exception {
// prepare searcher to search against
IndexSearcher searcher = newSearcher(ir);

FacetsCollector sfc = performSearch(tr, ir, searcher);
FacetsCollector sfc = performSearch(searcher);

Map<String, Facets> facetsMap = new HashMap<>();
facetsMap.put("Author", getTaxonomyFacetCounts(tr, config, sfc, "$author"));
Expand Down Expand Up @@ -168,7 +168,7 @@ public void testTwoCustomsSameField() throws Exception {
// prepare searcher to search against
IndexSearcher searcher = newSearcher(ir);

FacetsCollector sfc = performSearch(tr, ir, searcher);
FacetsCollector sfc = performSearch(searcher);

Map<String, Facets> facetsMap = new HashMap<>();
Facets facets2 = getTaxonomyFacetCounts(tr, config, sfc, "$music");
Expand Down Expand Up @@ -225,7 +225,7 @@ public void testDifferentFieldsAndText() throws Exception {
// prepare searcher to search against
IndexSearcher searcher = newSearcher(ir);

FacetsCollector sfc = performSearch(tr, ir, searcher);
FacetsCollector sfc = performSearch(searcher);

Map<String, Facets> facetsMap = new HashMap<>();
facetsMap.put("Band", getTaxonomyFacetCounts(tr, config, sfc, "$bands"));
Expand Down Expand Up @@ -271,7 +271,7 @@ public void testSomeSameSomeDifferent() throws Exception {
// prepare searcher to search against
IndexSearcher searcher = newSearcher(ir);

FacetsCollector sfc = performSearch(tr, ir, searcher);
FacetsCollector sfc = performSearch(searcher);

Map<String, Facets> facetsMap = new HashMap<>();
Facets facets2 = getTaxonomyFacetCounts(tr, config, sfc, "$music");
Expand Down Expand Up @@ -300,9 +300,8 @@ private void assertCorrectResults(Facets facets) throws IOException {
new String[0],
2,
5,
new LabelAndValue[] {
new LabelAndValue("Punk", 1), new LabelAndValue("Rock & Pop", 4),
});
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());
Expand All @@ -312,12 +311,10 @@ private void assertCorrectResults(Facets facets) throws IOException {
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),
});
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",
Expand All @@ -328,15 +325,12 @@ private void assertCorrectResults(Facets facets) throws IOException {
new String[0],
3,
3,
new LabelAndValue[] {
new LabelAndValue("Kurt Vonnegut", 1),
new LabelAndValue("Mark Twain", 1),
new LabelAndValue("Stephen King", 1),
});
new LabelAndValue("Kurt Vonnegut", 1),
new LabelAndValue("Mark Twain", 1),
new LabelAndValue("Stephen King", 1));
}

private FacetsCollector performSearch(TaxonomyReader tr, IndexReader ir, IndexSearcher searcher)
throws IOException {
private FacetsCollector performSearch(IndexSearcher searcher) throws IOException {
FacetsCollector fc = new FacetsCollector();
FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);
return fc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void testRandomSampling() throws Exception {
float ei = (float) md.totalHits / totalHits;
if (ei > 0.0f) {
float oi = (float) numSampledDocs[i] / totalSampledDocs;
chi_square += (Math.pow(ei - oi, 2) / ei);
chi_square += (float) (Math.pow(ei - oi, 2) / ei);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,7 @@ private void checkTopNFacetResult(
assertEquals(facetResult, topNDimsResult.get(0));

// test getAllDims(0)
expectThrows(
IllegalArgumentException.class,
() -> {
facets.getAllDims(0);
});
expectThrows(IllegalArgumentException.class, () -> facets.getAllDims(0));

// This is a little strange, but we request all labels at this point so that when we
// secondarily sort by label value in order to compare to the expected results, we have
Expand Down Expand Up @@ -538,8 +534,7 @@ private void checkAllChildrenFacetResult(

// sort expected counts by value, count
expectedCountsSortedByValue.sort(
Comparator.comparing((Map.Entry<String, Integer> a) -> a.getKey())
.thenComparingInt(Map.Entry::getValue));
Map.Entry.<String, Integer>comparingByKey().thenComparingInt(Map.Entry::getValue));

FacetResult facetResult = facets.getAllChildren("field");
assertEquals(expectedTotalDocsWithValue, facetResult.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,7 @@ public void testBasicLong() throws Exception {
result.toString());

// test getTopChildren(0, dim)
expectThrows(
IllegalArgumentException.class,
() -> {
facets.getTopChildren(0, "field");
});
expectThrows(IllegalArgumentException.class, () -> facets.getTopChildren(0, "field"));

r.close();
d.close();
Expand Down Expand Up @@ -169,11 +165,7 @@ public void testBasicLongMultiValued() throws Exception {
result.toString());

// test getTopChildren(0, dim)
expectThrows(
IllegalArgumentException.class,
() -> {
facets.getTopChildren(0, "field");
});
expectThrows(IllegalArgumentException.class, () -> facets.getTopChildren(0, "field"));

r.close();
d.close();
Expand Down Expand Up @@ -287,37 +279,19 @@ public void testLongGetAllDims() throws Exception {
assertEquals(0, topNDimsResult.size());

// test getAllDims(0)
expectThrows(
IllegalArgumentException.class,
() -> {
facets.getAllDims(0);
});
expectThrows(IllegalArgumentException.class, () -> facets.getAllDims(0));

r.close();
d.close();
}

public void testUselessRange() {
expectThrows(IllegalArgumentException.class, () -> new LongRange("useless", 7, true, 6, true));
expectThrows(IllegalArgumentException.class, () -> new LongRange("useless", 7, true, 7, false));
expectThrows(
IllegalArgumentException.class,
() -> {
new LongRange("useless", 7, true, 6, true);
});
expectThrows(
IllegalArgumentException.class,
() -> {
new LongRange("useless", 7, true, 7, false);
});
expectThrows(
IllegalArgumentException.class,
() -> {
new DoubleRange("useless", 7.0, true, 6.0, true);
});
IllegalArgumentException.class, () -> new DoubleRange("useless", 7.0, true, 6.0, true));
expectThrows(
IllegalArgumentException.class,
() -> {
new DoubleRange("useless", 7.0, true, 7.0, false);
});
IllegalArgumentException.class, () -> new DoubleRange("useless", 7.0, true, 7.0, false));
}

public void testLongMinMax() throws Exception {
Expand Down
Loading

0 comments on commit 49e7810

Please sign in to comment.