use of org.opensearch.index.mapper.RangeType in project OpenSearch by opensearch-project.
the class RangeHistogramAggregatorTests method testOffsetGtInterval.
public void testOffsetGtInterval() throws Exception {
RangeType rangeType = RangeType.DOUBLE;
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
for (RangeFieldMapper.Range range : new RangeFieldMapper.Range[] { // bucket 0 5
new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true), // bucket -5, 0
new RangeFieldMapper.Range(rangeType, -3.1, 4.2, true, true), // bucket 0, 5, 10
new RangeFieldMapper.Range(rangeType, 4.2, 13.3, true, true), // bucket 20, 25
new RangeFieldMapper.Range(rangeType, 22.5, 29.3, true, true) }) {
Document doc = new Document();
BytesRef encodedRange = rangeType.encodeRanges(Collections.singleton(range));
doc.add(new BinaryDocValuesField("field", encodedRange));
w.addDocument(doc);
}
// I'd like to randomize the offset here, like I did in the test for the numeric side, but there's no way I can think of to
// construct the intervals such that they wouldn't "slosh" between buckets.
final double offset = 20;
final double interval = 5;
final double expectedOffset = offset % interval;
HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg").field("field").interval(interval).offset(offset);
try (IndexReader reader = w.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
InternalHistogram histogram = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, rangeField("field", rangeType));
assertEquals(7, histogram.getBuckets().size());
assertEquals(-5d + expectedOffset, histogram.getBuckets().get(0).getKey());
assertEquals(1, histogram.getBuckets().get(0).getDocCount());
assertEquals(0d + expectedOffset, histogram.getBuckets().get(1).getKey());
assertEquals(3, histogram.getBuckets().get(1).getDocCount());
assertEquals(5d + expectedOffset, histogram.getBuckets().get(2).getKey());
assertEquals(2, histogram.getBuckets().get(2).getDocCount());
assertEquals(10d + expectedOffset, histogram.getBuckets().get(3).getKey());
assertEquals(1, histogram.getBuckets().get(3).getDocCount());
assertEquals(15d + expectedOffset, histogram.getBuckets().get(4).getKey());
assertEquals(0, histogram.getBuckets().get(4).getDocCount());
assertEquals(20d + expectedOffset, histogram.getBuckets().get(5).getKey());
assertEquals(1, histogram.getBuckets().get(5).getDocCount());
assertEquals(25d + expectedOffset, histogram.getBuckets().get(6).getKey());
assertEquals(1, histogram.getBuckets().get(6).getDocCount());
}
}
}
use of org.opensearch.index.mapper.RangeType in project OpenSearch by opensearch-project.
the class MissingAggregatorTests method testMatchSparseRangeField.
public void testMatchSparseRangeField() throws IOException {
final RangeType rangeType = RangeType.DOUBLE;
final MappedFieldType aggFieldType = new RangeFieldMapper.RangeFieldType("agg_field", rangeType);
final MappedFieldType anotherFieldType = new RangeFieldMapper.RangeFieldType("another_field", rangeType);
final RangeFieldMapper.Range range = new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true);
final BytesRef encodedRange = rangeType.encodeRanges(singleton(range));
final BinaryDocValuesField encodedRangeField = new BinaryDocValuesField(aggFieldType.name(), encodedRange);
final MissingAggregationBuilder builder = new MissingAggregationBuilder("_name").field(aggFieldType.name());
final int numDocs = randomIntBetween(100, 200);
int docsMissingAggField = 0;
final List<Set<IndexableField>> docs = new ArrayList<>();
for (int i = 0; i < numDocs; i++) {
if (randomBoolean()) {
docs.add(singleton(encodedRangeField));
} else {
docs.add(singleton(new SortedNumericDocValuesField(anotherFieldType.name(), randomLong())));
docsMissingAggField++;
}
}
final int finalDocsMissingAggField = docsMissingAggField;
testCase(newMatchAllQuery(), builder, writer -> writer.addDocuments(docs), internalMissing -> {
assertEquals(finalDocsMissingAggField, internalMissing.getDocCount());
assertTrue(AggregationInspectionHelper.hasValue(internalMissing));
}, Arrays.asList(aggFieldType, anotherFieldType));
}
use of org.opensearch.index.mapper.RangeType in project OpenSearch by opensearch-project.
the class SignificantTermsAggregatorTests method testRangeField.
/**
* Uses the significant terms aggregation on a range field
*/
public void testRangeField() throws IOException {
RangeType rangeType = RangeType.DOUBLE;
final RangeFieldMapper.Range range1 = new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true);
final RangeFieldMapper.Range range2 = new RangeFieldMapper.Range(rangeType, 6.0D, 10.0D, true, true);
final String fieldName = "rangeField";
MappedFieldType fieldType = new RangeFieldMapper.RangeFieldType(fieldName, rangeType);
IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
indexWriterConfig.setMaxBufferedDocs(100);
// flush on open to have a single segment
indexWriterConfig.setRAMBufferSizeMB(100);
try (Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, indexWriterConfig)) {
for (RangeFieldMapper.Range range : new RangeFieldMapper.Range[] { new RangeFieldMapper.Range(rangeType, 1L, 5L, true, true), new RangeFieldMapper.Range(rangeType, -3L, 4L, true, true), new RangeFieldMapper.Range(rangeType, 4L, 13L, true, true), new RangeFieldMapper.Range(rangeType, 42L, 49L, true, true) }) {
Document doc = new Document();
BytesRef encodedRange = rangeType.encodeRanges(Collections.singleton(range));
doc.add(new BinaryDocValuesField("field", encodedRange));
w.addDocument(doc);
}
// Attempt aggregation on range field
SignificantTermsAggregationBuilder sigAgg = new SignificantTermsAggregationBuilder("sig_text").field(fieldName);
sigAgg.executionHint(randomExecutionHint());
try (IndexReader reader = DirectoryReader.open(w)) {
IndexSearcher indexSearcher = newIndexSearcher(reader);
expectThrows(IllegalArgumentException.class, () -> createAggregator(sigAgg, indexSearcher, fieldType));
}
}
}
use of org.opensearch.index.mapper.RangeType in project OpenSearch by opensearch-project.
the class RareTermsAggregatorTests method testRangeField.
public void testRangeField() throws Exception {
RangeType rangeType = RangeType.DOUBLE;
try (Directory directory = newDirectory()) {
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
for (RangeFieldMapper.Range range : new RangeFieldMapper.Range[] { // bucket 0 5
new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true), // bucket -5, 0
new RangeFieldMapper.Range(rangeType, -3.1, 4.2, true, true), // bucket 0, 5, 10
new RangeFieldMapper.Range(rangeType, 4.2, 13.3, true, true), // bucket 40, 45
new RangeFieldMapper.Range(rangeType, 42.5, 49.3, true, true) }) {
Document doc = new Document();
BytesRef encodedRange = rangeType.encodeRanges(Collections.singleton(range));
doc.add(new BinaryDocValuesField("field", encodedRange));
indexWriter.addDocument(doc);
}
MappedFieldType fieldType = new RangeFieldMapper.RangeFieldType("field", rangeType);
try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
IndexSearcher indexSearcher = newIndexSearcher(indexReader);
RareTermsAggregationBuilder aggregationBuilder = new RareTermsAggregationBuilder("_name").field("field");
expectThrows(IllegalArgumentException.class, () -> createAggregator(aggregationBuilder, indexSearcher, fieldType));
}
}
}
}
use of org.opensearch.index.mapper.RangeType in project OpenSearch by opensearch-project.
the class ValueCountAggregatorTests method testRangeFieldValues.
public void testRangeFieldValues() throws IOException {
RangeType rangeType = RangeType.DOUBLE;
final RangeFieldMapper.Range range1 = new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true);
final RangeFieldMapper.Range range2 = new RangeFieldMapper.Range(rangeType, 6.0D, 10.0D, true, true);
final String fieldName = "rangeField";
MappedFieldType fieldType = new RangeFieldMapper.RangeFieldType(fieldName, rangeType);
final ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("_name").field(fieldName);
Set<RangeFieldMapper.Range> multiRecord = new HashSet<>(2);
multiRecord.add(range1);
multiRecord.add(range2);
testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range1)))));
iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range1)))));
iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range2)))));
iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(multiRecord))));
}, count -> {
assertEquals(4.0, count.getValue(), 0);
assertTrue(AggregationInspectionHelper.hasValue(count));
}, fieldType);
}
Aggregations