Search in sources :

Example 96 with MappedFieldType

use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.

the class BucketScriptAggregatorTests method testScript.

public void testScript() throws IOException {
    MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number_field", NumberFieldMapper.NumberType.INTEGER);
    MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType("the_field");
    FiltersAggregationBuilder filters = new FiltersAggregationBuilder("placeholder", new MatchAllQueryBuilder()).subAggregation(new TermsAggregationBuilder("the_terms").userValueTypeHint(ValueType.STRING).field("the_field").subAggregation(new AvgAggregationBuilder("the_avg").field("number_field"))).subAggregation(new BucketScriptPipelineAggregationBuilder("bucket_script", Collections.singletonMap("the_avg", "the_terms['test1']>the_avg.value"), new Script(ScriptType.INLINE, MockScriptEngine.NAME, SCRIPT_NAME, Collections.emptyMap())));
    testCase(filters, new MatchAllDocsQuery(), iw -> {
        Document doc = new Document();
        doc.add(new SortedSetDocValuesField("the_field", new BytesRef("test1")));
        doc.add(new SortedNumericDocValuesField("number_field", 19));
        iw.addDocument(doc);
        doc = new Document();
        doc.add(new SortedSetDocValuesField("the_field", new BytesRef("test2")));
        doc.add(new SortedNumericDocValuesField("number_field", 55));
        iw.addDocument(doc);
    }, f -> {
        assertThat(((InternalSimpleValue) (f.getBuckets().get(0).getAggregations().get("bucket_script"))).value, equalTo(19.0));
    }, fieldType, fieldType1);
}
Also used : Script(org.opensearch.script.Script) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Document(org.apache.lucene.document.Document) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) AvgAggregationBuilder(org.opensearch.search.aggregations.metrics.AvgAggregationBuilder) BytesRef(org.apache.lucene.util.BytesRef) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 97 with MappedFieldType

use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.

the class StatsAggregatorTests method randomLongsTestCase.

private void randomLongsTestCase(int valuesPerField, StatsAggregationBuilder builder, BiConsumer<SimpleStatsAggregator, InternalStats> verify) throws IOException {
    final MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.LONG);
    final int numDocs = randomIntBetween(10, 50);
    final List<Set<IndexableField>> docs = new ArrayList<>(numDocs);
    final SimpleStatsAggregator expected = new SimpleStatsAggregator();
    for (int iDoc = 0; iDoc < numDocs; iDoc++) {
        List<Long> values = randomList(valuesPerField, valuesPerField, () -> randomLongBetween(-100, 100));
        docs.add(values.stream().map(value -> new SortedNumericDocValuesField(ft.name(), value)).collect(toSet()));
        values.forEach(expected::add);
    }
    testCase(builder, iw -> iw.addDocuments(docs), stats -> verify.accept(expected, stats), singleton(ft));
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Collectors.toSet(java.util.stream.Collectors.toSet) Collections.emptySet(java.util.Collections.emptySet) Set(java.util.Set) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) ArrayList(java.util.ArrayList)

Example 98 with MappedFieldType

use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.

the class StatsAggregatorTests method testEmpty.

// TODO: Script tests, should fail with defaultValuesSourceType disabled.
public void testEmpty() throws IOException {
    final MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.LONG);
    testCase(stats("_name").field(ft.name()), iw -> {
    }, stats -> {
        assertEquals(0d, stats.getCount(), 0);
        assertEquals(0d, stats.getSum(), 0);
        assertEquals(Float.NaN, stats.getAvg(), 0);
        assertEquals(Double.POSITIVE_INFINITY, stats.getMin(), 0);
        assertEquals(Double.NEGATIVE_INFINITY, stats.getMax(), 0);
        assertFalse(AggregationInspectionHelper.hasValue(stats));
    }, singleton(ft));
}
Also used : MappedFieldType(org.opensearch.index.mapper.MappedFieldType)

Example 99 with MappedFieldType

use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.

the class StatsAggregatorTests method testCase.

private void testCase(StatsAggregationBuilder builder, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalStats> verify, Collection<MappedFieldType> fieldTypes) throws IOException {
    try (Directory directory = newDirectory();
        RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
        buildIndex.accept(indexWriter);
        try (IndexReader reader = indexWriter.getReader()) {
            IndexSearcher searcher = new IndexSearcher(reader);
            final MappedFieldType[] fieldTypesArray = fieldTypes.toArray(new MappedFieldType[0]);
            final InternalStats stats = searchAndReduce(searcher, new MatchAllDocsQuery(), builder, fieldTypesArray);
            verify.accept(stats);
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 100 with MappedFieldType

use of org.opensearch.index.mapper.MappedFieldType in project OpenSearch by opensearch-project.

the class SumAggregatorTests method testPartiallyUnmapped.

public void testPartiallyUnmapped() throws IOException {
    final MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(FIELD_NAME, NumberType.LONG);
    final SumAggregationBuilder builder = sum("_name").field(fieldType.name());
    final int numDocs = randomIntBetween(10, 100);
    final List<Set<IndexableField>> docs = new ArrayList<>(numDocs);
    int sum = 0;
    for (int i = 0; i < numDocs; i++) {
        final long value = randomLongBetween(0, 1000);
        sum += (int) value;
        docs.add(singleton(new NumericDocValuesField(fieldType.name(), value)));
    }
    try (Directory mappedDirectory = newDirectory();
        Directory unmappedDirectory = newDirectory()) {
        try (RandomIndexWriter mappedWriter = new RandomIndexWriter(random(), mappedDirectory)) {
            mappedWriter.addDocuments(docs);
        }
        new RandomIndexWriter(random(), unmappedDirectory).close();
        try (IndexReader mappedReader = DirectoryReader.open(mappedDirectory);
            IndexReader unmappedReader = DirectoryReader.open(unmappedDirectory);
            MultiReader multiReader = new MultiReader(mappedReader, unmappedReader)) {
            final IndexSearcher searcher = newSearcher(multiReader, true, true);
            final InternalSum internalSum = searchAndReduce(searcher, new MatchAllDocsQuery(), builder, fieldType);
            assertEquals(sum, internalSum.getValue(), 0d);
            assertTrue(AggregationInspectionHelper.hasValue(internalSum));
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Set(java.util.Set) HashSet(java.util.HashSet) MultiReader(org.apache.lucene.index.MultiReader) ArrayList(java.util.ArrayList) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

MappedFieldType (org.opensearch.index.mapper.MappedFieldType)321 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)150 IndexReader (org.apache.lucene.index.IndexReader)146 Directory (org.apache.lucene.store.Directory)136 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)130 RandomIndexWriter (org.apache.lucene.tests.index.RandomIndexWriter)127 IndexSearcher (org.apache.lucene.search.IndexSearcher)124 Document (org.apache.lucene.document.Document)96 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)83 ArrayList (java.util.ArrayList)76 BytesRef (org.apache.lucene.util.BytesRef)64 Query (org.apache.lucene.search.Query)57 Script (org.opensearch.script.Script)51 IntPoint (org.apache.lucene.document.IntPoint)49 IOException (java.io.IOException)48 List (java.util.List)48 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)48 LongPoint (org.apache.lucene.document.LongPoint)47 NumberFieldMapper (org.opensearch.index.mapper.NumberFieldMapper)46 KeywordFieldMapper (org.opensearch.index.mapper.KeywordFieldMapper)42