Search in sources :

Example 6 with NumberFieldType

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)));
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) NumberFieldType(org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType) Term(org.apache.lucene.index.Term) TextFieldType(org.opensearch.index.mapper.TextFieldMapper.TextFieldType) Document(org.apache.lucene.document.Document) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) StoredField(org.apache.lucene.document.StoredField) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Field(org.apache.lucene.document.Field) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) IndexReader(org.apache.lucene.index.IndexReader) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Directory(org.apache.lucene.store.Directory)

Aggregations

NumberFieldType (org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType)6 IndexNumericFieldData (org.opensearch.index.fielddata.IndexNumericFieldData)3 NumberType (org.opensearch.index.mapper.NumberFieldMapper.NumberType)3 IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 Collections (java.util.Collections)2 IndexWriter (org.apache.lucene.index.IndexWriter)2 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 Directory (org.apache.lucene.store.Directory)2 Mockito.any (org.mockito.Mockito.any)2 Mockito.anyInt (org.mockito.Mockito.anyInt)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.when (org.mockito.Mockito.when)2 LeafNumericFieldData (org.opensearch.index.fielddata.LeafNumericFieldData)2 SortedNumericDoubleValues (org.opensearch.index.fielddata.SortedNumericDoubleValues)2 MapperService (org.opensearch.index.mapper.MapperService)2 ScriptException (org.opensearch.script.ScriptException)2 SearchLookup (org.opensearch.search.lookup.SearchLookup)2 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)2