Skip to content

Commit

Permalink
Add test case for _doc_count
Browse files Browse the repository at this point in the history
Signed-off-by: Sandesh Kumar <[email protected]>
  • Loading branch information
sandeshkr419 committed Mar 11, 2024
1 parent b7fa2c0 commit 0127731
Showing 1 changed file with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,38 +278,54 @@ public void testUsesGlobalOrdinalsByDefault() throws Exception {
}

/**
* This test case utilizes the default implementation of GlobalOrdinalsStringTermsAggregator.
* This test case utilizes the default implementation of GlobalOrdinalsStringTermsAggregator since collectSegmentOrds is false
*/
public void testSimpleAggregation() throws Exception {
// Fields not indexed: cannot use LeafBucketCollector#termDocFreqCollector - all documents are visited
testSimple(ADD_SORTED_SET_FIELD_NOT_INDEXED, false, false, TermsAggregatorFactory.ExecutionMode.GLOBAL_ORDINALS, 4);
testSimple(ADD_SORTED_SET_FIELD_NOT_INDEXED, false, false, false, TermsAggregatorFactory.ExecutionMode.GLOBAL_ORDINALS, 4);

// Fields indexed, deleted documents in segment: cannot use LeafBucketCollector#termDocFreqCollector - all documents are visited
testSimple(ADD_SORTED_SET_FIELD_INDEXED, true, false, TermsAggregatorFactory.ExecutionMode.GLOBAL_ORDINALS, 4);
testSimple(ADD_SORTED_SET_FIELD_INDEXED, true, false, false, TermsAggregatorFactory.ExecutionMode.GLOBAL_ORDINALS, 4);

// Fields indexed, no deleted documents in segment: will use LeafBucketCollector#termDocFreqCollector - no documents are visited
testSimple(ADD_SORTED_SET_FIELD_INDEXED, false, false, TermsAggregatorFactory.ExecutionMode.GLOBAL_ORDINALS, 0);
testSimple(ADD_SORTED_SET_FIELD_INDEXED, false, false, false, TermsAggregatorFactory.ExecutionMode.GLOBAL_ORDINALS, 0);

// Fields indexed, no deleted documents, but _doc_field value present in document:
// cannot use LeafBucketCollector#termDocFreqCollector - all documents are visited
testSimple(ADD_SORTED_SET_FIELD_INDEXED, false, true, false, TermsAggregatorFactory.ExecutionMode.GLOBAL_ORDINALS, 4);

}

/**
* This test case utilizes the LowCardinality implementation of GlobalOrdinalsStringTermsAggregator.
* This test case utilizes the LowCardinality implementation of GlobalOrdinalsStringTermsAggregator since collectSegmentOrds is true
*/
public void testSimpleAggregationLowCardinality() throws Exception {
// Fields not indexed: cannot use LeafBucketCollector#termDocFreqCollector - all documents are visited
testSimple(ADD_SORTED_SET_FIELD_NOT_INDEXED, false, true, TermsAggregatorFactory.ExecutionMode.GLOBAL_ORDINALS, 4);
testSimple(ADD_SORTED_SET_FIELD_NOT_INDEXED, false, false, true, TermsAggregatorFactory.ExecutionMode.GLOBAL_ORDINALS, 4);

// Fields indexed, deleted documents in segment: cannot use LeafBucketCollector#termDocFreqCollector - all documents are visited
testSimple(ADD_SORTED_SET_FIELD_INDEXED, true, true, TermsAggregatorFactory.ExecutionMode.GLOBAL_ORDINALS, 4);
testSimple(ADD_SORTED_SET_FIELD_INDEXED, true, false, true, TermsAggregatorFactory.ExecutionMode.GLOBAL_ORDINALS, 4);

// Fields indexed, no deleted documents in segment: will use LeafBucketCollector#termDocFreqCollector - no documents are visited
testSimple(ADD_SORTED_SET_FIELD_INDEXED, false, true, TermsAggregatorFactory.ExecutionMode.GLOBAL_ORDINALS, 0);
testSimple(ADD_SORTED_SET_FIELD_INDEXED, false, false, true, TermsAggregatorFactory.ExecutionMode.GLOBAL_ORDINALS, 0);

// Fields indexed, no deleted documents, but _doc_field value present in document:
// cannot use LeafBucketCollector#termDocFreqCollector - all documents are visited
testSimple(ADD_SORTED_SET_FIELD_INDEXED, false, true, true, TermsAggregatorFactory.ExecutionMode.GLOBAL_ORDINALS, 4);
}

/**
* This test case utilizes the MapStringTermsAggregator.
*/
public void testSimpleMapStringAggregation() throws Exception {
testSimple(ADD_SORTED_SET_FIELD_INDEXED, randomBoolean(), randomBoolean(), TermsAggregatorFactory.ExecutionMode.MAP, 4);
testSimple(
ADD_SORTED_SET_FIELD_INDEXED,
randomBoolean(),
randomBoolean(),
randomBoolean(),
TermsAggregatorFactory.ExecutionMode.MAP,
4
);
}

/**
Expand All @@ -323,6 +339,7 @@ public void testSimpleMapStringAggregation() throws Exception {
private void testSimple(
TriConsumer<Document, String, String> addFieldConsumer,
final boolean includeDeletedDocumentsInSegment,
final boolean includeDocCountField,
boolean collectSegmentOrds,
TermsAggregatorFactory.ExecutionMode executionMode,
final int expectedCollectCount
Expand All @@ -344,6 +361,10 @@ private void testSimple(
indexWriter.addDocument(document);
document = new Document();
addFieldConsumer.apply(document, "string", "");
if (includeDocCountField) {
// Adding _doc_count to one document
document.add(new NumericDocValuesField("_doc_count", 10));
}
indexWriter.addDocument(document);

if (includeDeletedDocumentsInSegment) {
Expand Down Expand Up @@ -373,7 +394,11 @@ private void testSimple(
Terms result = reduce(aggregator);
assertEquals(5, result.getBuckets().size());
assertEquals("", result.getBuckets().get(0).getKeyAsString());
assertEquals(2L, result.getBuckets().get(0).getDocCount());
if (includeDocCountField) {
assertEquals(11L, result.getBuckets().get(0).getDocCount());
} else {
assertEquals(2L, result.getBuckets().get(0).getDocCount());
}
assertEquals("a", result.getBuckets().get(1).getKeyAsString());
assertEquals(2L, result.getBuckets().get(1).getDocCount());
assertEquals("b", result.getBuckets().get(2).getKeyAsString());
Expand Down

0 comments on commit 0127731

Please sign in to comment.