|
20 | 20 | package org.elasticsearch.indices; |
21 | 21 |
|
22 | 22 | import org.elasticsearch.Version; |
23 | | -import org.elasticsearch.common.ParsingException; |
| 23 | +import org.elasticsearch.common.ParseField; |
24 | 24 | import org.elasticsearch.common.io.stream.StreamInput; |
25 | 25 | import org.elasticsearch.common.io.stream.StreamOutput; |
26 | 26 | import org.elasticsearch.common.io.stream.Writeable; |
| 27 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
27 | 28 | import org.elasticsearch.common.xcontent.ToXContentFragment; |
28 | 29 | import org.elasticsearch.common.xcontent.XContentBuilder; |
29 | 30 | import org.elasticsearch.common.xcontent.XContentParser; |
|
33 | 34 | import java.io.IOException; |
34 | 35 | import java.util.Objects; |
35 | 36 |
|
| 37 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; |
| 38 | + |
36 | 39 | /** |
37 | 40 | * Encapsulates the parameters needed to fetch terms. |
38 | 41 | */ |
39 | 42 | public class TermsLookup implements Writeable, ToXContentFragment { |
| 43 | + |
40 | 44 | private final String index; |
41 | 45 | private final String id; |
42 | 46 | private final String path; |
@@ -102,40 +106,22 @@ public TermsLookup routing(String routing) { |
102 | 106 | return this; |
103 | 107 | } |
104 | 108 |
|
| 109 | + private static final ConstructingObjectParser<TermsLookup, Void> PARSER = new ConstructingObjectParser<>("terms_lookup", |
| 110 | + args -> { |
| 111 | + String index = (String) args[0]; |
| 112 | + String id = (String) args[1]; |
| 113 | + String path = (String) args[2]; |
| 114 | + return new TermsLookup(index, id, path); |
| 115 | + }); |
| 116 | + static { |
| 117 | + PARSER.declareString(constructorArg(), new ParseField("index")); |
| 118 | + PARSER.declareString(constructorArg(), new ParseField("id")); |
| 119 | + PARSER.declareString(constructorArg(), new ParseField("path")); |
| 120 | + PARSER.declareString(TermsLookup::routing, new ParseField("routing")); |
| 121 | + } |
| 122 | + |
105 | 123 | public static TermsLookup parseTermsLookup(XContentParser parser) throws IOException { |
106 | | - String index = null; |
107 | | - String id = null; |
108 | | - String path = null; |
109 | | - String routing = null; |
110 | | - XContentParser.Token token; |
111 | | - String currentFieldName = ""; |
112 | | - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
113 | | - if (token == XContentParser.Token.FIELD_NAME) { |
114 | | - currentFieldName = parser.currentName(); |
115 | | - } else if (token.isValue()) { |
116 | | - switch (currentFieldName) { |
117 | | - case "index": |
118 | | - index = parser.text(); |
119 | | - break; |
120 | | - case "id": |
121 | | - id = parser.text(); |
122 | | - break; |
123 | | - case "routing": |
124 | | - routing = parser.textOrNull(); |
125 | | - break; |
126 | | - case "path": |
127 | | - path = parser.text(); |
128 | | - break; |
129 | | - default: |
130 | | - throw new ParsingException(parser.getTokenLocation(), "[" + TermsQueryBuilder.NAME + |
131 | | - "] query does not support [" + currentFieldName + "] within lookup element"); |
132 | | - } |
133 | | - } else { |
134 | | - throw new ParsingException(parser.getTokenLocation(), "[" + TermsQueryBuilder.NAME + "] unknown token [" |
135 | | - + token + "] after [" + currentFieldName + "]"); |
136 | | - } |
137 | | - } |
138 | | - return new TermsLookup(index, id, path).routing(routing); |
| 124 | + return PARSER.parse(parser, null); |
139 | 125 | } |
140 | 126 |
|
141 | 127 | @Override |
|
0 commit comments