|
20 | 20 | package org.elasticsearch.index.mapper.object;
|
21 | 21 |
|
22 | 22 | import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
|
23 |
| -import org.apache.lucene.document.Field; |
24 | 23 | import org.apache.lucene.document.XStringField;
|
25 | 24 | import org.apache.lucene.index.IndexableField;
|
26 | 25 | import org.apache.lucene.index.Term;
|
@@ -180,63 +179,80 @@ protected ObjectMapper createMapper(String name, String fullPath, boolean enable
|
180 | 179 | public static class TypeParser implements Mapper.TypeParser {
|
181 | 180 | @Override
|
182 | 181 | public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
|
183 |
| - Map<String, Object> objectNode = node; |
184 | 182 | ObjectMapper.Builder builder = createBuilder(name);
|
185 |
| - |
186 |
| - boolean nested = false; |
187 |
| - boolean nestedIncludeInParent = false; |
188 |
| - boolean nestedIncludeInRoot = false; |
189 |
| - for (Map.Entry<String, Object> entry : objectNode.entrySet()) { |
| 183 | + for (Map.Entry<String, Object> entry : node.entrySet()) { |
190 | 184 | String fieldName = Strings.toUnderscoreCase(entry.getKey());
|
191 | 185 | Object fieldNode = entry.getValue();
|
| 186 | + parseObjectOrDocumentTypeProperties(fieldName, fieldNode, parserContext, builder); |
| 187 | + parseObjectProperties(name, fieldName, fieldNode, builder); |
| 188 | + } |
| 189 | + parseNested(name, node, builder); |
| 190 | + return builder; |
| 191 | + } |
192 | 192 |
|
193 |
| - if (fieldName.equals("dynamic")) { |
194 |
| - String value = fieldNode.toString(); |
195 |
| - if (value.equalsIgnoreCase("strict")) { |
196 |
| - builder.dynamic(Dynamic.STRICT); |
197 |
| - } else { |
198 |
| - builder.dynamic(nodeBooleanValue(fieldNode) ? Dynamic.TRUE : Dynamic.FALSE); |
199 |
| - } |
200 |
| - } else if (fieldName.equals("type")) { |
201 |
| - String type = fieldNode.toString(); |
202 |
| - if (type.equals(CONTENT_TYPE)) { |
203 |
| - builder.nested = Nested.NO; |
204 |
| - } else if (type.equals(NESTED_CONTENT_TYPE)) { |
205 |
| - nested = true; |
206 |
| - } else { |
207 |
| - throw new MapperParsingException("Trying to parse an object but has a different type [" + type + "] for [" + name + "]"); |
208 |
| - } |
209 |
| - } else if (fieldName.equals("include_in_parent")) { |
210 |
| - nestedIncludeInParent = nodeBooleanValue(fieldNode); |
211 |
| - } else if (fieldName.equals("include_in_root")) { |
212 |
| - nestedIncludeInRoot = nodeBooleanValue(fieldNode); |
213 |
| - } else if (fieldName.equals("enabled")) { |
214 |
| - builder.enabled(nodeBooleanValue(fieldNode)); |
215 |
| - } else if (fieldName.equals("path")) { |
216 |
| - builder.pathType(parsePathType(name, fieldNode.toString())); |
217 |
| - } else if (fieldName.equals("properties")) { |
218 |
| - if (fieldNode instanceof Collection && ((Collection) fieldNode).isEmpty()) { |
219 |
| - // nothing to do here, empty (to support "properties: []" case) |
220 |
| - } else if (!(fieldNode instanceof Map)) { |
221 |
| - throw new ElasticsearchParseException("properties must be a map type"); |
222 |
| - } else { |
223 |
| - parseProperties(builder, (Map<String, Object>) fieldNode, parserContext); |
224 |
| - } |
225 |
| - } else if (fieldName.equals("include_in_all")) { |
226 |
| - builder.includeInAll(nodeBooleanValue(fieldNode)); |
| 193 | + protected static boolean parseObjectOrDocumentTypeProperties(String fieldName, Object fieldNode, ParserContext parserContext, ObjectMapper.Builder builder) { |
| 194 | + if (fieldName.equals("dynamic")) { |
| 195 | + String value = fieldNode.toString(); |
| 196 | + if (value.equalsIgnoreCase("strict")) { |
| 197 | + builder.dynamic(Dynamic.STRICT); |
| 198 | + } else { |
| 199 | + builder.dynamic(nodeBooleanValue(fieldNode) ? Dynamic.TRUE : Dynamic.FALSE); |
| 200 | + } |
| 201 | + return true; |
| 202 | + } else if (fieldName.equals("enabled")) { |
| 203 | + builder.enabled(nodeBooleanValue(fieldNode)); |
| 204 | + return true; |
| 205 | + } else if (fieldName.equals("properties")) { |
| 206 | + if (fieldNode instanceof Collection && ((Collection) fieldNode).isEmpty()) { |
| 207 | + // nothing to do here, empty (to support "properties: []" case) |
| 208 | + } else if (!(fieldNode instanceof Map)) { |
| 209 | + throw new ElasticsearchParseException("properties must be a map type"); |
227 | 210 | } else {
|
228 |
| - processField(builder, fieldName, fieldNode); |
| 211 | + parseProperties(builder, (Map<String, Object>) fieldNode, parserContext); |
229 | 212 | }
|
| 213 | + return true; |
230 | 214 | }
|
| 215 | + return false; |
| 216 | + } |
231 | 217 |
|
| 218 | + protected static void parseObjectProperties(String name, String fieldName, Object fieldNode, ObjectMapper.Builder builder) { |
| 219 | + if (fieldName.equals("path")) { |
| 220 | + builder.pathType(parsePathType(name, fieldNode.toString())); |
| 221 | + } else if (fieldName.equals("include_in_all")) { |
| 222 | + builder.includeInAll(nodeBooleanValue(fieldNode)); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + protected static void parseNested(String name, Map<String, Object> node, ObjectMapper.Builder builder) { |
| 227 | + boolean nested = false; |
| 228 | + boolean nestedIncludeInParent = false; |
| 229 | + boolean nestedIncludeInRoot = false; |
| 230 | + Object fieldNode = node.get("type"); |
| 231 | + if (fieldNode!=null) { |
| 232 | + String type = fieldNode.toString(); |
| 233 | + if (type.equals(CONTENT_TYPE)) { |
| 234 | + builder.nested = Nested.NO; |
| 235 | + } else if (type.equals(NESTED_CONTENT_TYPE)) { |
| 236 | + nested = true; |
| 237 | + } else { |
| 238 | + throw new MapperParsingException("Trying to parse an object but has a different type [" + type + "] for [" + name + "]"); |
| 239 | + } |
| 240 | + } |
| 241 | + fieldNode = node.get("include_in_parent"); |
| 242 | + if (fieldNode != null) { |
| 243 | + nestedIncludeInParent = nodeBooleanValue(fieldNode); |
| 244 | + } |
| 245 | + fieldNode = node.get("include_in_root"); |
| 246 | + if (fieldNode != null) { |
| 247 | + nestedIncludeInRoot = nodeBooleanValue(fieldNode); |
| 248 | + } |
232 | 249 | if (nested) {
|
233 | 250 | builder.nested = Nested.newNested(nestedIncludeInParent, nestedIncludeInRoot);
|
234 | 251 | }
|
235 | 252 |
|
236 |
| - return builder; |
237 | 253 | }
|
238 | 254 |
|
239 |
| - private void parseProperties(ObjectMapper.Builder objBuilder, Map<String, Object> propsNode, ParserContext parserContext) { |
| 255 | + protected static void parseProperties(ObjectMapper.Builder objBuilder, Map<String, Object> propsNode, ParserContext parserContext) { |
240 | 256 | for (Map.Entry<String, Object> entry : propsNode.entrySet()) {
|
241 | 257 | String propName = entry.getKey();
|
242 | 258 | Map<String, Object> propNode = (Map<String, Object>) entry.getValue();
|
@@ -270,10 +286,6 @@ private void parseProperties(ObjectMapper.Builder objBuilder, Map<String, Object
|
270 | 286 | protected Builder createBuilder(String name) {
|
271 | 287 | return object(name);
|
272 | 288 | }
|
273 |
| - |
274 |
| - protected void processField(Builder builder, String fieldName, Object fieldNode) { |
275 |
| - |
276 |
| - } |
277 | 289 | }
|
278 | 290 |
|
279 | 291 | private final String name;
|
|
0 commit comments