All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.elasticsearch.search.aggregations.bucket.adjacency.InternalAdjacencyMatrix Maven / Gradle / Ivy
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.search.aggregations.bucket.adjacency;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.search.aggregations.AggregationReduceContext;
import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.search.aggregations.InternalAggregations;
import org.elasticsearch.search.aggregations.InternalMultiBucketAggregation;
import org.elasticsearch.search.aggregations.support.SamplingContext;
import org.elasticsearch.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class InternalAdjacencyMatrix extends InternalMultiBucketAggregation
implements
AdjacencyMatrix {
public static class InternalBucket extends InternalMultiBucketAggregation.InternalBucket implements AdjacencyMatrix.Bucket {
private final String key;
private long docCount;
InternalAggregations aggregations;
public InternalBucket(String key, long docCount, InternalAggregations aggregations) {
this.key = key;
this.docCount = docCount;
this.aggregations = aggregations;
}
/**
* Read from a stream.
*/
public InternalBucket(StreamInput in) throws IOException {
key = in.readOptionalString();
docCount = in.readVLong();
aggregations = InternalAggregations.readFrom(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(key);
out.writeVLong(docCount);
aggregations.writeTo(out);
}
@Override
public String getKey() {
return key;
}
@Override
public String getKeyAsString() {
return key;
}
@Override
public long getDocCount() {
return docCount;
}
@Override
public InternalAggregations getAggregations() {
return aggregations;
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(CommonFields.KEY.getPreferredName(), key);
builder.field(CommonFields.DOC_COUNT.getPreferredName(), docCount);
aggregations.toXContentInternal(builder, params);
builder.endObject();
return builder;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
InternalBucket that = (InternalBucket) other;
return Objects.equals(key, that.key)
&& Objects.equals(docCount, that.docCount)
&& Objects.equals(aggregations, that.aggregations);
}
@Override
public int hashCode() {
return Objects.hash(getClass(), key, docCount, aggregations);
}
InternalBucket finalizeSampling(SamplingContext samplingContext) {
return new InternalBucket(
key,
samplingContext.scaleUp(docCount),
InternalAggregations.finalizeSampling(aggregations, samplingContext)
);
}
}
private final List buckets;
private Map bucketMap;
public InternalAdjacencyMatrix(String name, List buckets, Map metadata) {
super(name, metadata);
this.buckets = buckets;
}
/**
* Read from a stream.
*/
public InternalAdjacencyMatrix(StreamInput in) throws IOException {
super(in);
int size = in.readVInt();
List buckets = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
buckets.add(new InternalBucket(in));
}
this.buckets = buckets;
this.bucketMap = null;
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeCollection(buckets);
}
@Override
public String getWriteableName() {
return AdjacencyMatrixAggregationBuilder.NAME;
}
@Override
public InternalAdjacencyMatrix create(List buckets) {
return new InternalAdjacencyMatrix(this.name, buckets, this.metadata);
}
@Override
public InternalBucket createBucket(InternalAggregations aggregations, InternalBucket prototype) {
return new InternalBucket(prototype.key, prototype.docCount, aggregations);
}
@Override
public List getBuckets() {
return buckets;
}
@Override
public InternalBucket getBucketByKey(String key) {
if (bucketMap == null) {
bucketMap = Maps.newMapWithExpectedSize(buckets.size());
for (InternalBucket bucket : buckets) {
bucketMap.put(bucket.getKey(), bucket);
}
}
return bucketMap.get(key);
}
@Override
public InternalAggregation reduce(List aggregations, AggregationReduceContext reduceContext) {
Map> bucketsMap = new HashMap<>();
for (InternalAggregation aggregation : aggregations) {
InternalAdjacencyMatrix filters = (InternalAdjacencyMatrix) aggregation;
for (InternalBucket bucket : filters.buckets) {
List sameRangeList = bucketsMap.get(bucket.key);
if (sameRangeList == null) {
sameRangeList = new ArrayList<>(aggregations.size());
bucketsMap.put(bucket.key, sameRangeList);
}
sameRangeList.add(bucket);
}
}
ArrayList reducedBuckets = new ArrayList<>(bucketsMap.size());
for (List sameRangeList : bucketsMap.values()) {
InternalBucket reducedBucket = reduceBucket(sameRangeList, reduceContext);
if (reducedBucket.docCount >= 1) {
reducedBuckets.add(reducedBucket);
}
}
reduceContext.consumeBucketsAndMaybeBreak(reducedBuckets.size());
Collections.sort(reducedBuckets, Comparator.comparing(InternalBucket::getKey));
InternalAdjacencyMatrix reduced = new InternalAdjacencyMatrix(name, reducedBuckets, getMetadata());
return reduced;
}
@Override
public InternalAggregation finalizeSampling(SamplingContext samplingContext) {
return new InternalAdjacencyMatrix(name, buckets.stream().map(b -> b.finalizeSampling(samplingContext)).toList(), getMetadata());
}
@Override
protected InternalBucket reduceBucket(List buckets, AggregationReduceContext context) {
assert buckets.size() > 0;
InternalBucket reduced = null;
List aggregationsList = new ArrayList<>(buckets.size());
for (InternalBucket bucket : buckets) {
if (reduced == null) {
reduced = new InternalBucket(bucket.key, bucket.docCount, bucket.aggregations);
} else {
reduced.docCount += bucket.docCount;
}
aggregationsList.add(bucket.aggregations);
}
reduced.aggregations = InternalAggregations.reduce(aggregationsList, context);
return reduced;
}
@Override
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
builder.startArray(CommonFields.BUCKETS.getPreferredName());
for (InternalBucket bucket : buckets) {
bucket.toXContent(builder, params);
}
builder.endArray();
return builder;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), buckets);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
if (super.equals(obj) == false) return false;
InternalAdjacencyMatrix that = (InternalAdjacencyMatrix) obj;
return Objects.equals(buckets, that.buckets);
}
}