コード例 #1
0
  /**
   * Given a type (eg. long, string, ...), return an anonymous field mapper that can be used for
   * search operations.
   */
  public MappedFieldType unmappedFieldType(String type) {
    if (type.equals("string")) {
      deprecationLogger.deprecated(
          "[unmapped_type:string] should be replaced with [unmapped_type:keyword]");
      type = "keyword";
    }
    MappedFieldType fieldType = unmappedFieldTypes.get(type);
    if (fieldType == null) {
      final Mapper.TypeParser.ParserContext parserContext =
          documentMapperParser().parserContext(type);
      Mapper.TypeParser typeParser = parserContext.typeParser(type);
      if (typeParser == null) {
        throw new IllegalArgumentException("No mapper found for type [" + type + "]");
      }
      final Mapper.Builder<?, ?> builder =
          typeParser.parse("__anonymous_" + type, emptyMap(), parserContext);
      final BuilderContext builderContext =
          new BuilderContext(indexSettings.getSettings(), new ContentPath(1));
      fieldType = ((FieldMapper) builder.build(builderContext)).fieldType();

      // There is no need to synchronize writes here. In the case of concurrent access, we could
      // just
      // compute some mappers several times, which is not a big deal
      Map<String, MappedFieldType> newUnmappedFieldTypes = new HashMap<>();
      newUnmappedFieldTypes.putAll(unmappedFieldTypes);
      newUnmappedFieldTypes.put(type, fieldType);
      unmappedFieldTypes = unmodifiableMap(newUnmappedFieldTypes);
    }
    return fieldType;
  }
コード例 #2
0
    @Override
    public ExternalMapper build(BuilderContext context) {
      context.path().add(name);
      BinaryFieldMapper binMapper = binBuilder.build(context);
      BooleanFieldMapper boolMapper = boolBuilder.build(context);
      BaseGeoPointFieldMapper pointMapper = latLonPointBuilder.build(context);
      GeoShapeFieldMapper shapeMapper = shapeBuilder.build(context);
      FieldMapper stringMapper = (FieldMapper) stringBuilder.build(context);
      context.path().remove();

      setupFieldType(context);

      return new ExternalMapper(
          name,
          fieldType,
          generatedValue,
          mapperName,
          binMapper,
          boolMapper,
          pointMapper,
          shapeMapper,
          stringMapper,
          context.indexSettings(),
          multiFieldsBuilder.build(this, context),
          copyTo);
    }
コード例 #3
0
    @Override
    public Y build(BuilderContext context) {
      ContentPath.Type origPathType = context.path().pathType();
      context.path().pathType(pathType);
      context.path().add(name);

      Map<String, Mapper> mappers = new HashMap<>();
      for (Mapper.Builder builder : mappersBuilders) {
        Mapper mapper = builder.build(context);
        mappers.put(mapper.simpleName(), mapper);
      }
      context.path().pathType(origPathType);
      context.path().remove();

      ObjectMapper objectMapper =
          createMapper(
              name,
              context.path().fullPathAsText(name),
              enabled,
              nested,
              dynamic,
              pathType,
              mappers,
              context.indexSettings());
      objectMapper.includeInAllIfNotSet(includeInAll);

      return (Y) objectMapper;
    }
コード例 #4
0
  /**
   * Given a type (eg. long, string, ...), return an anonymous field mapper that can be used for
   * search operations.
   */
  public MappedFieldType unmappedFieldType(String type) {
    final ImmutableMap<String, MappedFieldType> unmappedFieldMappers = this.unmappedFieldTypes;
    MappedFieldType fieldType = unmappedFieldMappers.get(type);
    if (fieldType == null) {
      final Mapper.TypeParser.ParserContext parserContext = documentMapperParser().parserContext();
      Mapper.TypeParser typeParser = parserContext.typeParser(type);
      if (typeParser == null) {
        throw new IllegalArgumentException("No mapper found for type [" + type + "]");
      }
      final Mapper.Builder<?, ?> builder =
          typeParser.parse("__anonymous_" + type, ImmutableMap.<String, Object>of(), parserContext);
      final BuilderContext builderContext = new BuilderContext(indexSettings, new ContentPath(1));
      fieldType = ((FieldMapper) builder.build(builderContext)).fieldType();

      // There is no need to synchronize writes here. In the case of concurrent access, we could
      // just
      // compute some mappers several times, which is not a big deal
      this.unmappedFieldTypes =
          ImmutableMap.<String, MappedFieldType>builder()
              .putAll(unmappedFieldMappers)
              .put(type, fieldType)
              .build();
    }
    return fieldType;
  }
コード例 #5
0
  private void serializeObject(final ParseContext context, String currentFieldName)
      throws IOException {
    if (currentFieldName == null) {
      throw new MapperParsingException(
          "object mapping ["
              + name
              + "] trying to serialize an object with no field associated with it, current value ["
              + context.parser().textOrNull()
              + "]");
    }
    context.path().add(currentFieldName);

    Mapper objectMapper = mappers.get(currentFieldName);
    if (objectMapper != null) {
      objectMapper.parse(context);
    } else {
      Dynamic dynamic = this.dynamic;
      if (dynamic == null) {
        dynamic = context.root().dynamic();
      }
      if (dynamic == Dynamic.STRICT) {
        throw new StrictDynamicMappingException(fullPath, currentFieldName);
      } else if (dynamic == Dynamic.TRUE) {
        // we sync here just so we won't add it twice. Its not the end of the world
        // to sync here since next operations will get it before
        synchronized (mutex) {
          objectMapper = mappers.get(currentFieldName);
          if (objectMapper == null) {
            // remove the current field name from path, since template search and the object builder
            // add it as well...
            context.path().remove();
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "object");
            if (builder == null) {
              builder = MapperBuilders.object(currentFieldName).enabled(true).pathType(pathType);
              // if this is a non root object, then explicitly set the dynamic behavior if set
              if (!(this instanceof RootObjectMapper) && this.dynamic != Defaults.DYNAMIC) {
                ((Builder) builder).dynamic(this.dynamic);
              }
            }
            BuilderContext builderContext =
                new BuilderContext(context.indexSettings(), context.path());
            objectMapper = builder.build(builderContext);
            putDynamicMapper(context, currentFieldName, objectMapper);
          } else {
            objectMapper.parse(context);
          }
        }
      } else {
        // not dynamic, read everything up to end object
        context.parser().skipChildren();
      }
    }

    context.path().remove();
  }
コード例 #6
0
  private static ObjectMapper parseObject(
      final ParseContext context, ObjectMapper mapper, String currentFieldName) throws IOException {
    if (currentFieldName == null) {
      throw new MapperParsingException(
          "object mapping ["
              + mapper.name()
              + "] trying to serialize an object with no field associated with it, current value ["
              + context.parser().textOrNull()
              + "]");
    }
    context.path().add(currentFieldName);

    ObjectMapper update = null;
    Mapper objectMapper = mapper.getMapper(currentFieldName);
    if (objectMapper != null) {
      final Mapper subUpdate = parseObjectOrField(context, objectMapper);
      if (subUpdate != null) {
        // propagate mapping update
        update = mapper.mappingUpdate(subUpdate);
      }
    } else {
      ObjectMapper.Dynamic dynamic = mapper.dynamic();
      if (dynamic == null) {
        dynamic = dynamicOrDefault(context.root().dynamic());
      }
      if (dynamic == ObjectMapper.Dynamic.STRICT) {
        throw new StrictDynamicMappingException(mapper.fullPath(), currentFieldName);
      } else if (dynamic == ObjectMapper.Dynamic.TRUE) {
        // remove the current field name from path, since template search and the object builder add
        // it as well...
        context.path().remove();
        Mapper.Builder builder =
            context.root().findTemplateBuilder(context, currentFieldName, "object");
        if (builder == null) {
          builder = MapperBuilders.object(currentFieldName).enabled(true);
          // if this is a non root object, then explicitly set the dynamic behavior if set
          if (!(mapper instanceof RootObjectMapper)
              && mapper.dynamic() != ObjectMapper.Defaults.DYNAMIC) {
            ((ObjectMapper.Builder) builder).dynamic(mapper.dynamic());
          }
        }
        Mapper.BuilderContext builderContext =
            new Mapper.BuilderContext(context.indexSettings(), context.path());
        objectMapper = builder.build(builderContext);
        context.path().add(currentFieldName);
        update = mapper.mappingUpdate(parseAndMergeUpdate(objectMapper, context));
      } else {
        // not dynamic, read everything up to end object
        context.parser().skipChildren();
      }
    }

    context.path().remove();
    return update;
  }
コード例 #7
0
    private static ObjectMapper parseDynamicValue(final ParseContext context, ObjectMapper parentMapper, String currentFieldName, XContentParser.Token token) throws IOException {
        ObjectMapper.Dynamic dynamic = parentMapper.dynamic();
        if (dynamic == null) {
            dynamic = dynamicOrDefault(context.root().dynamic());
        }
        if (dynamic == ObjectMapper.Dynamic.STRICT) {
            throw new StrictDynamicMappingException(parentMapper.fullPath(), currentFieldName);
        }
        if (dynamic == ObjectMapper.Dynamic.FALSE) {
            return null;
        }
        final Mapper.BuilderContext builderContext = new Mapper.BuilderContext(context.indexSettings(), context.path());
        final MappedFieldType existingFieldType = context.mapperService().fullName(context.path().fullPathAsText(currentFieldName));
        Mapper.Builder builder = null;
        if (existingFieldType != null) {
            // create a builder of the same type
            builder = createBuilderFromFieldType(context, existingFieldType, currentFieldName);
            if (builder != null) {
                // best-effort to not introduce a conflict
                if (builder instanceof StringFieldMapper.Builder) {
                    StringFieldMapper.Builder stringBuilder = (StringFieldMapper.Builder) builder;
                    stringBuilder.fieldDataSettings(existingFieldType.fieldDataType().getSettings());
                    stringBuilder.store(existingFieldType.stored());
                    stringBuilder.indexOptions(existingFieldType.indexOptions());
                    stringBuilder.tokenized(existingFieldType.tokenized());
                    stringBuilder.omitNorms(existingFieldType.omitNorms());
                    stringBuilder.docValues(existingFieldType.hasDocValues());
                    stringBuilder.indexAnalyzer(existingFieldType.indexAnalyzer());
                    stringBuilder.searchAnalyzer(existingFieldType.searchAnalyzer());
                } else if (builder instanceof NumberFieldMapper.Builder) {
                    NumberFieldMapper.Builder<?,?> numberBuilder = (NumberFieldMapper.Builder<?, ?>) builder;
                    numberBuilder.fieldDataSettings(existingFieldType.fieldDataType().getSettings());
                    numberBuilder.store(existingFieldType.stored());
                    numberBuilder.indexOptions(existingFieldType.indexOptions());
                    numberBuilder.tokenized(existingFieldType.tokenized());
                    numberBuilder.omitNorms(existingFieldType.omitNorms());
                    numberBuilder.docValues(existingFieldType.hasDocValues());
                    numberBuilder.precisionStep(existingFieldType.numericPrecisionStep());
                }
            }
        }
        if (builder == null) {
            builder = createBuilderFromDynamicValue(context, token, currentFieldName);
        }
        Mapper mapper = builder.build(builderContext);

        mapper = parseAndMergeUpdate(mapper, context);

        ObjectMapper update = null;
        if (mapper != null) {
            update = parentMapper.mappingUpdate(mapper);
        }
        return update;
    }
コード例 #8
0
  private void serializeArray(ParseContext context, String lastFieldName) throws IOException {
    String arrayFieldName = lastFieldName;
    Mapper mapper = mappers.get(lastFieldName);
    if (mapper != null) {
      // There is a concrete mapper for this field already. Need to check if the mapper
      // expects an array, if so we pass the context straight to the mapper and if not
      // we serialize the array components
      if (mapper instanceof ArrayValueMapperParser) {
        mapper.parse(context);
      } else {
        serializeNonDynamicArray(context, lastFieldName, arrayFieldName);
      }
    } else {

      Dynamic dynamic = this.dynamic;
      if (dynamic == null) {
        dynamic = context.root().dynamic();
      }
      if (dynamic == Dynamic.STRICT) {
        throw new StrictDynamicMappingException(fullPath, arrayFieldName);
      } else if (dynamic == Dynamic.TRUE) {
        // we sync here just so we won't add it twice. Its not the end of the world
        // to sync here since next operations will get it before
        synchronized (mutex) {
          mapper = mappers.get(arrayFieldName);
          if (mapper == null) {
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, arrayFieldName, "object");
            if (builder == null) {
              serializeNonDynamicArray(context, lastFieldName, arrayFieldName);
              return;
            }
            BuilderContext builderContext =
                new BuilderContext(context.indexSettings(), context.path());
            mapper = builder.build(builderContext);
            if (mapper != null && mapper instanceof ArrayValueMapperParser) {
              putDynamicMapper(context, arrayFieldName, mapper);
            } else {
              serializeNonDynamicArray(context, lastFieldName, arrayFieldName);
            }
          } else {

            serializeNonDynamicArray(context, lastFieldName, arrayFieldName);
          }
        }
      } else {

        serializeNonDynamicArray(context, lastFieldName, arrayFieldName);
      }
    }
  }
コード例 #9
0
  private static ObjectMapper parseArray(
      ParseContext context, ObjectMapper parentMapper, String lastFieldName) throws IOException {
    String arrayFieldName = lastFieldName;
    Mapper mapper = parentMapper.getMapper(lastFieldName);
    if (mapper != null) {
      // There is a concrete mapper for this field already. Need to check if the mapper
      // expects an array, if so we pass the context straight to the mapper and if not
      // we serialize the array components
      if (mapper instanceof ArrayValueMapperParser) {
        final Mapper subUpdate = parseObjectOrField(context, mapper);
        if (subUpdate != null) {
          // propagate the mapping update
          return parentMapper.mappingUpdate(subUpdate);
        } else {
          return null;
        }
      } else {
        return parseNonDynamicArray(context, parentMapper, lastFieldName, arrayFieldName);
      }
    } else {

      ObjectMapper.Dynamic dynamic = parentMapper.dynamic();
      if (dynamic == null) {
        dynamic = dynamicOrDefault(context.root().dynamic());
      }
      if (dynamic == ObjectMapper.Dynamic.STRICT) {
        throw new StrictDynamicMappingException(parentMapper.fullPath(), arrayFieldName);
      } else if (dynamic == ObjectMapper.Dynamic.TRUE) {
        Mapper.Builder builder =
            context.root().findTemplateBuilder(context, arrayFieldName, "object");
        if (builder == null) {
          return parseNonDynamicArray(context, parentMapper, lastFieldName, arrayFieldName);
        }
        Mapper.BuilderContext builderContext =
            new Mapper.BuilderContext(context.indexSettings(), context.path());
        mapper = builder.build(builderContext);
        if (mapper != null && mapper instanceof ArrayValueMapperParser) {
          context.path().add(arrayFieldName);
          mapper = parseAndMergeUpdate(mapper, context);
          return parentMapper.mappingUpdate(mapper);
        } else {
          return parseNonDynamicArray(context, parentMapper, lastFieldName, arrayFieldName);
        }
      } else {
        return parseNonDynamicArray(context, parentMapper, lastFieldName, arrayFieldName);
      }
    }
  }
コード例 #10
0
  private void serializeValue(
      final ParseContext context, String currentFieldName, XContentParser.Token token)
      throws IOException {
    if (currentFieldName == null) {
      throw new MapperParsingException(
          "object mapping ["
              + name
              + "] trying to serialize a value with no field associated with it, current value ["
              + context.parser().textOrNull()
              + "]");
    }
    Mapper mapper = mappers.get(currentFieldName);
    if (mapper != null) {
      mapper.parse(context);
      return;
    }
    Dynamic dynamic = this.dynamic;
    if (dynamic == null) {
      dynamic = context.root().dynamic();
    }
    if (dynamic == Dynamic.STRICT) {
      throw new StrictDynamicMappingException(fullPath, currentFieldName);
    }
    if (dynamic == Dynamic.FALSE) {
      return;
    }
    // we sync here since we don't want to add this field twice to the document mapper
    // its not the end of the world, since we add it to the mappers once we create it
    // so next time we won't even get here for this field
    boolean newMapper = false;
    synchronized (mutex) {
      mapper = mappers.get(currentFieldName);
      if (mapper == null) {
        newMapper = true;
        BuilderContext builderContext = new BuilderContext(context.indexSettings(), context.path());
        if (token == XContentParser.Token.VALUE_STRING) {
          boolean resolved = false;

          // do a quick test to see if its fits a dynamic template, if so, use it.
          // we need to do it here so we can handle things like attachment templates, where calling
          // text (to see if its a date) causes the binary value to be cleared
          if (!resolved) {
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "string", null);
            if (builder != null) {
              mapper = builder.build(builderContext);
              resolved = true;
            }
          }

          if (!resolved && context.parser().textLength() == 0) {
            // empty string with no mapping, treat it like null value
            return;
          }

          if (!resolved && context.root().dateDetection()) {
            String text = context.parser().text();
            // a safe check since "1" gets parsed as well
            if (Strings.countOccurrencesOf(text, ":") > 1
                || Strings.countOccurrencesOf(text, "-") > 1
                || Strings.countOccurrencesOf(text, "/") > 1) {
              for (FormatDateTimeFormatter dateTimeFormatter :
                  context.root().dynamicDateTimeFormatters()) {
                try {
                  dateTimeFormatter.parser().parseMillis(text);
                  Mapper.Builder builder =
                      context.root().findTemplateBuilder(context, currentFieldName, "date");
                  if (builder == null) {
                    builder = dateField(currentFieldName).dateTimeFormatter(dateTimeFormatter);
                  }
                  mapper = builder.build(builderContext);
                  resolved = true;
                  break;
                } catch (Exception e) {
                  // failure to parse this, continue
                }
              }
            }
          }
          if (!resolved && context.root().numericDetection()) {
            String text = context.parser().text();
            try {
              Long.parseLong(text);
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "long");
              if (builder == null) {
                builder = longField(currentFieldName);
              }
              mapper = builder.build(builderContext);
              resolved = true;
            } catch (Exception e) {
              // not a long number
            }
            if (!resolved) {
              try {
                Double.parseDouble(text);
                Mapper.Builder builder =
                    context.root().findTemplateBuilder(context, currentFieldName, "double");
                if (builder == null) {
                  builder = doubleField(currentFieldName);
                }
                mapper = builder.build(builderContext);
                resolved = true;
              } catch (Exception e) {
                // not a long number
              }
            }
          }
          // DON'T do automatic ip detection logic, since it messes up with docs that have hosts and
          // ips
          // check if its an ip
          //                if (!resolved && text.indexOf('.') != -1) {
          //                    try {
          //                        IpFieldMapper.ipToLong(text);
          //                        XContentMapper.Builder builder =
          // context.root().findTemplateBuilder(context, currentFieldName, "ip");
          //                        if (builder == null) {
          //                            builder = ipField(currentFieldName);
          //                        }
          //                        mapper = builder.build(builderContext);
          //                        resolved = true;
          //                    } catch (Exception e) {
          //                        // failure to parse, not ip...
          //                    }
          //                }
          if (!resolved) {
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "string");
            if (builder == null) {
              builder = stringField(currentFieldName);
            }
            mapper = builder.build(builderContext);
          }
        } else if (token == XContentParser.Token.VALUE_NUMBER) {
          XContentParser.NumberType numberType = context.parser().numberType();
          if (numberType == XContentParser.NumberType.INT) {
            if (context.parser().estimatedNumberType()) {
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "long");
              if (builder == null) {
                builder = longField(currentFieldName);
              }
              mapper = builder.build(builderContext);
            } else {
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "integer");
              if (builder == null) {
                builder = integerField(currentFieldName);
              }
              mapper = builder.build(builderContext);
            }
          } else if (numberType == XContentParser.NumberType.LONG) {
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "long");
            if (builder == null) {
              builder = longField(currentFieldName);
            }
            mapper = builder.build(builderContext);
          } else if (numberType == XContentParser.NumberType.FLOAT) {
            if (context.parser().estimatedNumberType()) {
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "double");
              if (builder == null) {
                builder = doubleField(currentFieldName);
              }
              mapper = builder.build(builderContext);
            } else {
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "float");
              if (builder == null) {
                builder = floatField(currentFieldName);
              }
              mapper = builder.build(builderContext);
            }
          } else if (numberType == XContentParser.NumberType.DOUBLE) {
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "double");
            if (builder == null) {
              builder = doubleField(currentFieldName);
            }
            mapper = builder.build(builderContext);
          }
        } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
          Mapper.Builder builder =
              context.root().findTemplateBuilder(context, currentFieldName, "boolean");
          if (builder == null) {
            builder = booleanField(currentFieldName);
          }
          mapper = builder.build(builderContext);
        } else if (token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) {
          Mapper.Builder builder =
              context.root().findTemplateBuilder(context, currentFieldName, "binary");
          if (builder == null) {
            builder = binaryField(currentFieldName);
          }
          mapper = builder.build(builderContext);
        } else {
          Mapper.Builder builder =
              context.root().findTemplateBuilder(context, currentFieldName, null);
          if (builder != null) {
            mapper = builder.build(builderContext);
          } else {
            // TODO how do we identify dynamically that its a binary value?
            throw new ElasticSearchIllegalStateException(
                "Can't handle serializing a dynamic type with content token ["
                    + token
                    + "] and field name ["
                    + currentFieldName
                    + "]");
          }
        }
        putMapper(mapper);
        context.setMappingsModified();
      }
    }
    if (newMapper) {
      mapper.traverse(context.newFieldMappers());
    }
    mapper.parse(context);
  }
コード例 #11
0
  /** Creates an copy of the current field with given field name and boost */
  private static void parseCopy(String field, ParseContext context) throws IOException {
    FieldMapper fieldMapper = context.docMapper().mappers().getMapper(field);
    if (fieldMapper != null) {
      fieldMapper.parse(context);
    } else {
      // The path of the dest field might be completely different from the current one so we need to
      // reset it
      context = context.overridePath(new ContentPath(0));

      String[] paths = Strings.splitStringToArray(field, '.');
      String fieldName = paths[paths.length - 1];
      ObjectMapper mapper = context.root();
      ObjectMapper[] mappers = new ObjectMapper[paths.length - 1];
      if (paths.length > 1) {
        ObjectMapper parent = context.root();
        for (int i = 0; i < paths.length - 1; i++) {
          mapper = context.docMapper().objectMappers().get(context.path().pathAsText(paths[i]));
          if (mapper == null) {
            // One mapping is missing, check if we are allowed to create a dynamic one.
            ObjectMapper.Dynamic dynamic = parent.dynamic();
            if (dynamic == null) {
              dynamic = dynamicOrDefault(context.root().dynamic());
            }

            switch (dynamic) {
              case STRICT:
                throw new StrictDynamicMappingException(parent.fullPath(), paths[i]);
              case TRUE:
                Mapper.Builder builder =
                    context.root().findTemplateBuilder(context, paths[i], "object");
                if (builder == null) {
                  // if this is a non root object, then explicitly set the dynamic behavior if set
                  if (!(parent instanceof RootObjectMapper)
                      && parent.dynamic() != ObjectMapper.Defaults.DYNAMIC) {
                    ((ObjectMapper.Builder) builder).dynamic(parent.dynamic());
                  }
                  builder = MapperBuilders.object(paths[i]).enabled(true);
                }
                Mapper.BuilderContext builderContext =
                    new Mapper.BuilderContext(context.indexSettings(), context.path());
                mapper = (ObjectMapper) builder.build(builderContext);
                if (mapper.nested() != ObjectMapper.Nested.NO) {
                  throw new MapperParsingException(
                      "It is forbidden to create dynamic nested objects (["
                          + context.path().pathAsText(paths[i])
                          + "]) through `copy_to`");
                }
                break;
              case FALSE:
                // Maybe we should log something to tell the user that the copy_to is ignored in
                // this case.
                break;
              default:
                throw new AssertionError("Unexpected dynamic type " + dynamic);
            }
          }
          context.path().add(paths[i]);
          mappers[i] = mapper;
          parent = mapper;
        }
      }
      ObjectMapper update =
          parseDynamicValue(context, mapper, fieldName, context.parser().currentToken());
      assert update
          != null; // we are parsing a dynamic value so we necessarily created a new mapping

      if (paths.length > 1) {
        for (int i = paths.length - 2; i >= 0; i--) {
          ObjectMapper parent = context.root();
          if (i > 0) {
            parent = mappers[i - 1];
          }
          assert parent != null;
          update = parent.mappingUpdate(update);
        }
      }
      context.addDynamicMappingsUpdate(update);
    }
  }
コード例 #12
0
  private void serializeObject(final ParseContext context, String currentFieldName)
      throws IOException {
    if (currentFieldName == null) {
      throw new MapperParsingException(
          "object mapping ["
              + name
              + "] trying to serialize an object with no field associated with it, current value ["
              + context.parser().textOrNull()
              + "]");
    }
    context.path().add(currentFieldName);

    Mapper objectMapper = mappers.get(currentFieldName);
    if (objectMapper != null) {
      objectMapper.parse(context);
    } else {
      Dynamic dynamic = this.dynamic;
      if (dynamic == null) {
        dynamic = context.root().dynamic();
      }
      if (dynamic == Dynamic.STRICT) {
        throw new StrictDynamicMappingException(currentFieldName);
      } else if (dynamic == Dynamic.TRUE) {
        // we sync here just so we won't add it twice. Its not the end of the world
        // to sync here since next operations will get it before
        boolean newMapper = false;
        synchronized (mutex) {
          objectMapper = mappers.get(currentFieldName);
          if (objectMapper == null) {
            newMapper = true;
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "object");
            if (builder == null) {
              builder =
                  MapperBuilders.object(currentFieldName)
                      .enabled(true)
                      .dynamic(dynamic)
                      .pathType(pathType);
            }
            // remove the current field name from path, since the object builder adds it as well...
            context.path().remove();
            BuilderContext builderContext =
                new BuilderContext(context.indexSettings(), context.path());
            objectMapper = builder.build(builderContext);
            putMapper(objectMapper);
            // now re add it
            context.path().add(currentFieldName);
            context.addedMapper();
          }
        }
        // traverse and parse outside of the mutex
        if (newMapper) {
          // we need to traverse in case we have a dynamic template and need to add field mappers
          // introduced by it
          objectMapper.traverse(
              new FieldMapperListener() {
                @Override
                public void fieldMapper(FieldMapper fieldMapper) {
                  context.docMapper().addFieldMapper(fieldMapper);
                }
              });
          objectMapper.traverse(
              new ObjectMapperListener() {
                @Override
                public void objectMapper(ObjectMapper objectMapper) {
                  context.docMapper().addObjectMapper(objectMapper);
                }
              });
        }
        // now, parse it
        objectMapper.parse(context);
      } else {
        // not dynamic, read everything up to end object
        context.parser().skipChildren();
      }
    }

    context.path().remove();
  }
コード例 #13
0
  public void parseDynamicValue(
      final ParseContext context, String currentFieldName, XContentParser.Token token)
      throws IOException {
    Dynamic dynamic = this.dynamic;
    if (dynamic == null) {
      dynamic = context.root().dynamic();
    }
    if (dynamic == Dynamic.STRICT) {
      throw new StrictDynamicMappingException(fullPath, currentFieldName);
    }
    if (dynamic == Dynamic.FALSE) {
      return;
    }
    // we sync here since we don't want to add this field twice to the document mapper
    // its not the end of the world, since we add it to the mappers once we create it
    // so next time we won't even get here for this field
    synchronized (mutex) {
      Mapper mapper = mappers.get(currentFieldName);
      if (mapper == null) {
        BuilderContext builderContext = new BuilderContext(context.indexSettings(), context.path());
        if (token == XContentParser.Token.VALUE_STRING) {
          boolean resolved = false;

          // do a quick test to see if its fits a dynamic template, if so, use it.
          // we need to do it here so we can handle things like attachment templates, where calling
          // text (to see if its a date) causes the binary value to be cleared
          if (!resolved) {
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "string", null);
            if (builder != null) {
              mapper = builder.build(builderContext);
              resolved = true;
            }
          }

          if (!resolved && context.parser().textLength() == 0) {
            // empty string with no mapping, treat it like null value
            return;
          }

          if (!resolved && context.root().dateDetection()) {
            String text = context.parser().text();
            // a safe check since "1" gets parsed as well
            if (Strings.countOccurrencesOf(text, ":") > 1
                || Strings.countOccurrencesOf(text, "-") > 1
                || Strings.countOccurrencesOf(text, "/") > 1) {
              for (FormatDateTimeFormatter dateTimeFormatter :
                  context.root().dynamicDateTimeFormatters()) {
                try {
                  dateTimeFormatter.parser().parseMillis(text);
                  Mapper.Builder builder =
                      context.root().findTemplateBuilder(context, currentFieldName, "date");
                  if (builder == null) {
                    builder = dateField(currentFieldName).dateTimeFormatter(dateTimeFormatter);
                  }
                  mapper = builder.build(builderContext);
                  resolved = true;
                  break;
                } catch (Exception e) {
                  // failure to parse this, continue
                }
              }
            }
          }
          if (!resolved && context.root().numericDetection()) {
            String text = context.parser().text();
            try {
              Long.parseLong(text);
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "long");
              if (builder == null) {
                builder = longField(currentFieldName);
              }
              mapper = builder.build(builderContext);
              resolved = true;
            } catch (Exception e) {
              // not a long number
            }
            if (!resolved) {
              try {
                Double.parseDouble(text);
                Mapper.Builder builder =
                    context.root().findTemplateBuilder(context, currentFieldName, "double");
                if (builder == null) {
                  builder = doubleField(currentFieldName);
                }
                mapper = builder.build(builderContext);
                resolved = true;
              } catch (Exception e) {
                // not a long number
              }
            }
          }
          if (!resolved) {
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "string");
            if (builder == null) {
              builder = stringField(currentFieldName);
            }
            mapper = builder.build(builderContext);
          }
        } else if (token == XContentParser.Token.VALUE_NUMBER) {
          XContentParser.NumberType numberType = context.parser().numberType();
          if (numberType == XContentParser.NumberType.INT) {
            if (context.parser().estimatedNumberType()) {
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "long");
              if (builder == null) {
                builder = longField(currentFieldName);
              }
              mapper = builder.build(builderContext);
            } else {
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "integer");
              if (builder == null) {
                builder = integerField(currentFieldName);
              }
              mapper = builder.build(builderContext);
            }
          } else if (numberType == XContentParser.NumberType.LONG) {
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "long");
            if (builder == null) {
              builder = longField(currentFieldName);
            }
            mapper = builder.build(builderContext);
          } else if (numberType == XContentParser.NumberType.FLOAT) {
            if (context.parser().estimatedNumberType()) {
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "double");
              if (builder == null) {
                builder = doubleField(currentFieldName);
              }
              mapper = builder.build(builderContext);
            } else {
              Mapper.Builder builder =
                  context.root().findTemplateBuilder(context, currentFieldName, "float");
              if (builder == null) {
                builder = floatField(currentFieldName);
              }
              mapper = builder.build(builderContext);
            }
          } else if (numberType == XContentParser.NumberType.DOUBLE) {
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "double");
            if (builder == null) {
              builder = doubleField(currentFieldName);
            }
            mapper = builder.build(builderContext);
          }
        } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
          Mapper.Builder builder =
              context.root().findTemplateBuilder(context, currentFieldName, "boolean");
          if (builder == null) {
            builder = booleanField(currentFieldName);
          }
          mapper = builder.build(builderContext);
        } else if (token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) {
          Mapper.Builder builder =
              context.root().findTemplateBuilder(context, currentFieldName, "binary");
          if (builder == null) {
            builder = binaryField(currentFieldName);
          }
          mapper = builder.build(builderContext);
        } else {
          Mapper.Builder builder =
              context.root().findTemplateBuilder(context, currentFieldName, null);
          if (builder != null) {
            mapper = builder.build(builderContext);
          } else {
            // TODO how do we identify dynamically that its a binary value?
            throw new ElasticsearchIllegalStateException(
                "Can't handle serializing a dynamic type with content token ["
                    + token
                    + "] and field name ["
                    + currentFieldName
                    + "]");
          }
        }

        if (context.isWithinNewMapper()) {
          mapper.parse(context);
        } else {
          context.setWithinNewMapper();
          try {
            mapper.parse(context);
            FieldMapperListener.Aggregator newFields = new FieldMapperListener.Aggregator();
            mapper.traverse(newFields);
            context.docMapper().addFieldMappers(newFields.mappers);
          } finally {
            context.clearWithinNewMapper();
          }
        }

        // only put after we traversed and did the callbacks, so other parsing won't see it only
        // after we
        // properly traversed it and adding the mappers
        putMapper(mapper);
        context.setMappingsModified();
      } else {
        mapper.parse(context);
      }
    }
  }
コード例 #14
0
    @Override
    public AttachmentMapper build(BuilderContext context) {
      ContentPath.Type origPathType = context.path().pathType();
      context.path().pathType(pathType);

      FieldMapper contentMapper;
      if (context.indexCreatedVersion().before(Version.V_2_0_0_beta1)) {
        // old behavior, we need the content to be indexed under the attachment field name
        if (contentBuilder instanceof FieldMapper.Builder == false) {
          throw new IllegalStateException("content field for attachment must be a field mapper");
        }
        ((FieldMapper.Builder) contentBuilder).indexName(name);
        contentBuilder.name = name + "." + FieldNames.CONTENT;
        contentMapper = (FieldMapper) contentBuilder.build(context);
        context.path().add(name);
      } else {
        context.path().add(name);
        contentMapper = (FieldMapper) contentBuilder.build(context);
      }

      FieldMapper dateMapper = (FieldMapper) dateBuilder.build(context);
      FieldMapper authorMapper = (FieldMapper) authorBuilder.build(context);
      FieldMapper titleMapper = (FieldMapper) titleBuilder.build(context);
      FieldMapper nameMapper = (FieldMapper) nameBuilder.build(context);
      FieldMapper keywordsMapper = (FieldMapper) keywordsBuilder.build(context);
      FieldMapper contentTypeMapper = (FieldMapper) contentTypeBuilder.build(context);
      FieldMapper contentLength = (FieldMapper) contentLengthBuilder.build(context);
      FieldMapper language = (FieldMapper) languageBuilder.build(context);
      context.path().remove();

      context.path().pathType(origPathType);

      if (defaultIndexedChars == null && context.indexSettings() != null) {
        defaultIndexedChars =
            context.indexSettings().getAsInt("index.mapping.attachment.indexed_chars", 100000);
      }
      if (defaultIndexedChars == null) {
        defaultIndexedChars = 100000;
      }

      if (ignoreErrors == null && context.indexSettings() != null) {
        ignoreErrors =
            context
                .indexSettings()
                .getAsBoolean("index.mapping.attachment.ignore_errors", Boolean.TRUE);
      }
      if (ignoreErrors == null) {
        ignoreErrors = Boolean.TRUE;
      }

      if (langDetect == null && context.indexSettings() != null) {
        langDetect =
            context
                .indexSettings()
                .getAsBoolean("index.mapping.attachment.detect_language", Boolean.FALSE);
      }
      if (langDetect == null) {
        langDetect = Boolean.FALSE;
      }
      MappedFieldType defaultFieldType = Defaults.FIELD_TYPE.clone();
      if (this.fieldType.indexOptions() != IndexOptions.NONE && !this.fieldType.tokenized()) {
        defaultFieldType.setOmitNorms(true);
        defaultFieldType.setIndexOptions(IndexOptions.DOCS);
        if (!this.omitNormsSet && this.fieldType.boost() == 1.0F) {
          this.fieldType.setOmitNorms(true);
        }

        if (!this.indexOptionsSet) {
          this.fieldType.setIndexOptions(IndexOptions.DOCS);
        }
      }

      defaultFieldType.freeze();
      this.setupFieldType(context);
      return new AttachmentMapper(
          name,
          fieldType,
          defaultFieldType,
          pathType,
          defaultIndexedChars,
          ignoreErrors,
          langDetect,
          contentMapper,
          dateMapper,
          titleMapper,
          nameMapper,
          authorMapper,
          keywordsMapper,
          contentTypeMapper,
          contentLength,
          language,
          context.indexSettings(),
          multiFieldsBuilder.build(this, context),
          copyTo);
    }