org.opensearch.search.aggregations.pipeline.BucketSortPipelineAggregator Maven / Gradle / Ivy
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.search.aggregations.pipeline;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.search.aggregations.InternalAggregation;
import org.opensearch.search.aggregations.InternalAggregation.ReduceContext;
import org.opensearch.search.aggregations.InternalMultiBucketAggregation;
import org.opensearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.opensearch.search.aggregations.pipeline.BucketHelpers.GapPolicy;
import org.opensearch.search.sort.FieldSortBuilder;
import org.opensearch.search.sort.SortOrder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BucketSortPipelineAggregator extends PipelineAggregator {
private final List sorts;
private final int from;
private final Integer size;
private final GapPolicy gapPolicy;
BucketSortPipelineAggregator(
String name,
List sorts,
int from,
Integer size,
GapPolicy gapPolicy,
Map metadata
) {
super(name, sorts.stream().map(FieldSortBuilder::getFieldName).toArray(String[]::new), metadata);
this.sorts = sorts;
this.from = from;
this.size = size;
this.gapPolicy = gapPolicy;
}
/**
* Read from a stream.
*/
public BucketSortPipelineAggregator(StreamInput in) throws IOException {
super(in);
sorts = in.readList(FieldSortBuilder::new);
from = in.readVInt();
size = in.readOptionalVInt();
gapPolicy = GapPolicy.readFrom(in);
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeList(sorts);
out.writeVInt(from);
out.writeOptionalVInt(size);
gapPolicy.writeTo(out);
}
@Override
public String getWriteableName() {
return BucketSortPipelineAggregationBuilder.NAME;
}
@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
InternalMultiBucketAggregation originalAgg =
(InternalMultiBucketAggregation) aggregation;
List buckets = originalAgg.getBuckets();
int bucketsCount = buckets.size();
int currentSize = size == null ? bucketsCount : size;
if (from >= bucketsCount) {
return originalAgg.create(Collections.emptyList());
}
// If no sorting needs to take place, we just truncate and return
if (sorts.size() == 0) {
return originalAgg.create(new ArrayList<>(buckets.subList(from, Math.min(from + currentSize, bucketsCount))));
}
List ordered = new ArrayList<>();
for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) {
ComparableBucket comparableBucket = new ComparableBucket(originalAgg, bucket);
if (comparableBucket.skip() == false) {
ordered.add(comparableBucket);
}
}
Collections.sort(ordered);
// We just have to get as many elements as we expect in results and store them in the same order starting from
// the specified offset and taking currentSize into consideration.
int limit = Math.min(from + currentSize, ordered.size());
List newBuckets = new ArrayList<>();
for (int i = from; i < limit; ++i) {
newBuckets.add(ordered.get(i).internalBucket);
}
return originalAgg.create(newBuckets);
}
private class ComparableBucket implements Comparable {
private final MultiBucketsAggregation parentAgg;
private final InternalMultiBucketAggregation.InternalBucket internalBucket;
private final Map> sortValues;
private ComparableBucket(MultiBucketsAggregation parentAgg, InternalMultiBucketAggregation.InternalBucket internalBucket) {
this.parentAgg = parentAgg;
this.internalBucket = internalBucket;
this.sortValues = resolveAndCacheSortValues();
}
private Map> resolveAndCacheSortValues() {
Map> resolved = new HashMap<>();
for (FieldSortBuilder sort : sorts) {
String sortField = sort.getFieldName();
if ("_key".equals(sortField)) {
resolved.put(sort, (Comparable