org.elasticsearch.index.mapper.object.RootObjectMapper Java Examples

The following examples show how to use org.elasticsearch.index.mapper.object.RootObjectMapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocalWeatherDataMapper.java    From JavaElasticSearchExperiment with MIT License 6 votes vote down vote up
@Override
protected void configure(RootObjectMapper.Builder builder) {
    builder
            .add(new DateFieldMapper.Builder("dateTime"))
            .add(new FloatFieldMapper.Builder("temperature"))
            .add(new FloatFieldMapper.Builder("windSpeed"))
            .add(new FloatFieldMapper.Builder("stationPressure"))
            .add(new StringFieldMapper.Builder("skyCondition"))
            .add(new ObjectMapper.Builder("station")
                    .add(new StringFieldMapper.Builder("wban"))
                    .add(new StringFieldMapper.Builder("name"))
                    .add(new StringFieldMapper.Builder("state"))
                    .add(new StringFieldMapper.Builder("location"))
                    .add(new GeoPointFieldMapper.Builder("coordinates")
                            .enableLatLon(true)
                            .enableGeoHash(false))
                    .nested(ObjectMapper.Nested.newNested(true, false)));
}
 
Example #2
Source File: LocalWeatherDataMapper.java    From ElasticUtils with MIT License 6 votes vote down vote up
@Override
protected void configureRootObjectBuilder(RootObjectMapper.Builder builder) {
    builder
            .add(new DateFieldMapper.Builder("dateTime"))
            .add(new FloatFieldMapper.Builder("temperature"))
            .add(new FloatFieldMapper.Builder("windSpeed"))
            .add(new FloatFieldMapper.Builder("stationPressure"))
            .add(new StringFieldMapper.Builder("skyCondition"))
            .add(new ObjectMapper.Builder("station")
                    .add(new StringFieldMapper.Builder("wban"))
                    .add(new StringFieldMapper.Builder("name"))
                    .add(new StringFieldMapper.Builder("state"))
                    .add(new StringFieldMapper.Builder("location"))
                    .add(new GeoPointFieldMapper.Builder("coordinates")
                            .enableLatLon(true)
                            .enableGeoHash(false))
                    .nested(ObjectMapper.Nested.newNested(true, false)));
}
 
Example #3
Source File: BaseElasticSearchMapping.java    From ElasticUtils with MIT License 6 votes vote down vote up
public XContentBuilder internalGetMapping() throws IOException {

        // Configure the RootObjectMapper:
        RootObjectMapper.Builder rootObjectMapperBuilder = getRootObjectBuilder();

        // Populate the Settings:
        Settings.Builder settingsBuilder = getSettingsBuilder();

        // Build the Mapping:
        Mapping mapping = new Mapping(
                version,
                rootObjectMapperBuilder.build(new Mapper.BuilderContext(settingsBuilder.build(), new ContentPath())),
                getMetaDataFieldMappers(),
                getSourceTransforms(),
                getMetaData());

        // Turn it into JsonXContent:
        return mapping.toXContent(JsonXContent.contentBuilder().startObject(), ToXContent.EMPTY_PARAMS);
    }
 
Example #4
Source File: Mapping.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/** @see DocumentMapper#merge(Mapping, boolean) */
public Mapping merge(Mapping mergeWith, boolean updateAllTypes) {
    RootObjectMapper mergedRoot = root.merge(mergeWith.root, updateAllTypes);
    Map<Class<? extends MetadataFieldMapper>, MetadataFieldMapper> mergedMetaDataMappers = new HashMap<>(metadataMappersMap);
    for (MetadataFieldMapper metaMergeWith : mergeWith.metadataMappers) {
        MetadataFieldMapper mergeInto = mergedMetaDataMappers.get(metaMergeWith.getClass());
        MetadataFieldMapper merged;
        if (mergeInto == null) {
            merged = metaMergeWith;
        } else {
            merged = mergeInto.merge(metaMergeWith, updateAllTypes);
        }
        mergedMetaDataMappers.put(merged.getClass(), merged);
    }
    return new Mapping(indexCreated, mergedRoot, mergedMetaDataMappers.values().toArray(new MetadataFieldMapper[0]), sourceTransforms, mergeWith.meta);
}
 
Example #5
Source File: Mapping.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public Mapping(Version indexCreated, RootObjectMapper rootObjectMapper, MetadataFieldMapper[] metadataMappers, SourceTransform[] sourceTransforms, ImmutableMap<String, Object> meta) {
    this.indexCreated = indexCreated;
    this.metadataMappers = metadataMappers;
    ImmutableMap.Builder<Class<? extends MetadataFieldMapper>, MetadataFieldMapper> builder = ImmutableMap.builder();
    for (MetadataFieldMapper metadataMapper : metadataMappers) {
        if (indexCreated.before(Version.V_2_0_0_beta1) && LEGACY_INCLUDE_IN_OBJECT.contains(metadataMapper.name())) {
            rootObjectMapper = rootObjectMapper.copyAndPutMapper(metadataMapper);
        }
        builder.put(metadataMapper.getClass(), metadataMapper);
    }
    this.root = rootObjectMapper;
    // keep root mappers sorted for consistent serialization
    Arrays.sort(metadataMappers, new Comparator<Mapper>() {
        @Override
        public int compare(Mapper o1, Mapper o2) {
            return o1.name().compareTo(o2.name());
        }
    });
    this.metadataMappersMap = builder.build();
    this.sourceTransforms = sourceTransforms;
    this.meta = meta;
}
 
Example #6
Source File: Mapping.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively update sub field types.
 */
public Mapping updateFieldType(Map<String, MappedFieldType> fullNameToFieldType) {
    final MetadataFieldMapper[] updatedMeta = Arrays.copyOf(metadataMappers, metadataMappers.length);
    for (int i = 0; i < updatedMeta.length; ++i) {
        updatedMeta[i] = (MetadataFieldMapper) updatedMeta[i].updateFieldType(fullNameToFieldType);
    }
    RootObjectMapper updatedRoot = root.updateFieldType(fullNameToFieldType);
    return new Mapping(indexCreated, updatedRoot, updatedMeta, sourceTransforms, meta);
}
 
Example #7
Source File: Mapping.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/** Return the root object mapper. */
public RootObjectMapper root() {
    return root;
}
 
Example #8
Source File: Mapping.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Generate a mapping update for the given root object mapper.
 */
public Mapping mappingUpdate(Mapper rootObjectMapper) {
    return new Mapping(indexCreated, (RootObjectMapper) rootObjectMapper, metadataMappers, sourceTransforms, meta);
}
 
Example #9
Source File: DocumentMapperParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked"})
private DocumentMapper parse(String type, Map<String, Object> mapping, String defaultSource) throws MapperParsingException {
    if (type == null) {
        throw new MapperParsingException("Failed to derive type");
    }

    if (defaultSource != null) {
        Tuple<String, Map<String, Object>> t = extractMapping(MapperService.DEFAULT_MAPPING, defaultSource);
        if (t.v2() != null) {
            XContentHelper.mergeDefaults(mapping, t.v2());
        }
    }


    Mapper.TypeParser.ParserContext parserContext = parserContext(type);
    // parse RootObjectMapper
    DocumentMapper.Builder docBuilder = doc((RootObjectMapper.Builder) rootObjectTypeParser.parse(type, mapping, parserContext), mapperService);
    Iterator<Map.Entry<String, Object>> iterator = mapping.entrySet().iterator();
    // parse DocumentMapper
    while(iterator.hasNext()) {
        Map.Entry<String, Object> entry = iterator.next();
        String fieldName = Strings.toUnderscoreCase(entry.getKey());
        Object fieldNode = entry.getValue();

        if ("transform".equals(fieldName)) {
            deprecationLogger.deprecated("Mapping transform is deprecated and will be removed in the next major version");
            if (fieldNode instanceof Map) {
                parseTransform(docBuilder, (Map<String, Object>) fieldNode, parserContext.indexVersionCreated());
            } else if (fieldNode instanceof List) {
                for (Object transformItem: (List)fieldNode) {
                    if (!(transformItem instanceof Map)) {
                        throw new MapperParsingException("Elements of transform list must be objects but one was:  " + fieldNode);
                    }
                    parseTransform(docBuilder, (Map<String, Object>) transformItem, parserContext.indexVersionCreated());
                }
            } else {
                throw new MapperParsingException("Transform must be an object or an array but was:  " + fieldNode);
            }
            iterator.remove();
        } else {
            MetadataFieldMapper.TypeParser typeParser = rootTypeParsers.get(fieldName);
            if (typeParser != null) {
                iterator.remove();
                Map<String, Object> fieldNodeMap = (Map<String, Object>) fieldNode;
                docBuilder.put((MetadataFieldMapper.Builder)typeParser.parse(fieldName, fieldNodeMap, parserContext));
                fieldNodeMap.remove("type");
                checkNoRemainingFields(fieldName, fieldNodeMap, parserContext.indexVersionCreated());
            }
        }
    }

    ImmutableMap<String, Object> attributes = ImmutableMap.of();
    if (mapping.containsKey("_meta")) {
        attributes = ImmutableMap.copyOf((Map<String, Object>) mapping.remove("_meta"));
    }
    docBuilder.meta(attributes);

    checkNoRemainingFields(mapping, parserContext.indexVersionCreated(), "Root mapping definition has unsupported parameters: ");

    return docBuilder.build(mapperService);
}
 
Example #10
Source File: MapperBuilders.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static RootObjectMapper.Builder rootObject(String name) {
    return new RootObjectMapper.Builder(name);
}
 
Example #11
Source File: MapperBuilders.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static DocumentMapper.Builder doc(RootObjectMapper.Builder objectBuilder, MapperService mapperService) {
    return new DocumentMapper.Builder(objectBuilder, mapperService);
}
 
Example #12
Source File: DocumentParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
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).pathType(mapper.pathType());
                // 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;
}
 
Example #13
Source File: DocumentMapper.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public RootObjectMapper root() {
    return mapping.root;
}
 
Example #14
Source File: ParseContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public RootObjectMapper root() {
    return docMapper.root();
}
 
Example #15
Source File: ParseContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public RootObjectMapper root() {
    return in.root();
}
 
Example #16
Source File: BaseElasticSearchMapping.java    From ElasticUtils with MIT License 3 votes vote down vote up
private RootObjectMapper.Builder getRootObjectBuilder() {
    RootObjectMapper.Builder rootObjectMapperBuilder = new RootObjectMapper.Builder(indexType);

    configureRootObjectBuilder(rootObjectMapperBuilder);

    return rootObjectMapperBuilder;
}
 
Example #17
Source File: ParseContext.java    From Elasticsearch with Apache License 2.0 votes vote down vote up
public abstract RootObjectMapper root(); 
Example #18
Source File: BaseElasticSearchMapping.java    From ElasticUtils with MIT License votes vote down vote up
protected abstract void configureRootObjectBuilder(RootObjectMapper.Builder builder);