|
| 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.pipeline.having; |
| 21 | + |
| 22 | +import org.elasticsearch.common.ParseField; |
| 23 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 24 | +import org.elasticsearch.script.Script; |
| 25 | +import org.elasticsearch.script.Script.ScriptField; |
| 26 | +import org.elasticsearch.search.SearchParseException; |
| 27 | +import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; |
| 28 | +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; |
| 29 | +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorFactory; |
| 30 | +import org.elasticsearch.search.internal.SearchContext; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | + |
| 38 | +public class BucketSelectorParser implements PipelineAggregator.Parser { |
| 39 | + |
| 40 | + public static final ParseField FORMAT = new ParseField("format"); |
| 41 | + public static final ParseField GAP_POLICY = new ParseField("gap_policy"); |
| 42 | + public static final ParseField PARAMS_FIELD = new ParseField("params"); |
| 43 | + |
| 44 | + @Override |
| 45 | + public String type() { |
| 46 | + return BucketSelectorPipelineAggregator.TYPE.name(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public PipelineAggregatorFactory parse(String reducerName, XContentParser parser, SearchContext context) throws IOException { |
| 51 | + XContentParser.Token token; |
| 52 | + Script script = null; |
| 53 | + String currentFieldName = null; |
| 54 | + Map<String, String> bucketsPathsMap = null; |
| 55 | + GapPolicy gapPolicy = GapPolicy.SKIP; |
| 56 | + |
| 57 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 58 | + if (token == XContentParser.Token.FIELD_NAME) { |
| 59 | + currentFieldName = parser.currentName(); |
| 60 | + } else if (token == XContentParser.Token.VALUE_STRING) { |
| 61 | + if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) { |
| 62 | + bucketsPathsMap = new HashMap<>(); |
| 63 | + bucketsPathsMap.put("_value", parser.text()); |
| 64 | + } else if (context.parseFieldMatcher().match(currentFieldName, GAP_POLICY)) { |
| 65 | + gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation()); |
| 66 | + } else if (context.parseFieldMatcher().match(currentFieldName, ScriptField.SCRIPT)) { |
| 67 | + script = Script.parse(parser, context.parseFieldMatcher()); |
| 68 | + } else { |
| 69 | + throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: [" |
| 70 | + + currentFieldName + "].", parser.getTokenLocation()); |
| 71 | + } |
| 72 | + } else if (token == XContentParser.Token.START_ARRAY) { |
| 73 | + if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) { |
| 74 | + List<String> paths = new ArrayList<>(); |
| 75 | + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { |
| 76 | + String path = parser.text(); |
| 77 | + paths.add(path); |
| 78 | + } |
| 79 | + bucketsPathsMap = new HashMap<>(); |
| 80 | + for (int i = 0; i < paths.size(); i++) { |
| 81 | + bucketsPathsMap.put("_value" + i, paths.get(i)); |
| 82 | + } |
| 83 | + } else { |
| 84 | + throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: [" |
| 85 | + + currentFieldName + "].", parser.getTokenLocation()); |
| 86 | + } |
| 87 | + } else if (token == XContentParser.Token.START_OBJECT) { |
| 88 | + if (context.parseFieldMatcher().match(currentFieldName, ScriptField.SCRIPT)) { |
| 89 | + script = Script.parse(parser, context.parseFieldMatcher()); |
| 90 | + } else if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) { |
| 91 | + Map<String, Object> map = parser.map(); |
| 92 | + bucketsPathsMap = new HashMap<>(); |
| 93 | + for (Map.Entry<String, Object> entry : map.entrySet()) { |
| 94 | + bucketsPathsMap.put(entry.getKey(), String.valueOf(entry.getValue())); |
| 95 | + } |
| 96 | + } else { |
| 97 | + throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: [" |
| 98 | + + currentFieldName + "].", parser.getTokenLocation()); |
| 99 | + } |
| 100 | + } else { |
| 101 | + throw new SearchParseException(context, "Unexpected token " + token + " in [" + reducerName + "].", |
| 102 | + parser.getTokenLocation()); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (bucketsPathsMap == null) { |
| 107 | + throw new SearchParseException(context, "Missing required field [" + BUCKETS_PATH.getPreferredName() |
| 108 | + + "] for bucket_selector aggregation [" + reducerName + "]", parser.getTokenLocation()); |
| 109 | + } |
| 110 | + |
| 111 | + if (script == null) { |
| 112 | + throw new SearchParseException(context, "Missing required field [" + ScriptField.SCRIPT.getPreferredName() |
| 113 | + + "] for bucket_selector aggregation [" + reducerName + "]", parser.getTokenLocation()); |
| 114 | + } |
| 115 | + |
| 116 | + return new BucketSelectorPipelineAggregator.Factory(reducerName, bucketsPathsMap, script, gapPolicy); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments