Search in sources :

Example 1 with FieldAlias

use of com.b2international.index.mapping.FieldAlias in project snow-owl by b2ihealthcare.

the class EsIndexAdmin method toProperties.

private Map<String, Object> toProperties(DocumentMapping mapping) {
    Map<String, Object> properties = newHashMap();
    for (Field field : mapping.getFields()) {
        // skip transient fields
        if (Modifier.isTransient(field.getModifiers())) {
            continue;
        }
        com.b2international.index.mapping.Field fieldAnnotation = field.getAnnotation(com.b2international.index.mapping.Field.class);
        final String property = field.getName();
        final Class<?> fieldType = NumericClassUtils.unwrapCollectionType(field);
        if (Map.class.isAssignableFrom(fieldType)) {
            // allow dynamic mappings for dynamic objects like field using Map
            final Map<String, Object> prop = newHashMap();
            prop.put("type", "object");
            prop.put("dynamic", "true");
            properties.put(property, prop);
            continue;
        } else if (fieldType.isAnnotationPresent(Doc.class)) {
            Doc annotation = fieldType.getAnnotation(Doc.class);
            // this is a nested document type create a nested mapping
            final Map<String, Object> prop = newHashMap();
            // XXX type: object is the default for nested objects, ES won't store it in the mapping and will default to object even if explicitly set, which would cause unnecessary mapping update during boot
            if (annotation.nested()) {
                prop.put("type", "nested");
            }
            // XXX enabled: true is the default, ES won't store it in the mapping and will default to true even if explicitly set, which would cause unnecessary mapping update during boot
            if (!annotation.index() || (fieldAnnotation != null && !fieldAnnotation.index())) {
                prop.put("enabled", false);
            }
            prop.putAll(toProperties(new DocumentMapping(fieldType, true)));
            properties.put(property, prop);
        } else {
            final Map<String, Object> prop = newHashMap();
            addFieldProperties(prop, fieldType);
            // add aliases
            final Map<String, FieldAlias> fieldAliases = mapping.getFieldAliases(property);
            if (!fieldAliases.isEmpty()) {
                final Map<String, Object> fields = newHashMapWithExpectedSize(fieldAliases.size());
                for (FieldAlias fieldAlias : fieldAliases.values()) {
                    final Map<String, Object> fieldAliasProps = newHashMap();
                    // only keywords can have normalizers
                    switch(fieldAlias.type()) {
                        case KEYWORD:
                            fieldAliasProps.put("type", "keyword");
                            String normalizer = fieldAlias.normalizer().getNormalizer();
                            if (!Strings.isNullOrEmpty(normalizer)) {
                                fieldAliasProps.put("normalizer", normalizer);
                            }
                            // XXX doc_values: true is the default, ES won't store it in the mapping and will default to true even if explicitly set, which would cause unnecessary mapping update during boot
                            if (!fieldAlias.index()) {
                                fieldAliasProps.put("index", false);
                                fieldAliasProps.put("doc_values", false);
                            }
                            break;
                        case TEXT:
                            fieldAliasProps.put("type", "text");
                            fieldAliasProps.put("analyzer", fieldAlias.analyzer().getAnalyzer());
                            if (fieldAlias.searchAnalyzer() != Analyzers.INDEX) {
                                fieldAliasProps.put("search_analyzer", fieldAlias.searchAnalyzer().getAnalyzer());
                            }
                            // XXX index: true is the default, ES won't store it in the mapping and will default to true even if explicitly set, which would cause unnecessary mapping update during boot
                            if (!fieldAlias.index()) {
                                fieldAliasProps.put("index", false);
                            }
                            break;
                        default:
                            throw new UnsupportedOperationException("Unknown field alias type: " + fieldAlias.type());
                    }
                    fields.put(fieldAlias.name(), fieldAliasProps);
                }
                prop.put("fields", fields);
            }
            // XXX enabled: true is the default, ES won't store it in the mapping and will default to true even if explicitly set, which would cause unnecessary mapping update during boot
            if (fieldAnnotation != null && !fieldAnnotation.index()) {
                prop.put("index", false);
                prop.put("doc_values", false);
            }
            // register mapping
            properties.put(property, prop);
        }
    }
    return ImmutableMap.of("properties", properties);
}
Also used : DocumentMapping(com.b2international.index.mapping.DocumentMapping) Field(java.lang.reflect.Field) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) ImmutableMap(com.google.common.collect.ImmutableMap) FieldAlias(com.b2international.index.mapping.FieldAlias)

Aggregations

DocumentMapping (com.b2international.index.mapping.DocumentMapping)1 FieldAlias (com.b2international.index.mapping.FieldAlias)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)1 Field (java.lang.reflect.Field)1