|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.search.aggregations; |
| 20 | + |
| 21 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 22 | +import org.elasticsearch.common.io.stream.Writeable; |
| 23 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 24 | +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket; |
| 25 | +import org.elasticsearch.search.aggregations.support.AggregationPath; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.Comparator; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +/** |
| 33 | + * {@link Bucket} Ordering strategy. |
| 34 | + */ |
| 35 | +public abstract class BucketOrder implements ToXContentObject, Writeable { |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates a bucket ordering strategy that sorts buckets by their document counts (ascending or descending). |
| 39 | + * |
| 40 | + * @param asc direction to sort by: {@code true} for ascending, {@code false} for descending. |
| 41 | + */ |
| 42 | + public static BucketOrder count(boolean asc) { |
| 43 | + return asc ? InternalOrder.COUNT_ASC : InternalOrder.COUNT_DESC; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a bucket ordering strategy that sorts buckets by their keys (ascending or descending). This may be |
| 48 | + * used as a tie-breaker to avoid non-deterministic ordering. |
| 49 | + * |
| 50 | + * @param asc direction to sort by: {@code true} for ascending, {@code false} for descending. |
| 51 | + */ |
| 52 | + public static BucketOrder key(boolean asc) { |
| 53 | + return asc ? InternalOrder.KEY_ASC : InternalOrder.KEY_DESC; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates a bucket ordering strategy which sorts buckets based on a single-valued sub-aggregation. |
| 58 | + * |
| 59 | + * @param path path to the sub-aggregation to sort on. |
| 60 | + * @param asc direction to sort by: {@code true} for ascending, {@code false} for descending. |
| 61 | + * @see AggregationPath |
| 62 | + */ |
| 63 | + public static BucketOrder aggregation(String path, boolean asc) { |
| 64 | + return new InternalOrder.Aggregation(path, asc); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates a bucket ordering strategy which sorts buckets based on a metric from a multi-valued sub-aggregation. |
| 69 | + * |
| 70 | + * @param path path to the sub-aggregation to sort on. |
| 71 | + * @param metricName name of the value of the multi-value metric to sort on. |
| 72 | + * @param asc direction to sort by: {@code true} for ascending, {@code false} for descending. |
| 73 | + * @see AggregationPath |
| 74 | + */ |
| 75 | + public static BucketOrder aggregation(String path, String metricName, boolean asc) { |
| 76 | + return new InternalOrder.Aggregation(path + "." + metricName, asc); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Creates a bucket ordering strategy which sorts buckets based on multiple criteria. A tie-breaker may be added to |
| 81 | + * avoid non-deterministic ordering. |
| 82 | + * |
| 83 | + * @param orders a list of {@link BucketOrder} objects to sort on, in order of priority. |
| 84 | + */ |
| 85 | + public static BucketOrder compound(List<BucketOrder> orders) { |
| 86 | + return new InternalOrder.CompoundOrder(orders); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Creates a bucket ordering strategy which sorts buckets based on multiple criteria. A tie-breaker may be added to |
| 91 | + * avoid non-deterministic ordering. |
| 92 | + * |
| 93 | + * @param orders a list of {@link BucketOrder} parameters to sort on, in order of priority. |
| 94 | + */ |
| 95 | + public static BucketOrder compound(BucketOrder... orders) { |
| 96 | + return compound(Arrays.asList(orders)); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return A comparator for the bucket based on the given aggregator. The comparator is used in two phases: |
| 101 | + * <p> |
| 102 | + * - aggregation phase, where each shard builds a list of buckets to be sent to the coordinating node. |
| 103 | + * In this phase, the passed in aggregator will be the aggregator that aggregates the buckets on the |
| 104 | + * shard level. |
| 105 | + * <p> |
| 106 | + * - reduce phase, where the coordinating node gathers all the buckets from all the shards and reduces them |
| 107 | + * to a final bucket list. In this case, the passed in aggregator will be {@code null}. |
| 108 | + */ |
| 109 | + public abstract Comparator<Bucket> comparator(Aggregator aggregator); |
| 110 | + |
| 111 | + /** |
| 112 | + * @return unique internal ID used for reading/writing this order from/to a stream. |
| 113 | + * @see InternalOrder.Streams |
| 114 | + */ |
| 115 | + abstract byte id(); |
| 116 | + |
| 117 | + @Override |
| 118 | + public abstract int hashCode(); |
| 119 | + |
| 120 | + @Override |
| 121 | + public abstract boolean equals(Object obj); |
| 122 | + |
| 123 | + @Override |
| 124 | + public void writeTo(StreamOutput out) throws IOException { |
| 125 | + InternalOrder.Streams.writeOrder(this, out); |
| 126 | + } |
| 127 | +} |
0 commit comments