org.elasticsearch.index.mapper.DocumentMapperParser Java Examples

The following examples show how to use org.elasticsearch.index.mapper.DocumentMapperParser. 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: ContextBuilder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
protected static ContextMapping loadMapping(String name, Map<String, Object> config, Version indexVersionCreated)
        throws ElasticsearchParseException {
    final Object argType = config.get(ContextMapping.FIELD_TYPE);

    if (argType == null) {
        throw new ElasticsearchParseException("missing [{}] in context mapping", ContextMapping.FIELD_TYPE);
    }

    final String type = argType.toString();
    ContextMapping contextMapping;
    if (GeolocationContextMapping.TYPE.equals(type)) {
        contextMapping = GeolocationContextMapping.load(name, config);
    } else if (CategoryContextMapping.TYPE.equals(type)) {
        contextMapping = CategoryContextMapping.load(name, config);
    } else {
        throw new ElasticsearchParseException("unknown context type [{}]", type);
    }
    config.remove(ContextMapping.FIELD_TYPE);
    DocumentMapperParser.checkNoRemainingFields(name, config, indexVersionCreated);

    return contextMapping;
}
 
Example #2
Source File: MapperTestUtils.java    From elasticsearch-analysis-baseform with Apache License 2.0 6 votes vote down vote up
public static DocumentMapperParser newDocumentMapperParser(Settings settings) {
    Settings forcedSettings = Settings.builder()
            .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
            .put(settings)
            .build();
    SimilarityLookupService similarityLookupService = newSimilarityLookupService(forcedSettings);
    Map<String, Mapper.TypeParser> mappers = registerBuiltInMappers();
    Map<String, MetadataFieldMapper.TypeParser> metadataMappers = registerBuiltInMetadataMappers();
    MapperRegistry mapperRegistry = new MapperRegistry(mappers, metadataMappers);
    MapperService mapperService = new MapperService(new Index("test"),
            forcedSettings,
            newAnalysisService(forcedSettings),
            similarityLookupService,
            null,
            mapperRegistry);
    return new DocumentMapperParser(
            forcedSettings,
            mapperService,
            newAnalysisService(forcedSettings),
            similarityLookupService,
            null,
            mapperRegistry);
}
 
Example #3
Source File: ArrayMapperTest.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * create index with type and mapping and validate DocumentMapper serialization
 */
private DocumentMapper mapper(String indexName, String mapping) throws IOException {
    IndicesModule indicesModule = new IndicesModule(Collections.singletonList(new MapperPlugin() {
        @Override
        public Map<String, Mapper.TypeParser> getMappers() {
            return Collections.singletonMap(ArrayMapper.CONTENT_TYPE, new ArrayTypeParser());
        }
    }));
    MapperService mapperService = MapperTestUtils.newMapperService(
        NamedXContentRegistry.EMPTY,
        createTempDir(),
        Settings.EMPTY,
        indicesModule,
        indexName
    );
    DocumentMapperParser parser = mapperService.documentMapperParser();

    DocumentMapper defaultMapper = parser.parse(TYPE, new CompressedXContent(mapping));
    XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
    builder.startObject();
    defaultMapper.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    String rebuildMapping = Strings.toString(builder);
    return parser.parse(TYPE, new CompressedXContent(rebuildMapping));
}
 
Example #4
Source File: ObjectMapper.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
protected static void parseProperties(ObjectMapper.Builder objBuilder, Map<String, Object> propsNode, ParserContext parserContext) {
    Iterator<Map.Entry<String, Object>> iterator = propsNode.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, Object> entry = iterator.next();
        String fieldName = entry.getKey();
        if (fieldName.contains(".")) {
            throw new MapperParsingException("Field name [" + fieldName + "] cannot contain '.'");
        }
        // Should accept empty arrays, as a work around for when the
        // user can't provide an empty Map. (PHP for example)
        boolean isEmptyList = entry.getValue() instanceof List && ((List<?>) entry.getValue()).isEmpty();

        if (entry.getValue() instanceof Map) {
            @SuppressWarnings("unchecked")
            Map<String, Object> propNode = (Map<String, Object>) entry.getValue();

            /* mark delete field when reindex */
            Object deleteNode = propNode.get("delete");
            if (deleteNode != null && deleteNode.toString().equals("true")) {
                /* TODO GaoPan: mark delete field when reindex */
                iterator.remove();
            } else {
                String type;
                Object typeNode = propNode.get("type");
                if (typeNode != null) {
                    type = typeNode.toString();
                } else {
                    // lets see if we can derive this...
                    if (propNode.get("properties") != null) {
                        type = ObjectMapper.CONTENT_TYPE;
                    } else if (propNode.size() == 1 && propNode.get("enabled") != null) {
                        // if there is a single property with the enabled
                        // flag on it, make it an object
                        // (usually, setting enabled to false to not index
                        // any type, including core values, which
                        type = ObjectMapper.CONTENT_TYPE;
                    } else {
                        throw new MapperParsingException("No type specified for field [" + fieldName + "]");
                    }
                }

                Mapper.TypeParser typeParser = parserContext.typeParser(type);
                if (typeParser == null) {
                    throw new MapperParsingException("No handler for type [" + type + "] declared on field [" + fieldName + "]");
                }
                objBuilder.add(typeParser.parse(fieldName, propNode, parserContext));
                propNode.remove("type");
                DocumentMapperParser.checkNoRemainingFields(fieldName, propNode, parserContext.indexVersionCreated());
                iterator.remove();
            }
        } else if (isEmptyList) {
            iterator.remove();
        } else {
            throw new MapperParsingException("Expected map for property [fields] on field [" + fieldName + "] but got a "
                    + fieldName.getClass());
        }
    }

    DocumentMapperParser.checkNoRemainingFields(propsNode, parserContext.indexVersionCreated(),
            "DocType mapping definition has unsupported parameters: ");

}
 
Example #5
Source File: TypeParsers.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Parse common field attributes such as {@code doc_values} or {@code store}.
 */
public static void parseField(FieldMapper.Builder builder, String name, Map<String, Object> fieldNode, Mapper.TypeParser.ParserContext parserContext) {
    Version indexVersionCreated = parserContext.indexVersionCreated();
    for (Iterator<Map.Entry<String, Object>> iterator = fieldNode.entrySet().iterator(); iterator.hasNext();) {
        Map.Entry<String, Object> entry = iterator.next();
        final String propName = Strings.toUnderscoreCase(entry.getKey());
        final Object propNode = entry.getValue();
        if (propName.equals("index_name") && indexVersionCreated.before(Version.V_2_0_0_beta1)) {
            builder.indexName(propNode.toString());
            iterator.remove();
        } else if (propName.equals("store")) {
            builder.store(parseStore(name, propNode.toString()));
            iterator.remove();
        } else if (propName.equals("index")) {
            parseIndex(name, propNode.toString(), builder);
            iterator.remove();
        } else if (propName.equals(DOC_VALUES)) {
            builder.docValues(nodeBooleanValue(propNode));
            iterator.remove();
        } else if (propName.equals("boost")) {
            builder.boost(nodeFloatValue(propNode));
            iterator.remove();
        } else if (propName.equals("omit_norms")) {
            builder.omitNorms(nodeBooleanValue(propNode));
            iterator.remove();
        } else if (propName.equals("norms")) {
            final Map<String, Object> properties = nodeMapValue(propNode, "norms");
            for (Iterator<Entry<String, Object>> propsIterator = properties.entrySet().iterator(); propsIterator.hasNext();) {
                Entry<String, Object> entry2 = propsIterator.next();
                final String propName2 = Strings.toUnderscoreCase(entry2.getKey());
                final Object propNode2 = entry2.getValue();
                if (propName2.equals("enabled")) {
                    builder.omitNorms(!nodeBooleanValue(propNode2));
                    propsIterator.remove();
                } else if (propName2.equals(Loading.KEY)) {
                    builder.normsLoading(Loading.parse(nodeStringValue(propNode2, null), null));
                    propsIterator.remove();
                }
            }
            DocumentMapperParser.checkNoRemainingFields(propName, properties, parserContext.indexVersionCreated());
            iterator.remove();
        } else if (propName.equals("omit_term_freq_and_positions")) {
            final IndexOptions op = nodeBooleanValue(propNode) ? IndexOptions.DOCS : IndexOptions.DOCS_AND_FREQS_AND_POSITIONS;
            if (indexVersionCreated.onOrAfter(Version.V_1_0_0_RC2)) {
                throw new ElasticsearchParseException("'omit_term_freq_and_positions' is not supported anymore - use ['index_options' : 'docs']  instead");
            }
            // deprecated option for BW compat
            builder.indexOptions(op);
            iterator.remove();
        } else if (propName.equals("index_options")) {
            builder.indexOptions(nodeIndexOptionValue(propNode));
            iterator.remove();
        } else if (propName.equals("include_in_all")) {
            builder.includeInAll(nodeBooleanValue(propNode));
            iterator.remove();
        } else if (propName.equals("postings_format") && indexVersionCreated.before(Version.V_2_0_0_beta1)) {
            // ignore for old indexes
            iterator.remove();
        } else if (propName.equals("doc_values_format") && indexVersionCreated.before(Version.V_2_0_0_beta1)) {
            // ignore for old indexes
            iterator.remove();
        } else if (propName.equals("similarity")) {
            builder.similarity(parserContext.similarityLookupService().similarity(propNode.toString()));
            iterator.remove();
        } else if (propName.equals("fielddata")) {
            final Settings settings = Settings.builder().put(SettingsLoader.Helper.loadNestedFromMap(nodeMapValue(propNode, "fielddata"))).build();
            builder.fieldDataSettings(settings);
            iterator.remove();
        } else if (propName.equals("copy_to")) {
            if (parserContext.isWithinMultiField()) {
                if (indexVersionCreated.after(Version.V_2_1_0) ||
                    (indexVersionCreated.after(Version.V_2_0_1) && indexVersionCreated.before(Version.V_2_1_0))) {
                    throw new MapperParsingException("copy_to in multi fields is not allowed. Found the copy_to in field [" + name + "] which is within a multi field.");
                } else {
                    ESLoggerFactory.getLogger("mapping [" + parserContext.type() + "]").warn("Found a copy_to in field [" + name + "] which is within a multi field. This feature has been removed and the copy_to will be ignored.");
                    // we still parse this, otherwise the message will only appear once and the copy_to removed. After that it will appear again. Better to have it always.
                }
            }
            parseCopyFields(propNode, builder);
            iterator.remove();
        }
    }
    if (indexVersionCreated.before(Version.V_2_2_0)) {
        // analyzer, search_analyzer, term_vectors were accepted on all fields
        // before 2.2, even though it made little sense
        parseAnalyzersAndTermVectors(builder, name, fieldNode, parserContext);
    }
}
 
Example #6
Source File: TypeParsers.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static boolean parseMultiField(FieldMapper.Builder builder, String name, Mapper.TypeParser.ParserContext parserContext, String propName, Object propNode) {
    parserContext = parserContext.createMultiFieldContext(parserContext);
    if (propName.equals("path") && parserContext.indexVersionCreated().before(Version.V_2_0_0_beta1)) {
        builder.multiFieldPathType(parsePathType(name, propNode.toString()));
        return true;
    } else if (propName.equals("fields")) {

        final Map<String, Object> multiFieldsPropNodes;

        if (propNode instanceof List && ((List<?>) propNode).isEmpty()) {
            multiFieldsPropNodes = Collections.emptyMap();
        } else if (propNode instanceof Map) {
            multiFieldsPropNodes = (Map<String, Object>) propNode;
        } else {
            throw new MapperParsingException("expected map for property [fields] on field [" + propNode + "] or " +
                    "[" + propName + "] but got a " + propNode.getClass());
        }

        for (Map.Entry<String, Object> multiFieldEntry : multiFieldsPropNodes.entrySet()) {
            String multiFieldName = multiFieldEntry.getKey();
            if (multiFieldName.contains(".")) {
                throw new MapperParsingException("Field name [" + multiFieldName + "] which is a multi field of [" + name + "] cannot contain '.'");
            }
            if (!(multiFieldEntry.getValue() instanceof Map)) {
                throw new MapperParsingException("illegal field [" + multiFieldName + "], only fields can be specified inside fields");
            }
            @SuppressWarnings("unchecked")
            Map<String, Object> multiFieldNodes = (Map<String, Object>) multiFieldEntry.getValue();

            String type;
            Object typeNode = multiFieldNodes.get("type");
            if (typeNode != null) {
                type = typeNode.toString();
            } else {
                throw new MapperParsingException("no type specified for property [" + multiFieldName + "]");
            }
            if (type.equals(ObjectMapper.CONTENT_TYPE) || type.equals(ObjectMapper.NESTED_CONTENT_TYPE)) {
                throw new MapperParsingException("Type [" + type + "] cannot be used in multi field");
            }

            Mapper.TypeParser typeParser = parserContext.typeParser(type);
            if (typeParser == null) {
                throw new MapperParsingException("no handler for type [" + type + "] declared on field [" + multiFieldName + "]");
            }
            builder.addMultiField(typeParser.parse(multiFieldName, multiFieldNodes, parserContext));
            multiFieldNodes.remove("type");
            DocumentMapperParser.checkNoRemainingFields(propName, multiFieldNodes, parserContext.indexVersionCreated());
        }
        return true;
    }
    return false;
}
 
Example #7
Source File: MapperTestUtils.java    From elasticsearch-analysis-baseform with Apache License 2.0 4 votes vote down vote up
public static DocumentMapperParser newDocumentMapperParser() {
    return newDocumentMapperParser(Settings.builder()
            .put("path.home", System.getProperty("path.home"))
            .build());
}