use of org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType in project OpenSearch by opensearch-project.
the class SignificantTermsAggregatorTests method testNumericSignificance.
/**
* Uses the significant terms aggregation to find the keywords in numeric
* fields
*/
public void testNumericSignificance() throws IOException {
NumberFieldType longFieldType = new NumberFieldMapper.NumberFieldType("long_field", NumberFieldMapper.NumberType.LONG);
TextFieldType textFieldType = new TextFieldType("text");
IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
indexWriterConfig.setMaxBufferedDocs(100);
// flush on open to have a single segment
indexWriterConfig.setRAMBufferSizeMB(100);
final long ODD_VALUE = 3;
final long EVEN_VALUE = 6;
final long COMMON_VALUE = 2;
try (Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, indexWriterConfig)) {
for (int i = 0; i < 10; i++) {
Document doc = new Document();
if (i % 2 == 0) {
addFields(doc, NumberType.LONG.createFields("long_field", ODD_VALUE, true, true, false));
doc.add(new Field("text", "odd", TextFieldMapper.Defaults.FIELD_TYPE));
} else {
addFields(doc, NumberType.LONG.createFields("long_field", EVEN_VALUE, true, true, false));
doc.add(new Field("text", "even", TextFieldMapper.Defaults.FIELD_TYPE));
}
addFields(doc, NumberType.LONG.createFields("long_field", COMMON_VALUE, true, true, false));
w.addDocument(doc);
}
SignificantTermsAggregationBuilder sigNumAgg = new SignificantTermsAggregationBuilder("sig_number").field("long_field");
sigNumAgg.executionHint(randomExecutionHint());
try (IndexReader reader = DirectoryReader.open(w)) {
assertEquals("test expects a single segment", 1, reader.leaves().size());
IndexSearcher searcher = new IndexSearcher(reader);
// Search "odd"
SignificantLongTerms terms = searchAndReduce(searcher, new TermQuery(new Term("text", "odd")), sigNumAgg, longFieldType);
assertEquals(1, terms.getBuckets().size());
assertThat(terms.getSubsetSize(), equalTo(5L));
assertNull(terms.getBucketByKey(Long.toString(EVEN_VALUE)));
assertNull(terms.getBucketByKey(Long.toString(COMMON_VALUE)));
assertNotNull(terms.getBucketByKey(Long.toString(ODD_VALUE)));
terms = searchAndReduce(searcher, new TermQuery(new Term("text", "even")), sigNumAgg, longFieldType);
assertEquals(1, terms.getBuckets().size());
assertThat(terms.getSubsetSize(), equalTo(5L));
assertNull(terms.getBucketByKey(Long.toString(ODD_VALUE)));
assertNull(terms.getBucketByKey(Long.toString(COMMON_VALUE)));
assertNotNull(terms.getBucketByKey(Long.toString(EVEN_VALUE)));
}
}
}
Aggregations