Search in sources :

Example 1 with ValueType

use of org.opensearch.search.aggregations.support.ValueType in project OpenSearch by opensearch-project.

the class TermsAggregatorTests method testUnmappedWithMissing.

public void testUnmappedWithMissing() throws Exception {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            Document document = new Document();
            document.add(new NumericDocValuesField("unrelated_value", 100));
            indexWriter.addDocument(document);
            try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
                MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType("unrelated_value");
                IndexSearcher indexSearcher = newIndexSearcher(indexReader);
                ValueType[] valueTypes = new ValueType[] { ValueType.STRING, ValueType.LONG, ValueType.DOUBLE };
                String[] fieldNames = new String[] { "string", "long", "double" };
                Object[] missingValues = new Object[] { "abc", 19L, 19.2 };
                for (int i = 0; i < fieldNames.length; i++) {
                    TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name").userValueTypeHint(valueTypes[i]).field(fieldNames[i]).missing(missingValues[i]);
                    Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType1);
                    aggregator.preCollection();
                    indexSearcher.search(new MatchAllDocsQuery(), aggregator);
                    aggregator.postCollection();
                    Terms result = reduce(aggregator);
                    assertEquals("_name", result.getName());
                    assertEquals(1, result.getBuckets().size());
                    assertEquals(missingValues[i], result.getBuckets().get(0).getKey());
                    assertEquals(1, result.getBuckets().get(0).getDocCount());
                }
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ValueType(org.opensearch.search.aggregations.support.ValueType) Aggregator(org.opensearch.search.aggregations.Aggregator) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) GeoPoint(org.opensearch.common.geo.GeoPoint) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 2 with ValueType

use of org.opensearch.search.aggregations.support.ValueType in project OpenSearch by opensearch-project.

the class CompositeValuesSourceParserHelper method declareValuesSourceFields.

static <VB extends CompositeValuesSourceBuilder<VB>, T> void declareValuesSourceFields(AbstractObjectParser<VB, T> objectParser) {
    objectParser.declareField(VB::field, XContentParser::text, new ParseField("field"), ObjectParser.ValueType.STRING);
    objectParser.declareBoolean(VB::missingBucket, new ParseField("missing_bucket"));
    objectParser.declareString(VB::missingOrder, new ParseField(MissingOrder.NAME));
    objectParser.declareField(VB::userValuetypeHint, p -> {
        ValueType valueType = ValueType.lenientParse(p.text());
        return valueType;
    }, new ParseField("value_type"), ObjectParser.ValueType.STRING);
    objectParser.declareField(VB::script, (parser, context) -> Script.parse(parser), Script.SCRIPT_PARSE_FIELD, ObjectParser.ValueType.OBJECT_OR_STRING);
    objectParser.declareField(VB::order, XContentParser::text, new ParseField("order"), ObjectParser.ValueType.STRING);
}
Also used : ValueType(org.opensearch.search.aggregations.support.ValueType) XContentParser(org.opensearch.common.xcontent.XContentParser) ParseField(org.opensearch.common.ParseField)

Example 3 with ValueType

use of org.opensearch.search.aggregations.support.ValueType in project OpenSearch by opensearch-project.

the class TermsAggregatorTests method testUnmapped.

public void testUnmapped() throws Exception {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
                IndexSearcher indexSearcher = newIndexSearcher(indexReader);
                ValueType[] valueTypes = new ValueType[] { ValueType.STRING, ValueType.LONG, ValueType.DOUBLE };
                String[] fieldNames = new String[] { "string", "long", "double" };
                for (int i = 0; i < fieldNames.length; i++) {
                    TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name").userValueTypeHint(valueTypes[i]).field(fieldNames[i]);
                    Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, (MappedFieldType) null);
                    aggregator.preCollection();
                    indexSearcher.search(new MatchAllDocsQuery(), aggregator);
                    aggregator.postCollection();
                    Terms result = reduce(aggregator);
                    assertEquals("_name", result.getName());
                    assertEquals(0, result.getBuckets().size());
                    assertFalse(AggregationInspectionHelper.hasValue((InternalTerms) result));
                }
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ValueType(org.opensearch.search.aggregations.support.ValueType) Aggregator(org.opensearch.search.aggregations.Aggregator) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) GeoPoint(org.opensearch.common.geo.GeoPoint) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 4 with ValueType

use of org.opensearch.search.aggregations.support.ValueType in project OpenSearch by opensearch-project.

the class ValueCountAggregatorTests method testNoDocs.

public void testNoDocs() throws IOException {
    for (ValueType valueType : ValueType.values()) {
        testAggregation(new MatchAllDocsQuery(), valueType, iw -> {
        // Intentionally not writing any docs
        }, count -> {
            assertEquals(0L, count.getValue());
            assertFalse(AggregationInspectionHelper.hasValue(count));
        });
    }
}
Also used : ValueType(org.opensearch.search.aggregations.support.ValueType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

ValueType (org.opensearch.search.aggregations.support.ValueType)4 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 InetAddressPoint (org.apache.lucene.document.InetAddressPoint)2 IndexReader (org.apache.lucene.index.IndexReader)2 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 Directory (org.apache.lucene.store.Directory)2 GeoPoint (org.opensearch.common.geo.GeoPoint)2 Aggregator (org.opensearch.search.aggregations.Aggregator)2 Document (org.apache.lucene.document.Document)1 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)1 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)1 ParseField (org.opensearch.common.ParseField)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)1