Example #1
0
  @Override
  protected void doXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
    if (dynamicDateTimeFormatters != Defaults.DYNAMIC_DATE_TIME_FORMATTERS) {
      if (dynamicDateTimeFormatters.length > 0) {
        builder.startArray("dynamic_date_formats");
        for (FormatDateTimeFormatter dateTimeFormatter : dynamicDateTimeFormatters) {
          builder.value(dateTimeFormatter.format());
        }
        builder.endArray();
      }
    }

    if (dynamicTemplates != null && dynamicTemplates.length > 0) {
      builder.startArray("dynamic_templates");
      for (DynamicTemplate dynamicTemplate : dynamicTemplates) {
        builder.startObject();
        builder.field(dynamicTemplate.name());
        builder.map(dynamicTemplate.conf());
        builder.endObject();
      }
      builder.endArray();
    }

    if (dateDetection != Defaults.DATE_DETECTION) {
      builder.field("date_detection", dateDetection);
    }
    if (numericDetection != Defaults.NUMERIC_DETECTION) {
      builder.field("numeric_detection", numericDetection);
    }
  }
Example #2
0
 public DynamicTemplate findTemplate(ContentPath path, String name, String matchType) {
   for (DynamicTemplate dynamicTemplate : dynamicTemplates) {
     if (dynamicTemplate.match(path, name, matchType)) {
       return dynamicTemplate;
     }
   }
   return null;
 }
Example #3
0
 public Mapper.Builder findTemplateBuilder(
     ParseContext context, String name, String dynamicType, String matchType) {
   DynamicTemplate dynamicTemplate = findTemplate(context.path(), name, matchType);
   if (dynamicTemplate == null) {
     return null;
   }
   Mapper.TypeParser.ParserContext parserContext = context.docMapperParser().parserContext();
   String mappingType = dynamicTemplate.mappingType(dynamicType);
   Mapper.TypeParser typeParser = parserContext.typeParser(mappingType);
   if (typeParser == null) {
     throw new MapperParsingException(
         "failed to find type parsed [" + mappingType + "] for [" + name + "]");
   }
   return typeParser.parse(name, dynamicTemplate.mappingForName(name, dynamicType), parserContext);
 }
Example #4
0
 @Override
 protected void doMerge(ObjectMapper mergeWith, MergeContext mergeContext) {
   RootObjectMapper mergeWithObject = (RootObjectMapper) mergeWith;
   if (!mergeContext.mergeFlags().simulate()) {
     // merge them
     List<DynamicTemplate> mergedTemplates =
         Lists.newArrayList(Arrays.asList(this.dynamicTemplates));
     for (DynamicTemplate template : mergeWithObject.dynamicTemplates) {
       boolean replaced = false;
       for (int i = 0; i < mergedTemplates.size(); i++) {
         if (mergedTemplates.get(i).name().equals(template.name())) {
           mergedTemplates.set(i, template);
           replaced = true;
         }
       }
       if (!replaced) {
         mergedTemplates.add(template);
       }
     }
     this.dynamicTemplates = mergedTemplates.toArray(new DynamicTemplate[mergedTemplates.size()]);
   }
 }
Example #5
0
 protected boolean processField(
     ObjectMapper.Builder builder, String fieldName, Object fieldNode) {
   if (fieldName.equals("date_formats") || fieldName.equals("dynamic_date_formats")) {
     List<FormatDateTimeFormatter> dateTimeFormatters = newArrayList();
     if (fieldNode instanceof List) {
       for (Object node1 : (List) fieldNode) {
         dateTimeFormatters.add(parseDateTimeFormatter(fieldName, node1));
       }
     } else if ("none".equals(fieldNode.toString())) {
       dateTimeFormatters = null;
     } else {
       dateTimeFormatters.add(parseDateTimeFormatter(fieldName, fieldNode));
     }
     if (dateTimeFormatters == null) {
       ((Builder) builder).noDynamicDateTimeFormatter();
     } else {
       ((Builder) builder).dynamicDateTimeFormatter(dateTimeFormatters);
     }
     return true;
   } else if (fieldName.equals("dynamic_templates")) {
     //  "dynamic_templates" : [
     //      {
     //          "template_1" : {
     //              "match" : "*_test",
     //              "match_mapping_type" : "string",
     //              "mapping" : { "type" : "string", "store" : "yes" }
     //          }
     //      }
     //  ]
     List tmplNodes = (List) fieldNode;
     for (Object tmplNode : tmplNodes) {
       Map<String, Object> tmpl = (Map<String, Object>) tmplNode;
       if (tmpl.size() != 1) {
         throw new MapperParsingException("A dynamic template must be defined with a name");
       }
       Map.Entry<String, Object> entry = tmpl.entrySet().iterator().next();
       ((Builder) builder)
           .add(DynamicTemplate.parse(entry.getKey(), (Map<String, Object>) entry.getValue()));
     }
     return true;
   } else if (fieldName.equals("date_detection")) {
     ((Builder) builder).dateDetection = nodeBooleanValue(fieldNode);
     return true;
   } else if (fieldName.equals("numeric_detection")) {
     ((Builder) builder).numericDetection = nodeBooleanValue(fieldNode);
     return true;
   }
   return false;
 }