Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
setup:
- do:
indices.create:
index: test
body:
settings:
number_of_replicas: 0
mappings:
properties:
field1:
type: long
field2:
type: long
field3:
type: long
- do:
bulk:
refresh: true
body:
- index:
_index: test
- field1: 100
- index:
_index: test
- field1: 200
- index:
_index: test
- field1: 300
field2: 300

---
"match all":

- do:
search:
rest_total_hits_as_int: true
body:
aggs:
missing_agg:
missing:
field: field3

- match: { hits.total: 3 }
- length: { hits.hits: 3 }

- match: { aggregations.missing_agg.doc_count: 3 }

---
"match some":

- do:
search:
rest_total_hits_as_int: true
body:
aggs:
missing_agg:
missing:
field: field2

- match: { hits.total: 3 }
- length: { hits.hits: 3 }

- match: { aggregations.missing_agg.doc_count: 2 }

---
"match none":

- do:
search:
rest_total_hits_as_int: true
body:
aggs:
missing_agg:
missing:
field: field1

- match: { hits.total: 3 }
- length: { hits.hits: 3 }

- match: { aggregations.missing_agg.doc_count: 0 }

---
"missing param":

- do:
search:
rest_total_hits_as_int: true
body:
aggs:
missing_agg:
missing:
field: field3
missing: 1

- match: { hits.total: 3 }
- length: { hits.hits: 3 }

- match: { aggregations.missing_agg.doc_count: 0 }
Original file line number Diff line number Diff line change
Expand Up @@ -28,81 +28,115 @@
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.NumberFieldMapper;
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType;
import org.elasticsearch.index.mapper.RangeFieldMapper;
import org.elasticsearch.index.mapper.RangeType;
import org.elasticsearch.search.aggregations.AggregatorTestCase;
import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper;

import java.io.IOException;
import java.util.Collections;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import static java.util.Collections.singleton;


public class MissingAggregatorTests extends AggregatorTestCase {
public void testMatchNoDocs() throws IOException {
int numDocs = randomIntBetween(10, 200);
final MappedFieldType fieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType();
fieldType.setName("field");
testBothCases(numDocs,
"field",
fieldType.name(),
Queries.newMatchAllQuery(),
doc -> doc.add(new SortedNumericDocValuesField("field", randomLong())),
builder -> {},
(index, doc) -> doc.add(new SortedNumericDocValuesField(fieldType.name(), randomLong())),
internalMissing -> {
assertEquals(internalMissing.getDocCount(), 0);
assertFalse(AggregationInspectionHelper.hasValue(internalMissing));
});
},
singleton(fieldType));
}

public void testMatchAllDocs() throws IOException {
int numDocs = randomIntBetween(10, 200);

final MappedFieldType fieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType();
fieldType.setName("field");
final MappedFieldType anotherFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType();
anotherFieldType.setName("another_field");

testBothCases(numDocs,
"field",
fieldType.name(),
Queries.newMatchAllQuery(),
doc -> doc.add(new SortedNumericDocValuesField("another_field", randomLong())),
builder -> {},
(index, doc) -> doc.add(new SortedNumericDocValuesField(anotherFieldType.name(), randomLong())),
internalMissing -> {
assertEquals(internalMissing.getDocCount(), numDocs);
assertTrue(AggregationInspectionHelper.hasValue(internalMissing));
});
},
List.of(fieldType, anotherFieldType));
}

public void testMatchSparse() throws IOException {
int numDocs = randomIntBetween(100, 200);
final AtomicInteger count = new AtomicInteger();

final MappedFieldType fieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType();
fieldType.setName("field");
final MappedFieldType anotherFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType();
anotherFieldType.setName("another_field");

testBothCases(numDocs,
"field",
fieldType.name(),
Queries.newMatchAllQuery(),
doc -> {
builder -> {},
(index, doc) -> {
if (randomBoolean()) {
doc.add(new SortedNumericDocValuesField("another_field", randomLong()));
doc.add(new SortedNumericDocValuesField(anotherFieldType.name(), randomLong()));
count.incrementAndGet();
} else {
doc.add(new SortedNumericDocValuesField("field", randomLong()));
doc.add(new SortedNumericDocValuesField(fieldType.name(), randomLong()));
}
},
internalMissing -> {
assertEquals(internalMissing.getDocCount(), count.get());
count.set(0);
assertTrue(AggregationInspectionHelper.hasValue(internalMissing));
});
},
List.of(fieldType, anotherFieldType));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: in some of the aggregator tests similar to this one (where there's a second field we expect to be absent sometimes), we don't pass a field type for the second field like above. If the test is missing the second field type, that field can't be read even if an aggregator was erroneously trying to collect it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should fix that, could you open a ticket so we don't forget please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #53250

}

public void testMatchSparseRangeField() throws IOException {
int numDocs = randomIntBetween(100, 200);
final AtomicInteger count = new AtomicInteger();

final String fieldName = "field";
RangeType rangeType = RangeType.DOUBLE;
final BinaryDocValuesField field = new BinaryDocValuesField(fieldName, rangeType.encodeRanges(Collections.singleton(
new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true))));
MappedFieldType fieldType = new RangeFieldMapper.Builder(fieldName, rangeType).fieldType();
final RangeType rangeType = RangeType.DOUBLE;
MappedFieldType fieldType = new RangeFieldMapper.Builder("_name", rangeType).fieldType();
fieldType.setName(fieldName);

final RangeFieldMapper.Range range = new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true);
final BytesRef encodedRange = rangeType.encodeRanges(singleton(range));
final BinaryDocValuesField field = new BinaryDocValuesField(fieldName, encodedRange);

final MappedFieldType anotherFieldType = new RangeFieldMapper.Builder("_name", rangeType).fieldType();
anotherFieldType.setName("another_field");

testBothCases(numDocs,
fieldName,
Queries.newMatchAllQuery(),
doc -> {
builder -> {},
(index, doc) -> {
if (randomBoolean()) {
doc.add(new SortedNumericDocValuesField("another_field", randomLong()));
doc.add(new SortedNumericDocValuesField(anotherFieldType.name(), randomLong()));
count.incrementAndGet();
} else {
doc.add(field);
Expand All @@ -112,63 +146,96 @@ public void testMatchSparseRangeField() throws IOException {
assertEquals(internalMissing.getDocCount(), count.get());
count.set(0);
assertTrue(AggregationInspectionHelper.hasValue(internalMissing));
}, fieldType);
},
List.of(fieldType, anotherFieldType));
}


public void testMissingField() throws IOException {
public void testUnmappedWithoutMissingParam() throws IOException {
int numDocs = randomIntBetween(10, 20);
final MappedFieldType fieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType();
fieldType.setName("field");
testBothCases(numDocs,
"unknown_field",
Queries.newMatchAllQuery(),
doc -> {
doc.add(new SortedNumericDocValuesField("field", randomLong()));
},
builder -> {},
(index, doc) -> doc.add(new SortedNumericDocValuesField(fieldType.name(), randomLong())),
internalMissing -> {
assertEquals(internalMissing.getDocCount(), numDocs);
assertTrue(AggregationInspectionHelper.hasValue(internalMissing));
});
},
singleton(fieldType));
}

private void testBothCases(int numDocs,
String fieldName,
Query query,
Consumer<Document> consumer,
Consumer<InternalMissing> verify) throws IOException {
NumberFieldMapper.Builder mapperBuilder = new NumberFieldMapper.Builder("_name",
NumberFieldMapper.NumberType.LONG);
final MappedFieldType fieldType = mapperBuilder.fieldType();
fieldType.setHasDocValues(true);
fieldType.setName(fieldName);
testBothCases(numDocs, fieldName, query, consumer, verify, fieldType);
public void testUnmappedWithMissingParam() throws IOException {
final int numDocs = randomIntBetween(10, 20);
final MappedFieldType fieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType();
fieldType.setName("field");
testBothCases(numDocs,
"unknown_field",
Queries.newMatchAllQuery(),
builder -> builder.missing(randomLong()),
(index, doc) -> doc.add(new SortedNumericDocValuesField(fieldType.name(), randomLong())),
internalMissing -> {
assertEquals(internalMissing.getDocCount(), 0);
assertFalse(AggregationInspectionHelper.hasValue(internalMissing));
},
singleton(fieldType));
}

public void testMissingParam() throws IOException {
final int numDocs = randomIntBetween(100, 200);

final MappedFieldType fieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType();
fieldType.setName("field");
final MappedFieldType anotherFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType();
anotherFieldType.setName("another_field");

testBothCases(numDocs,
anotherFieldType.name(),
Queries.newMatchAllQuery(),
builder -> builder.missing(randomLong()),
(index, doc) -> {
doc.add(new SortedNumericDocValuesField(fieldType.name(), randomLong()));
if (index % 10 == 0) {
doc.add(new SortedNumericDocValuesField(anotherFieldType.name(), randomLong()));
}
},
internalMissing -> {
assertEquals(internalMissing.getDocCount(), 0);
assertFalse(AggregationInspectionHelper.hasValue(internalMissing));
},
List.of(fieldType, anotherFieldType));
}

private void testBothCases(int numDocs,
String fieldName,
Query query,
Consumer<Document> consumer,
Consumer<MissingAggregationBuilder> builderConsumer,
BiConsumer<Integer, Document> documentConsumer,
Consumer<InternalMissing> verify,
MappedFieldType fieldType) throws IOException {
executeTestCase(numDocs, fieldName, query, consumer, verify, false, fieldType);
executeTestCase(numDocs, fieldName, query, consumer, verify, true, fieldType);
Collection<MappedFieldType> fieldTypes) throws IOException {
executeTestCase(numDocs, fieldName, query, builderConsumer, documentConsumer, verify, false, fieldTypes);
executeTestCase(numDocs, fieldName, query, builderConsumer, documentConsumer, verify, true, fieldTypes);

}

private void executeTestCase(int numDocs,
String fieldName,
Query query,
Consumer<Document> consumer,
Consumer<MissingAggregationBuilder> builderConsumer,
BiConsumer<Integer, Document> documentConsumer,
Consumer<InternalMissing> verify,
boolean reduced,
MappedFieldType fieldType) throws IOException {
Collection<MappedFieldType> fieldTypes) throws IOException {
try (Directory directory = newDirectory()) {
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
Document document = new Document();
for (int i = 0; i < numDocs; i++) {
if (frequently()) {
indexWriter.commit();
}
consumer.accept(document);
documentConsumer.accept(i, document);
indexWriter.addDocument(document);
document.clear();
}
Expand All @@ -179,12 +246,14 @@ private void executeTestCase(int numDocs,
newSearcher(indexReader, true, true);
MissingAggregationBuilder builder = new MissingAggregationBuilder("_name", null);
builder.field(fieldName);
builderConsumer.accept(builder);

final MappedFieldType[] fieldTypesArray = fieldTypes.toArray(new MappedFieldType[0]);
InternalMissing missing;
if (reduced) {
missing = searchAndReduce(indexSearcher, query, builder, fieldType);
missing = searchAndReduce(indexSearcher, query, builder, fieldTypesArray);
} else {
missing = search(indexSearcher, query, builder, fieldType);
missing = search(indexSearcher, query, builder, fieldTypesArray);
}
verify.accept(missing);
}
Expand Down