|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.analytics.metricselector; |
| 7 | + |
| 8 | +import org.elasticsearch.common.ParseField; |
| 9 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 10 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 11 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 12 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 13 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 14 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 15 | +import org.elasticsearch.index.query.QueryShardContext; |
| 16 | +import org.elasticsearch.search.DocValueFormat; |
| 17 | +import org.elasticsearch.search.MultiValueMode; |
| 18 | +import org.elasticsearch.search.aggregations.AggregationBuilder; |
| 19 | +import org.elasticsearch.search.aggregations.AggregatorFactories; |
| 20 | +import org.elasticsearch.search.aggregations.AggregatorFactory; |
| 21 | +import org.elasticsearch.search.aggregations.support.MultiValuesSourceAggregationBuilder; |
| 22 | +import org.elasticsearch.search.aggregations.support.MultiValuesSourceAggregatorFactory; |
| 23 | +import org.elasticsearch.search.aggregations.support.MultiValuesSourceFieldConfig; |
| 24 | +import org.elasticsearch.search.aggregations.support.MultiValuesSourceParseHelper; |
| 25 | +import org.elasticsearch.search.aggregations.support.ValueType; |
| 26 | +import org.elasticsearch.search.aggregations.support.ValuesSource; |
| 27 | +import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +public class MetricSelectorAggregationBuilder extends MultiValuesSourceAggregationBuilder.LeafOnly<ValuesSource.Numeric, MetricSelectorAggregationBuilder> { |
| 34 | + public static final String NAME = "metric_selector"; |
| 35 | + public static final ParseField METRIC_FIELD = new ParseField("metric_field"); |
| 36 | + public static final ParseField SELECTOR_FIELD = new ParseField("selector_field"); |
| 37 | + public static final ParseField MULTIVALUE_MODE_FIELD = new ParseField("multi_value_mode"); |
| 38 | + |
| 39 | + private SelectionCriteria selectionCriteria = SelectionCriteria.MAX; |
| 40 | + private MultiValueMode multiValueMode = MultiValueMode.AVG; |
| 41 | + |
| 42 | + private static final ObjectParser<MetricSelectorAggregationBuilder, Void> PARSER; |
| 43 | + static { |
| 44 | + PARSER = new ObjectParser<>(MetricSelectorAggregationBuilder.NAME); |
| 45 | + MultiValuesSourceParseHelper.declareCommon(PARSER, true, ValueType.NUMERIC); |
| 46 | + MultiValuesSourceParseHelper.declareField(METRIC_FIELD.getPreferredName(), PARSER, true, false); |
| 47 | + MultiValuesSourceParseHelper.declareField(SELECTOR_FIELD.getPreferredName(), PARSER, true, false); |
| 48 | + PARSER.declareString(MetricSelectorAggregationBuilder::selectionCriteria, SelectionCriteria.SELECTION_CRITERIA); |
| 49 | + PARSER.declareString(MetricSelectorAggregationBuilder::multiValueMode, MULTIVALUE_MODE_FIELD); |
| 50 | + } |
| 51 | + |
| 52 | + public static AggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException { |
| 53 | + return PARSER.parse(parser, new MetricSelectorAggregationBuilder(aggregationName), null); |
| 54 | + } |
| 55 | + |
| 56 | + public MetricSelectorAggregationBuilder(String name) { |
| 57 | + super(name, ValueType.NUMERIC); |
| 58 | + } |
| 59 | + |
| 60 | + public MetricSelectorAggregationBuilder(MetricSelectorAggregationBuilder clone, AggregatorFactories.Builder factoriesBuilder, Map<String, Object> metaData) { |
| 61 | + super(clone, factoriesBuilder, metaData); |
| 62 | + } |
| 63 | + |
| 64 | + public MetricSelectorAggregationBuilder metricField(MultiValuesSourceFieldConfig metricConfig) { |
| 65 | + metricConfig = Objects.requireNonNull(metricConfig, "Configuration for field [" + METRIC_FIELD + "] cannot be null"); |
| 66 | + field(METRIC_FIELD.getPreferredName(), metricConfig); |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + public MetricSelectorAggregationBuilder selectorField(MultiValuesSourceFieldConfig selectorConfig) { |
| 71 | + selectorConfig = Objects.requireNonNull(selectorConfig, "Configuration for field [" + SELECTOR_FIELD + "] cannot be null"); |
| 72 | + field(SELECTOR_FIELD.getPreferredName(), selectorConfig); |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + public MetricSelectorAggregationBuilder selectionCriteria(String criteria) { |
| 77 | + this.selectionCriteria = SelectionCriteria.fromString(criteria); |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + public MetricSelectorAggregationBuilder multiValueMode(String mode) { |
| 82 | + this.multiValueMode = MultiValueMode.fromString(mode); |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Read from a stream. |
| 88 | + */ |
| 89 | + public MetricSelectorAggregationBuilder(StreamInput in) throws IOException { |
| 90 | + super(in, ValueType.NUMERIC); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected AggregationBuilder shallowCopy(AggregatorFactories.Builder factoriesBuilder, Map<String, Object> metaData) { |
| 95 | + return new MetricSelectorAggregationBuilder(this, factoriesBuilder, metaData); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + protected void innerWriteTo(StreamOutput out) throws IOException { |
| 100 | + multiValueMode.writeTo(out); |
| 101 | + selectionCriteria.writeTo(out); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + protected MultiValuesSourceAggregatorFactory<ValuesSource.Numeric> innerBuild(QueryShardContext queryShardContext, |
| 106 | + Map<String, ValuesSourceConfig<ValuesSource.Numeric>> configs, |
| 107 | + DocValueFormat format, |
| 108 | + AggregatorFactory parent, |
| 109 | + AggregatorFactories.Builder subFactoriesBuilder) throws IOException { |
| 110 | + return new MetricSelectorAggregatorFactory(name, configs, format, selectionCriteria, multiValueMode, |
| 111 | + queryShardContext, parent, subFactoriesBuilder, metaData); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public XContentBuilder doXContentBody(XContentBuilder builder, ToXContent.Params params) throws IOException { |
| 116 | + builder.field(MULTIVALUE_MODE_FIELD.getPreferredName(), multiValueMode); |
| 117 | + builder.field(SelectionCriteria.SELECTION_CRITERIA.getPreferredName(), selectionCriteria); |
| 118 | + return builder; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public String getType() { |
| 123 | + return NAME; |
| 124 | + } |
| 125 | +} |
0 commit comments