Exemplo n.º 1
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<String, Mapper>();
      for (Mapper.Builder builder : mappersBuilders) {
        Mapper mapper = builder.build(context);
        mappers.put(mapper.name(), mapper);
      }
      context.path().pathType(origPathType);
      context.path().remove();

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

      return (Y) objectMapper;
    }
Exemplo n.º 2
0
 public ObjectMapper putMapper(Mapper mapper) {
   if (mapper instanceof AllFieldMapper.IncludeInAll) {
     ((AllFieldMapper.IncludeInAll) mapper).includeInAllIfNotSet(includeInAll);
   }
   synchronized (mutex) {
     mappers = newMapBuilder(mappers).put(mapper.name(), mapper).immutableMap();
   }
   return this;
 }
Exemplo n.º 3
0
 public ObjectMapper putMapper(Mapper mapper) {
   if (mapper instanceof AllFieldMapper.IncludeInAll) {
     ((AllFieldMapper.IncludeInAll) mapper).includeInAllIfNotSet(includeInAll);
   }
   synchronized (mutex) {
     mappers = mappers.copyAndPut(mapper.name(), mapper);
   }
   return this;
 }
Exemplo n.º 4
0
 public ObjectMapper putMapper(Mapper mapper) {
   if (mapper instanceof AllFieldMapper.IncludeInAll) {
     ((AllFieldMapper.IncludeInAll) mapper).includeInAllIfNotSet(includeInAll);
   }
   synchronized (mutex) {
     UpdateInPlaceMap<String, Mapper>.Mutator mappingMutator = this.mappers.mutator();
     mappingMutator.put(mapper.name(), mapper);
     mappingMutator.close();
   }
   return this;
 }
Exemplo n.º 5
0
  public void toXContent(
      XContentBuilder builder, Params params, ToXContent custom, Mapper... additionalMappers)
      throws IOException {
    builder.startObject(name);
    if (nested.isNested()) {
      builder.field("type", NESTED_CONTENT_TYPE);
      if (nested.isIncludeInParent()) {
        builder.field("include_in_parent", true);
      }
      if (nested.isIncludeInRoot()) {
        builder.field("include_in_root", true);
      }
    } else if (mappers
        .isEmpty()) { // only write the object content type if there are no properties, otherwise,
                      // it is automatically detected
      builder.field("type", CONTENT_TYPE);
    }
    // grr, ugly! on root, dynamic defaults to TRUE, on children, it defaults to null to
    // inherit the root behavior
    if (this instanceof RootObjectMapper) {
      if (dynamic != Dynamic.TRUE) {
        builder.field("dynamic", dynamic.name().toLowerCase());
      }
    } else {
      if (dynamic != Defaults.DYNAMIC) {
        builder.field("dynamic", dynamic.name().toLowerCase());
      }
    }
    if (enabled != Defaults.ENABLED) {
      builder.field("enabled", enabled);
    }
    if (pathType != Defaults.PATH_TYPE) {
      builder.field("path", pathType.name().toLowerCase());
    }
    if (includeInAll != null) {
      builder.field("include_in_all", includeInAll);
    }

    if (custom != null) {
      custom.toXContent(builder, params);
    }

    doXContent(builder, params);

    // sort the mappers so we get consistent serialization format
    TreeMap<String, Mapper> sortedMappers = new TreeMap<String, Mapper>(mappers);

    // check internal mappers first (this is only relevant for root object)
    for (Mapper mapper : sortedMappers.values()) {
      if (mapper instanceof InternalMapper) {
        mapper.toXContent(builder, params);
      }
    }
    if (additionalMappers != null && additionalMappers.length > 0) {
      TreeMap<String, Mapper> additionalSortedMappers = new TreeMap<String, Mapper>();
      for (Mapper mapper : additionalMappers) {
        additionalSortedMappers.put(mapper.name(), mapper);
      }

      for (Mapper mapper : additionalSortedMappers.values()) {
        mapper.toXContent(builder, params);
      }
    }

    if (!mappers.isEmpty()) {
      builder.startObject("properties");
      for (Mapper mapper : sortedMappers.values()) {
        if (!(mapper instanceof InternalMapper)) {
          mapper.toXContent(builder, params);
        }
      }
      builder.endObject();
    }
    builder.endObject();
  }
Exemplo n.º 6
0
  @Override
  public void merge(final Mapper mergeWith, final MergeContext mergeContext)
      throws MergeMappingException {
    if (!(mergeWith instanceof ObjectMapper)) {
      mergeContext.addConflict(
          "Can't merge a non object mapping ["
              + mergeWith.name()
              + "] with an object mapping ["
              + name()
              + "]");
      return;
    }
    ObjectMapper mergeWithObject = (ObjectMapper) mergeWith;

    if (nested().isNested()) {
      if (!mergeWithObject.nested().isNested()) {
        mergeContext.addConflict(
            "object mapping [" + name() + "] can't be changed from nested to non-nested");
        return;
      }
    } else {
      if (mergeWithObject.nested().isNested()) {
        mergeContext.addConflict(
            "object mapping [" + name() + "] can't be changed from non-nested to nested");
        return;
      }
    }

    doMerge(mergeWithObject, mergeContext);

    List<Mapper> mappersToTraverse = new ArrayList<Mapper>();
    synchronized (mutex) {
      for (Mapper mergeWithMapper : mergeWithObject.mappers.values()) {
        Mapper mergeIntoMapper = mappers.get(mergeWithMapper.name());
        if (mergeIntoMapper == null) {
          // no mapping, simply add it if not simulating
          if (!mergeContext.mergeFlags().simulate()) {
            putMapper(mergeWithMapper);
            mappersToTraverse.add(mergeWithMapper);
          }
        } else {
          if ((mergeWithMapper instanceof MultiFieldMapper)
              && !(mergeIntoMapper instanceof MultiFieldMapper)) {
            MultiFieldMapper mergeWithMultiField = (MultiFieldMapper) mergeWithMapper;
            mergeWithMultiField.merge(mergeIntoMapper, mergeContext);
            if (!mergeContext.mergeFlags().simulate()) {
              putMapper(mergeWithMultiField);
              // now, record mappers to traverse events for all mappers
              for (Mapper mapper : mergeWithMultiField.mappers().values()) {
                mappersToTraverse.add(mapper);
              }
            }
          } else {
            mergeIntoMapper.merge(mergeWithMapper, mergeContext);
          }
        }
      }
    }
    // call this outside of the mutex
    for (Mapper mapper : mappersToTraverse) {
      mapper.traverse(mergeContext.newFieldMappers());
      mapper.traverse(mergeContext.newObjectMappers());
    }
  }
Exemplo n.º 7
0
  public void toXContent(
      XContentBuilder builder, Params params, ToXContent custom, Mapper... additionalMappers)
      throws IOException {
    builder.startObject(name);
    if (nested.isNested()) {
      builder.field("type", NESTED_CONTENT_TYPE);
      if (nested.isIncludeInParent()) {
        builder.field("include_in_parent", true);
      }
      if (nested.isIncludeInRoot()) {
        builder.field("include_in_root", true);
      }
    } else if (mappers
        .isEmpty()) { // only write the object content type if there are no properties, otherwise,
                      // it is automatically detected
      builder.field("type", CONTENT_TYPE);
    }
    if (dynamic != null) {
      builder.field("dynamic", dynamic.name().toLowerCase(Locale.ROOT));
    }
    if (enabled != Defaults.ENABLED) {
      builder.field("enabled", enabled);
    }
    if (pathType != Defaults.PATH_TYPE) {
      builder.field("path", pathType.name().toLowerCase(Locale.ROOT));
    }
    if (includeInAll != null) {
      builder.field("include_in_all", includeInAll);
    }

    if (custom != null) {
      custom.toXContent(builder, params);
    }

    doXContent(builder, params);

    // sort the mappers so we get consistent serialization format
    Mapper[] sortedMappers = Iterables.toArray(mappers.values(), Mapper.class);
    Arrays.sort(
        sortedMappers,
        new Comparator<Mapper>() {
          @Override
          public int compare(Mapper o1, Mapper o2) {
            return o1.name().compareTo(o2.name());
          }
        });

    // check internal mappers first (this is only relevant for root object)
    for (Mapper mapper : sortedMappers) {
      if (mapper instanceof InternalMapper) {
        mapper.toXContent(builder, params);
      }
    }
    if (additionalMappers != null && additionalMappers.length > 0) {
      TreeMap<String, Mapper> additionalSortedMappers = new TreeMap<>();
      for (Mapper mapper : additionalMappers) {
        additionalSortedMappers.put(mapper.name(), mapper);
      }

      for (Mapper mapper : additionalSortedMappers.values()) {
        mapper.toXContent(builder, params);
      }
    }

    if (!mappers.isEmpty()) {
      builder.startObject("properties");
      for (Mapper mapper : sortedMappers) {
        if (!(mapper instanceof InternalMapper)) {
          mapper.toXContent(builder, params);
        }
      }
      builder.endObject();
    }
    builder.endObject();
  }
Exemplo n.º 8
0
  @Override
  public void merge(final Mapper mergeWith, final MergeContext mergeContext)
      throws MergeMappingException {
    if (!(mergeWith instanceof ObjectMapper)) {
      mergeContext.addConflict(
          "Can't merge a non object mapping ["
              + mergeWith.name()
              + "] with an object mapping ["
              + name()
              + "]");
      return;
    }
    ObjectMapper mergeWithObject = (ObjectMapper) mergeWith;

    if (nested().isNested()) {
      if (!mergeWithObject.nested().isNested()) {
        mergeContext.addConflict(
            "object mapping [" + name() + "] can't be changed from nested to non-nested");
        return;
      }
    } else {
      if (mergeWithObject.nested().isNested()) {
        mergeContext.addConflict(
            "object mapping [" + name() + "] can't be changed from non-nested to nested");
        return;
      }
    }

    if (!mergeContext.mergeFlags().simulate()) {
      if (mergeWithObject.dynamic != null) {
        this.dynamic = mergeWithObject.dynamic;
      }
    }

    doMerge(mergeWithObject, mergeContext);

    List<Mapper> mappersToPut = new ArrayList<>();
    FieldMapperListener.Aggregator newFieldMappers = new FieldMapperListener.Aggregator();
    ObjectMapperListener.Aggregator newObjectMappers = new ObjectMapperListener.Aggregator();
    synchronized (mutex) {
      for (Mapper mapper : mergeWithObject.mappers.values()) {
        Mapper mergeWithMapper = mapper;
        Mapper mergeIntoMapper = mappers.get(mergeWithMapper.name());
        if (mergeIntoMapper == null) {
          // no mapping, simply add it if not simulating
          if (!mergeContext.mergeFlags().simulate()) {
            mappersToPut.add(mergeWithMapper);
            mergeWithMapper.traverse(newFieldMappers);
            mergeWithMapper.traverse(newObjectMappers);
          }
        } else {
          mergeIntoMapper.merge(mergeWithMapper, mergeContext);
        }
      }
      if (!newFieldMappers.mappers.isEmpty()) {
        mergeContext.docMapper().addFieldMappers(newFieldMappers.mappers);
      }
      if (!newObjectMappers.mappers.isEmpty()) {
        mergeContext.docMapper().addObjectMappers(newObjectMappers.mappers);
      }
      // and the mappers only after the administration have been done, so it will not be visible to
      // parser (which first try to read with no lock)
      for (Mapper mapper : mappersToPut) {
        putMapper(mapper);
      }
    }
  }