org.elasticsearch.search.aggregations.bucket.terms.Terms.Order Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.bucket.terms.Terms.Order. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static void writeOrder(Terms.Order order, StreamOutput out) throws IOException {
    if (order instanceof Aggregation) {
        out.writeByte(order.id());
        Aggregation aggregationOrder = (Aggregation) order;
        out.writeBoolean(((MultiBucketsAggregation.Bucket.SubAggregationComparator) aggregationOrder.comparator).asc());
        AggregationPath path = ((Aggregation) order).path();
        out.writeString(path.toString());
    } else if (order instanceof CompoundOrder) {
        CompoundOrder compoundOrder = (CompoundOrder) order;
            out.writeByte(order.id());
            out.writeVInt(compoundOrder.orderElements.size());
            for (Terms.Order innerOrder : compoundOrder.orderElements) {
                Streams.writeOrder(innerOrder, out);
            }
    } else {
        out.writeByte(order.id());
    }
}
 
Example #2
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static Terms.Order readOrder(StreamInput in, boolean absoluteOrder) throws IOException {
    byte id = in.readByte();
    switch (id) {
        case COUNT_DESC_ID: return absoluteOrder ? new CompoundOrder(Collections.singletonList((Terms.Order) InternalOrder.COUNT_DESC)) : InternalOrder.COUNT_DESC;
        case COUNT_ASC_ID: return absoluteOrder ? new CompoundOrder(Collections.singletonList((Terms.Order) InternalOrder.COUNT_ASC)) : InternalOrder.COUNT_ASC;
        case TERM_DESC_ID: return InternalOrder.TERM_DESC;
        case TERM_ASC_ID: return InternalOrder.TERM_ASC;
        case Aggregation.ID:
            boolean asc = in.readBoolean();
            String key = in.readString();
            return new InternalOrder.Aggregation(key, asc);
        case CompoundOrder.ID:
            int size = in.readVInt();
            List<Terms.Order> compoundOrder = new ArrayList<>(size);
            for (int i = 0; i < size; i++) {
                compoundOrder.add(Streams.readOrder(in, false));
            }
            return new CompoundOrder(compoundOrder, absoluteOrder);
        default:
            throw new RuntimeException("unknown terms order");
    }
}
 
Example #3
Source File: TermsParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    TermsParametersParser aggParser = new TermsParametersParser();
    ValuesSourceParser vsParser = ValuesSourceParser.any(aggregationName, StringTerms.TYPE, context).scriptable(true).formattable(true).build();
    IncludeExclude.Parser incExcParser = new IncludeExclude.Parser();
    aggParser.parse(aggregationName, parser, context, vsParser, incExcParser);

    List<OrderElement> orderElements = aggParser.getOrderElements();
    List<Terms.Order> orders = new ArrayList<>(orderElements.size());
    for (OrderElement orderElement : orderElements) {
        orders.add(resolveOrder(orderElement.key(), orderElement.asc()));
    }
    Terms.Order order;
    if (orders.size() == 1 && (orders.get(0) == InternalOrder.TERM_ASC || orders.get(0) == InternalOrder.TERM_DESC))
    {
        // If order is only terms order then we don't need compound ordering
        order = orders.get(0);
    }
    else
    {
        // for all other cases we need compound order so term order asc can be added to make the order deterministic
        order = Order.compound(orders);
    }
    TermsAggregator.BucketCountThresholds bucketCountThresholds = aggParser.getBucketCountThresholds();
    if (!(order == InternalOrder.TERM_ASC || order == InternalOrder.TERM_DESC)
            && bucketCountThresholds.getShardSize() == aggParser.getDefaultBucketCountThresholds().getShardSize()) {
        // The user has not made a shardSize selection. Use default heuristic to avoid any wrong-ranking caused by distributed counting
        bucketCountThresholds.setShardSize(BucketUtils.suggestShardSideQueueSize(bucketCountThresholds.getRequiredSize(),
                context.numberOfShards()));
    }
    bucketCountThresholds.ensureValidity();
    return new TermsAggregatorFactory(aggregationName, vsParser.config(), order, bucketCountThresholds, aggParser.getIncludeExclude(), aggParser.getExecutionHint(), aggParser.getCollectionMode(), aggParser.showTermDocCountError());
}
 
Example #4
Source File: TermsParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
static Terms.Order resolveOrder(String key, boolean asc) {
    if ("_term".equals(key)) {
        return Order.term(asc);
    }
    if ("_count".equals(key)) {
        return Order.count(asc);
    }
    return Order.aggregation(key, asc);
}
 
Example #5
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static boolean isCountDesc(Terms.Order order) {
    if (order == COUNT_DESC) {
        return true;
    }else if (order instanceof CompoundOrder) {
        // check if its a compound order with count desc and the tie breaker (term asc)
        CompoundOrder compoundOrder = (CompoundOrder) order;
        if (compoundOrder.orderElements.size() == 2 && compoundOrder.orderElements.get(0) == COUNT_DESC && compoundOrder.orderElements.get(1) == TERM_ASC) {
            return true;
        }
    }
    return false;
}
 
Example #6
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static Terms.Order validate(Terms.Order order, Aggregator termsAggregator) {
    if (order instanceof CompoundOrder) {
        for (Terms.Order innerOrder : ((CompoundOrder)order).orderElements) {
            validate(innerOrder, termsAggregator);
        }
        return order;
    } else if (!(order instanceof Aggregation)) {
        return order;
    }
    AggregationPath path = ((Aggregation) order).path();
    path.validate(termsAggregator);
    return order;
}
 
Example #7
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public CompoundOrder(List<Terms.Order> compoundOrder, boolean absoluteOrdering) {
    this.orderElements = new LinkedList<>(compoundOrder);
    Terms.Order lastElement = compoundOrder.get(compoundOrder.size() - 1);
    if (absoluteOrdering && !(InternalOrder.TERM_ASC == lastElement || InternalOrder.TERM_DESC == lastElement)) {
        // add term order ascending as a tie-breaker to avoid non-deterministic ordering
        // if all user provided comparators return 0.
        this.orderElements.add(Order.term(true));
    }
}
 
Example #8
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startArray();
    for (Terms.Order order : orderElements) {
        order.toXContent(builder, params);
    }
    return builder.endArray();
}
 
Example #9
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public int compare(Bucket o1, Bucket o2) {
    int result = 0;
    for (Iterator<Terms.Order> itr = compoundOrder.iterator(); itr.hasNext() && result == 0;) {
        result = itr.next().comparator(aggregator).compare(o1, o2);
    }
    return result;
}
 
Example #10
Source File: ElasticsearchSearchDao.java    From metron with Apache License 2.0 5 votes vote down vote up
private Order getElasticsearchGroupOrder(GroupOrder groupOrder) {
  if (groupOrder.getGroupOrderType() == GroupOrderType.TERM) {
    return groupOrder.getSortOrder() == SortOrder.ASC ? Order.term(true) : Order.term(false);
  } else {
    return groupOrder.getSortOrder() == SortOrder.ASC ? Order.count(true) : Order.count(false);
  }
}
 
Example #11
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public CompoundOrder(List<Terms.Order> compoundOrder) {
    this(compoundOrder, true);
}
 
Example #12
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
List<Terms.Order> orderElements() {
    return Collections.unmodifiableList(orderElements);
}
 
Example #13
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public CompoundOrderComparator(List<Terms.Order> compoundOrder, Aggregator aggregator) {
    this.compoundOrder = compoundOrder;
    this.aggregator = aggregator;
}
 
Example #14
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static Terms.Order readOrder(StreamInput in) throws IOException {
    return readOrder(in, true);
}