Search in sources :

Example 1 with DocValueFormat

use of org.opensearch.search.DocValueFormat in project OpenSearch by opensearch-project.

the class DateFieldMapperTests method testFetchDocValuesMillis.

public void testFetchDocValuesMillis() throws IOException {
    MapperService mapperService = createMapperService(fieldMapping(b -> b.field("type", "date").field("format", "strict_date_time||epoch_millis")));
    MappedFieldType ft = mapperService.fieldType("field");
    DocValueFormat format = ft.docValueFormat(null, null);
    String date = "2020-05-15T21:33:02.123Z";
    assertEquals(List.of(date), fetchFromDocValues(mapperService, ft, format, date));
    assertEquals(List.of(date), fetchFromDocValues(mapperService, ft, format, 1589578382123L));
}
Also used : Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) DocValueFormat(org.opensearch.search.DocValueFormat) IndexableField(org.apache.lucene.index.IndexableField) ZonedDateTime(java.time.ZonedDateTime) IOException(java.io.IOException) JavaVersion(org.opensearch.bootstrap.JavaVersion) ZoneId(java.time.ZoneId) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) DocValuesType(org.apache.lucene.index.DocValuesType) List(org.opensearch.common.collect.List) ZoneOffset(java.time.ZoneOffset) Matchers.containsString(org.hamcrest.Matchers.containsString) DateFormatter(org.opensearch.common.time.DateFormatter) TermVectorsService(org.opensearch.index.termvectors.TermVectorsService) DocValueFormat(org.opensearch.search.DocValueFormat) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with DocValueFormat

use of org.opensearch.search.DocValueFormat in project OpenSearch by opensearch-project.

the class InternalComposite method reduce.

@Override
public InternalAggregation reduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
    PriorityQueue<BucketIterator> pq = new PriorityQueue<>(aggregations.size());
    boolean earlyTerminated = false;
    for (InternalAggregation agg : aggregations) {
        InternalComposite sortedAgg = (InternalComposite) agg;
        earlyTerminated |= sortedAgg.earlyTerminated;
        BucketIterator it = new BucketIterator(sortedAgg.buckets);
        if (it.next() != null) {
            pq.add(it);
        }
    }
    InternalBucket lastBucket = null;
    List<InternalBucket> buckets = new ArrayList<>();
    List<InternalBucket> result = new ArrayList<>();
    while (pq.size() > 0) {
        BucketIterator bucketIt = pq.poll();
        if (lastBucket != null && bucketIt.current.compareKey(lastBucket) != 0) {
            InternalBucket reduceBucket = reduceBucket(buckets, reduceContext);
            buckets.clear();
            result.add(reduceBucket);
            if (result.size() >= size) {
                break;
            }
        }
        lastBucket = bucketIt.current;
        buckets.add(bucketIt.current);
        if (bucketIt.next() != null) {
            pq.add(bucketIt);
        }
    }
    if (buckets.size() > 0) {
        InternalBucket reduceBucket = reduceBucket(buckets, reduceContext);
        result.add(reduceBucket);
    }
    List<DocValueFormat> reducedFormats = formats;
    CompositeKey lastKey = null;
    if (result.size() > 0) {
        lastBucket = result.get(result.size() - 1);
        /* Attach the formats from the last bucket to the reduced composite
             * so that we can properly format the after key. */
        reducedFormats = lastBucket.formats;
        lastKey = lastBucket.getRawKey();
    }
    reduceContext.consumeBucketsAndMaybeBreak(result.size());
    return new InternalComposite(name, size, sourceNames, reducedFormats, result, lastKey, reverseMuls, missingOrders, earlyTerminated, metadata);
}
Also used : InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) DocValueFormat(org.opensearch.search.DocValueFormat) ArrayList(java.util.ArrayList) PriorityQueue(java.util.PriorityQueue)

Example 3 with DocValueFormat

use of org.opensearch.search.DocValueFormat in project OpenSearch by opensearch-project.

the class InternalComposite method reduceBucket.

@Override
protected InternalBucket reduceBucket(List<InternalBucket> buckets, ReduceContext context) {
    assert buckets.size() > 0;
    List<InternalAggregations> aggregations = new ArrayList<>(buckets.size());
    long docCount = 0;
    for (InternalBucket bucket : buckets) {
        docCount += bucket.docCount;
        aggregations.add(bucket.aggregations);
    }
    InternalAggregations aggs = InternalAggregations.reduce(aggregations, context);
    /* Use the formats from the bucket because they'll be right to format
         * the key. The formats on the InternalComposite doing the reducing are
         * just whatever formats make sense for *its* index. This can be real
         * trouble when the index doing the reducing is unmapped. */
    List<DocValueFormat> reducedFormats = buckets.get(0).formats;
    return new InternalBucket(sourceNames, reducedFormats, buckets.get(0).key, reverseMuls, missingOrders, docCount, aggs);
}
Also used : InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) DocValueFormat(org.opensearch.search.DocValueFormat) ArrayList(java.util.ArrayList)

Example 4 with DocValueFormat

use of org.opensearch.search.DocValueFormat in project OpenSearch by opensearch-project.

the class InternalComposite method doWriteTo.

@Override
protected void doWriteTo(StreamOutput out) throws IOException {
    out.writeVInt(size);
    out.writeStringCollection(sourceNames);
    for (DocValueFormat format : formats) {
        out.writeNamedWriteable(format);
    }
    out.writeIntArray(reverseMuls);
    if (out.getVersion().onOrAfter(Version.V_1_3_0)) {
        out.writeArray((output, order) -> order.writeTo(output), missingOrders);
    }
    out.writeList(buckets);
    out.writeBoolean(afterKey != null);
    if (afterKey != null) {
        afterKey.writeTo(out);
    }
    if (out.getVersion().onOrAfter(LegacyESVersion.V_7_6_0)) {
        out.writeBoolean(earlyTerminated);
    }
}
Also used : DocValueFormat(org.opensearch.search.DocValueFormat)

Example 5 with DocValueFormat

use of org.opensearch.search.DocValueFormat in project OpenSearch by opensearch-project.

the class TermsValuesSourceBuilder method register.

static void register(ValuesSourceRegistry.Builder builder) {
    builder.register(REGISTRY_KEY, org.opensearch.common.collect.List.of(CoreValuesSourceType.DATE, CoreValuesSourceType.NUMERIC, CoreValuesSourceType.BOOLEAN), (valuesSourceConfig, name, hasScript, format, missingBucket, missingOrder, order) -> {
        final DocValueFormat docValueFormat;
        if (format == null && valuesSourceConfig.valueSourceType() == CoreValuesSourceType.DATE) {
            // defaults to the raw format on date fields (preserve timestamp as longs).
            docValueFormat = DocValueFormat.RAW;
        } else {
            docValueFormat = valuesSourceConfig.format();
        }
        return new CompositeValuesSourceConfig(name, valuesSourceConfig.fieldType(), valuesSourceConfig.getValuesSource(), docValueFormat, order, missingBucket, missingOrder, hasScript, (BigArrays bigArrays, IndexReader reader, int size, LongConsumer addRequestCircuitBreakerBytes, CompositeValuesSourceConfig compositeValuesSourceConfig) -> {
            final ValuesSource.Numeric vs = (ValuesSource.Numeric) compositeValuesSourceConfig.valuesSource();
            if (vs.isFloatingPoint()) {
                return new DoubleValuesSource(bigArrays, compositeValuesSourceConfig.fieldType(), vs::doubleValues, compositeValuesSourceConfig.format(), compositeValuesSourceConfig.missingBucket(), compositeValuesSourceConfig.missingOrder(), size, compositeValuesSourceConfig.reverseMul());
            } else {
                final LongUnaryOperator rounding;
                rounding = LongUnaryOperator.identity();
                return new LongValuesSource(bigArrays, compositeValuesSourceConfig.fieldType(), vs::longValues, rounding, compositeValuesSourceConfig.format(), compositeValuesSourceConfig.missingBucket(), compositeValuesSourceConfig.missingOrder(), size, compositeValuesSourceConfig.reverseMul());
            }
        });
    }, false);
    builder.register(REGISTRY_KEY, org.opensearch.common.collect.List.of(CoreValuesSourceType.BYTES, CoreValuesSourceType.IP), (valuesSourceConfig, name, hasScript, format, missingBucket, missingOrder, order) -> new CompositeValuesSourceConfig(name, valuesSourceConfig.fieldType(), valuesSourceConfig.getValuesSource(), valuesSourceConfig.format(), order, missingBucket, missingOrder, hasScript, (BigArrays bigArrays, IndexReader reader, int size, LongConsumer addRequestCircuitBreakerBytes, CompositeValuesSourceConfig compositeValuesSourceConfig) -> {
        if (valuesSourceConfig.hasGlobalOrdinals() && reader instanceof DirectoryReader) {
            ValuesSource.Bytes.WithOrdinals vs = (ValuesSource.Bytes.WithOrdinals) compositeValuesSourceConfig.valuesSource();
            return new GlobalOrdinalValuesSource(bigArrays, compositeValuesSourceConfig.fieldType(), vs::globalOrdinalsValues, compositeValuesSourceConfig.format(), compositeValuesSourceConfig.missingBucket(), compositeValuesSourceConfig.missingOrder(), size, compositeValuesSourceConfig.reverseMul());
        } else {
            ValuesSource.Bytes vs = (ValuesSource.Bytes) compositeValuesSourceConfig.valuesSource();
            return new BinaryValuesSource(bigArrays, addRequestCircuitBreakerBytes, compositeValuesSourceConfig.fieldType(), vs::bytesValues, compositeValuesSourceConfig.format(), compositeValuesSourceConfig.missingBucket(), compositeValuesSourceConfig.missingOrder(), size, compositeValuesSourceConfig.reverseMul());
        }
    }), false);
}
Also used : DirectoryReader(org.apache.lucene.index.DirectoryReader) DocValueFormat(org.opensearch.search.DocValueFormat) ValuesSource(org.opensearch.search.aggregations.support.ValuesSource) BigArrays(org.opensearch.common.util.BigArrays) LongConsumer(java.util.function.LongConsumer) LongUnaryOperator(java.util.function.LongUnaryOperator) IndexReader(org.apache.lucene.index.IndexReader)

Aggregations

DocValueFormat (org.opensearch.search.DocValueFormat)80 ArrayList (java.util.ArrayList)16 SortField (org.apache.lucene.search.SortField)13 BytesRef (org.apache.lucene.util.BytesRef)10 BucketOrder (org.opensearch.search.aggregations.BucketOrder)10 FieldDoc (org.apache.lucene.search.FieldDoc)9 TopDocs (org.apache.lucene.search.TopDocs)8 TotalHits (org.apache.lucene.search.TotalHits)7 TopDocsAndMaxScore (org.opensearch.common.lucene.search.TopDocsAndMaxScore)7 BigArrays (org.opensearch.common.util.BigArrays)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 NoopCircuitBreaker (org.opensearch.common.breaker.NoopCircuitBreaker)6 HashSet (java.util.HashSet)5 IndexReader (org.apache.lucene.index.IndexReader)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 ShardId (org.opensearch.index.shard.ShardId)5 SearchShardTarget (org.opensearch.search.SearchShardTarget)5 SortAndFormats (org.opensearch.search.sort.SortAndFormats)5 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4