|
| 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 | + |
| 20 | +package org.elasticsearch.search.aggregations.support; |
| 21 | + |
| 22 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 23 | +import org.elasticsearch.script.Script; |
| 24 | +import org.elasticsearch.script.Script.ScriptField; |
| 25 | +import org.elasticsearch.script.ScriptParameterParser; |
| 26 | +import org.elasticsearch.script.ScriptParameterParser.ScriptParameterValue; |
| 27 | +import org.elasticsearch.search.SearchParseException; |
| 28 | +import org.elasticsearch.search.aggregations.Aggregator; |
| 29 | +import org.elasticsearch.search.aggregations.AggregatorFactory; |
| 30 | +import org.elasticsearch.search.internal.SearchContext; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +import static com.google.common.collect.Maps.newHashMap; |
| 36 | + |
| 37 | +/** |
| 38 | + * |
| 39 | + */ |
| 40 | +public abstract class AbstractValuesSourceParser<VS extends ValuesSource> implements Aggregator.Parser { |
| 41 | + |
| 42 | + public abstract static class AnyValuesSourceParser extends AbstractValuesSourceParser<ValuesSource> { |
| 43 | + |
| 44 | + protected AnyValuesSourceParser(boolean scriptable, boolean formattable) { |
| 45 | + super(scriptable, formattable, ValuesSource.class, null); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public abstract static class NumericValuesSourceParser extends AbstractValuesSourceParser<ValuesSource.Numeric> { |
| 50 | + |
| 51 | + protected NumericValuesSourceParser(boolean scriptable, boolean formattable) { |
| 52 | + super(scriptable, formattable, ValuesSource.Numeric.class, ValueType.NUMERIC); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public abstract static class BytesValuesSourceParser extends AbstractValuesSourceParser<ValuesSource.Bytes> { |
| 57 | + |
| 58 | + protected BytesValuesSourceParser(boolean scriptable, boolean formattable) { |
| 59 | + super(scriptable, formattable, ValuesSource.Bytes.class, ValueType.STRING); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public abstract static class GeoPointValuesSourceParser extends AbstractValuesSourceParser<ValuesSource.GeoPoint> { |
| 64 | + |
| 65 | + protected GeoPointValuesSourceParser(boolean scriptable, boolean formattable) { |
| 66 | + super(scriptable, formattable, ValuesSource.GeoPoint.class, ValueType.GEOPOINT); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private boolean scriptable = true; |
| 71 | + private boolean formattable = false; |
| 72 | + private Class<VS> valuesSourceType = null; |
| 73 | + private ValueType targetValueType = null; |
| 74 | + private ScriptParameterParser scriptParameterParser = new ScriptParameterParser(); |
| 75 | + |
| 76 | + private AbstractValuesSourceParser(boolean scriptable, |
| 77 | + boolean formattable, Class<VS> valuesSourceType, ValueType targetValueType) { |
| 78 | + this.valuesSourceType = valuesSourceType; |
| 79 | + this.targetValueType = targetValueType; |
| 80 | + this.scriptable = scriptable; |
| 81 | + this.formattable = formattable; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException { |
| 86 | + |
| 87 | + String field = null; |
| 88 | + Script script = null; |
| 89 | + @Deprecated |
| 90 | + Map<String, Object> params = null; // TODO Remove in 3.0 |
| 91 | + ValueType valueType = null; |
| 92 | + String format = null; |
| 93 | + Object missing = null; |
| 94 | + |
| 95 | + XContentParser.Token token; |
| 96 | + String currentFieldName = null; |
| 97 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 98 | + if (token == XContentParser.Token.FIELD_NAME) { |
| 99 | + currentFieldName = parser.currentName(); |
| 100 | + } else if ("missing".equals(currentFieldName) && token.isValue()) { |
| 101 | + missing = parser.objectText(); |
| 102 | + } else if (token == XContentParser.Token.VALUE_STRING) { |
| 103 | + if ("field".equals(currentFieldName)) { |
| 104 | + field = parser.text(); |
| 105 | + } else if (formattable && "format".equals(currentFieldName)) { |
| 106 | + format = parser.text(); |
| 107 | + } else if (scriptable) { |
| 108 | + if ("value_type".equals(currentFieldName) || "valueType".equals(currentFieldName)) { |
| 109 | + valueType = ValueType.resolveForScript(parser.text()); |
| 110 | + if (targetValueType != null && valueType.isNotA(targetValueType)) { |
| 111 | + throw new SearchParseException(context, type() + " aggregation [" + aggregationName |
| 112 | + + "] was configured with an incompatible value type [" + valueType + "]. [" + type() |
| 113 | + + "] aggregation can only work on value of type [" + targetValueType + "]", |
| 114 | + parser.getTokenLocation()); |
| 115 | + } |
| 116 | + } else if (!scriptParameterParser.token(currentFieldName, token, parser, context.parseFieldMatcher())) { |
| 117 | + throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].", |
| 118 | + parser.getTokenLocation()); |
| 119 | + } |
| 120 | + } else { |
| 121 | + throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].", |
| 122 | + parser.getTokenLocation()); |
| 123 | + } |
| 124 | + } else if (scriptable && token == XContentParser.Token.START_OBJECT) { |
| 125 | + if (context.parseFieldMatcher().match(currentFieldName, ScriptField.SCRIPT)) { |
| 126 | + script = Script.parse(parser, context.parseFieldMatcher()); |
| 127 | + } else if ("params".equals(currentFieldName)) { |
| 128 | + params = parser.map(); |
| 129 | + } else { |
| 130 | + throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].", |
| 131 | + parser.getTokenLocation()); |
| 132 | + } |
| 133 | + } else if (!token(currentFieldName, token, parser)) { |
| 134 | + throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].", |
| 135 | + parser.getTokenLocation()); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if (script == null) { // Didn't find anything using the new API so |
| 140 | + // try using the old one instead |
| 141 | + ScriptParameterValue scriptValue = scriptParameterParser.getDefaultScriptParameterValue(); |
| 142 | + if (scriptValue != null) { |
| 143 | + if (params == null) { |
| 144 | + params = newHashMap(); |
| 145 | + } |
| 146 | + script = new Script(scriptValue.script(), scriptValue.scriptType(), scriptParameterParser.lang(), params); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + ValuesSourceAggregatorFactory<VS> factory = createFactory(aggregationName, this.valuesSourceType, this.targetValueType); |
| 151 | + factory.field(field); |
| 152 | + factory.script(script); |
| 153 | + factory.valueType(valueType); |
| 154 | + factory.format(format); |
| 155 | + factory.missing(missing); |
| 156 | + return factory; |
| 157 | + } |
| 158 | + |
| 159 | + protected abstract ValuesSourceAggregatorFactory<VS> createFactory(String aggregationName, Class<VS> valuesSourceType, |
| 160 | + ValueType targetValueType); |
| 161 | + |
| 162 | + protected abstract boolean token(String currentFieldName, XContentParser.Token token, XContentParser parser) throws IOException; |
| 163 | +} |
0 commit comments