-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement top_metrics agg (#51155) (#52366)
The `top_metrics` agg is kind of like `top_hits` but it only works on doc values so it *should* be faster. At this point it is fairly limited in that it only supports a single, numeric sort and a single, numeric metric. And it only fetches the "very topest" document worth of metric. We plan to support returning a configurable number of top metrics, requesting more than one metric and more than one sort. And, eventually, non-numeric sorts and metrics. The trick is doing those things fairly efficiently. Co-Authored by: Zachary Tong <zach@elastic.co>
- Loading branch information
Showing
60 changed files
with
4,228 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
...nt/rest-high-level/src/main/java/org/elasticsearch/client/analytics/ParsedTopMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.elasticsearch.client.analytics; | ||
|
||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.ObjectParser; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.common.xcontent.XContentParserUtils; | ||
import org.elasticsearch.search.aggregations.ParsedAggregation; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; | ||
|
||
/** | ||
* Results of the {@code top_metrics} aggregation. | ||
*/ | ||
public class ParsedTopMetrics extends ParsedAggregation { | ||
private static final ParseField TOP_FIELD = new ParseField("top"); | ||
|
||
private final List<TopMetrics> topMetrics; | ||
|
||
private ParsedTopMetrics(String name, List<TopMetrics> topMetrics) { | ||
setName(name); | ||
this.topMetrics = topMetrics; | ||
} | ||
|
||
/** | ||
* The list of top metrics, in sorted order. | ||
*/ | ||
public List<TopMetrics> getTopMetrics() { | ||
return topMetrics; | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return TopMetricsAggregationBuilder.NAME; | ||
} | ||
|
||
@Override | ||
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { | ||
builder.startArray(TOP_FIELD.getPreferredName()); | ||
for (TopMetrics top : topMetrics) { | ||
top.toXContent(builder, params); | ||
} | ||
return builder.endArray(); | ||
} | ||
|
||
public static final ConstructingObjectParser<ParsedTopMetrics, String> PARSER = new ConstructingObjectParser<>( | ||
TopMetricsAggregationBuilder.NAME, true, (args, name) -> { | ||
@SuppressWarnings("unchecked") | ||
List<TopMetrics> topMetrics = (List<TopMetrics>) args[0]; | ||
return new ParsedTopMetrics(name, topMetrics); | ||
}); | ||
static { | ||
PARSER.declareObjectArray(constructorArg(), (p, c) -> TopMetrics.PARSER.parse(p, null), TOP_FIELD); | ||
ParsedAggregation.declareAggregationFields(PARSER); | ||
} | ||
|
||
/** | ||
* The metrics belonging to the document with the "top" sort key. | ||
*/ | ||
public static class TopMetrics implements ToXContent { | ||
private static final ParseField SORT_FIELD = new ParseField("sort"); | ||
private static final ParseField METRICS_FIELD = new ParseField("metrics"); | ||
|
||
private final List<Object> sort; | ||
private final Map<String, Double> metrics; | ||
|
||
private TopMetrics(List<Object> sort, Map<String, Double> metrics) { | ||
this.sort = sort; | ||
this.metrics = metrics; | ||
} | ||
|
||
/** | ||
* The sort key for these top metrics. | ||
*/ | ||
public List<Object> getSort() { | ||
return sort; | ||
} | ||
|
||
/** | ||
* The top metric values returned by the aggregation. | ||
*/ | ||
public Map<String, Double> getMetrics() { | ||
return metrics; | ||
} | ||
|
||
private static final ConstructingObjectParser<TopMetrics, Void> PARSER = new ConstructingObjectParser<>("top", true, | ||
(args, name) -> { | ||
@SuppressWarnings("unchecked") | ||
List<Object> sort = (List<Object>) args[0]; | ||
@SuppressWarnings("unchecked") | ||
Map<String, Double> metrics = (Map<String, Double>) args[1]; | ||
return new TopMetrics(sort, metrics); | ||
}); | ||
static { | ||
PARSER.declareFieldArray(constructorArg(), (p, c) -> XContentParserUtils.parseFieldsValue(p), | ||
SORT_FIELD, ObjectParser.ValueType.VALUE_ARRAY); | ||
PARSER.declareObject(constructorArg(), (p, c) -> p.map(HashMap::new, XContentParser::doubleValue), METRICS_FIELD); | ||
} | ||
|
||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(SORT_FIELD.getPreferredName(), sort); | ||
builder.field(METRICS_FIELD.getPreferredName(), metrics); | ||
builder.endObject(); | ||
return builder; | ||
}; | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...-level/src/main/java/org/elasticsearch/client/analytics/TopMetricsAggregationBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.elasticsearch.client.analytics; | ||
|
||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.index.query.QueryRewriteContext; | ||
import org.elasticsearch.index.query.QueryShardContext; | ||
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; | ||
import org.elasticsearch.search.aggregations.AggregationBuilder; | ||
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder; | ||
import org.elasticsearch.search.aggregations.AggregatorFactory; | ||
import org.elasticsearch.search.builder.SearchSourceBuilder; | ||
import org.elasticsearch.search.sort.SortBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** | ||
* Builds the Top Metrics aggregation request. | ||
* <p> | ||
* NOTE: This extends {@linkplain AbstractAggregationBuilder} for compatibility | ||
* with {@link SearchSourceBuilder#aggregation(AggregationBuilder)} but it | ||
* doesn't support any "server" side things like | ||
* {@linkplain Writeable#writeTo(StreamOutput)}, | ||
* {@linkplain AggregationBuilder#rewrite(QueryRewriteContext)}, or | ||
* {@linkplain AbstractAggregationBuilder#build(QueryShardContext, AggregatorFactory)}. | ||
*/ | ||
public class TopMetricsAggregationBuilder extends AbstractAggregationBuilder<TopMetricsAggregationBuilder> { | ||
public static final String NAME = "top_metrics"; | ||
|
||
private final SortBuilder<?> sort; | ||
private final String metric; | ||
|
||
/** | ||
* Build the request. | ||
* @param name the name of the metric | ||
* @param sort the sort key used to select the top metrics | ||
* @param metric the name of the field to select | ||
*/ | ||
public TopMetricsAggregationBuilder(String name, SortBuilder<?> sort, String metric) { | ||
super(name); | ||
this.sort = sort; | ||
this.metric = metric; | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
{ | ||
builder.startArray("sort"); | ||
sort.toXContent(builder, params); | ||
builder.endArray(); | ||
builder.startObject("metric").field("field", metric).endObject(); | ||
} | ||
return builder.endObject(); | ||
} | ||
|
||
@Override | ||
protected void doWriteTo(StreamOutput out) throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
protected AggregatorFactory doBuild(QueryShardContext queryShardContext, AggregatorFactory parent, Builder subfactoriesBuilder) | ||
throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
protected AggregationBuilder shallowCopy(Builder factoriesBuilder, Map<String, Object> metaData) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.