LUCENE-9444: Improve test coverage for TaxonomyFacetLabels (#1928)

Co-authored-by: Ankur Goel <goankur@amazon.com>
This commit is contained in:
goankur 2020-09-30 10:21:18 -07:00 committed by GitHub
parent f8b7a60562
commit 2e2161b0e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 21 deletions

View File

@ -70,32 +70,51 @@ public abstract class FacetTestCase extends LuceneTestCase {
*
* @param taxoReader {@link TaxonomyReader} used to read taxonomy during search. This instance is expected to be open for reading.
* @param fc {@link FacetsCollector} A collector with matching hits.
* @return {@code List<List<FacetLabel>} where outer list has one non-null entry per document
* @param dimension facet dimension for which labels are requested. A null value fetches labels for all dimensions.
* @return {@code List<List<FacetLabel>} where outer list has one non-null entry per document.
* and inner list contain all {@link FacetLabel} entries that belong to a document.
* @throws IOException when a low-level IO issue occurs.
*/
public List<List<FacetLabel>> getAllTaxonomyFacetLabels(TaxonomyReader taxoReader, FacetsCollector fc) throws IOException {
public List<List<FacetLabel>> getAllTaxonomyFacetLabels(String dimension, TaxonomyReader taxoReader, FacetsCollector fc) throws IOException {
List<List<FacetLabel>> actualLabels = new ArrayList<>();
TaxonomyFacetLabels taxoLabels = new TaxonomyFacetLabels(taxoReader, FacetsConfig.DEFAULT_INDEX_FIELD_NAME);
for (MatchingDocs m : fc.getMatchingDocs()) {
FacetLabelReader facetLabelReader = taxoLabels.getFacetLabelReader(m.context);
DocIdSetIterator disi = m.bits.iterator();
while (disi.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
List<FacetLabel> facetLabels = new ArrayList<>();
int docId = disi.docID();
FacetLabel facetLabel = facetLabelReader.nextFacetLabel(docId);
while (facetLabel != null) {
facetLabels.add(facetLabel);
facetLabel = facetLabelReader.nextFacetLabel(docId);
}
actualLabels.add(facetLabels);
actualLabels.add(allFacetLabels(disi.docID(), dimension, facetLabelReader));
}
}
return actualLabels;
}
/**
* Utility method to get all facet labels for an input docId and dimension using the supplied
* {@link FacetLabelReader}.
*
* @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.
* @return {@code List<FacetLabel>} containing matching facet labels.
* @throws IOException when a low-level IO issue occurs while reading facet labels.
*/
List<FacetLabel> allFacetLabels(int docId, String dimension, FacetLabelReader facetLabelReader) throws IOException {
List<FacetLabel> facetLabels = new ArrayList<>();
FacetLabel facetLabel;
if (dimension != null) {
for (facetLabel = facetLabelReader.nextFacetLabel(docId, dimension); facetLabel != null; ) {
facetLabels.add(facetLabel);
facetLabel = facetLabelReader.nextFacetLabel(docId, dimension);
}
} else {
for (facetLabel = facetLabelReader.nextFacetLabel(docId); facetLabel != null; ) {
facetLabels.add(facetLabel);
facetLabel = facetLabelReader.nextFacetLabel(docId);
}
}
return facetLabels;
}
protected String[] getRandomTokens(int count) {
String[] tokens = new String[count];
for(int i=0;i<tokens.length;i++) {

View File

@ -22,7 +22,7 @@ import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -696,7 +696,6 @@ public class TestTaxonomyFacetCounts extends FacetTestCase {
} else {
expectedCounts[j].put(doc.dims[j], v.intValue() + 1);
}
// Add document facet labels
facetLabels.add(new FacetLabel("dim" + j, doc.dims[j]));
}
@ -719,11 +718,6 @@ public class TestTaxonomyFacetCounts extends FacetTestCase {
}
}
// Test facet labels for each matching test doc
List<List<FacetLabel>> actualLabels = getAllTaxonomyFacetLabels(tr, fc);
assertEquals(expectedLabels.size(), actualLabels.size());
assertTrue(sortedFacetLabels(expectedLabels).equals(sortedFacetLabels(actualLabels)));
// Sort by highest value, tie break by value:
sortFacetResults(expected);
@ -733,6 +727,18 @@ public class TestTaxonomyFacetCounts extends FacetTestCase {
sortTies(actual);
assertEquals(expected, actual);
// Test facet labels for each matching test doc
List<List<FacetLabel>> actualLabels = getAllTaxonomyFacetLabels(null, tr, fc);
assertEquals(expectedLabels.size(), actualLabels.size());
assertTrue(sortedFacetLabels(expectedLabels).equals(sortedFacetLabels(actualLabels)));
// Test facet labels for each matching test doc, given a specific dimension chosen randomly
final String dimension = "dim" + random().nextInt(numDims);
expectedLabels.forEach(list -> list.removeIf(f -> f.components[0].equals(dimension) == false));
actualLabels = getAllTaxonomyFacetLabels(dimension, tr, fc);
assertTrue(sortedFacetLabels(expectedLabels).equals(sortedFacetLabels(actualLabels)));
}
w.close();
@ -740,8 +746,8 @@ public class TestTaxonomyFacetCounts extends FacetTestCase {
}
private static List<List<FacetLabel>> sortedFacetLabels(List<List<FacetLabel>> allFacetLabels) {
// sort each inner list since there is no guaranteed order in which FacetLabels
// are expected to be retrieved for each document
// Sort each inner list since there is no guaranteed order in which
// FacetLabels are expected to be retrieved for each document.
for (List<FacetLabel> facetLabels : allFacetLabels) {
Collections.sort(facetLabels);
}