org.elasticsearch.plugins.MapperPlugin Java Examples

The following examples show how to use org.elasticsearch.plugins.MapperPlugin. 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: IndicesModule.java    From crate with Apache License 2.0 6 votes vote down vote up
private static Function<String, Predicate<String>> and(Function<String, Predicate<String>> first,
                                                       Function<String, Predicate<String>> second) {
    //the purpose of this method is to not chain no-op field predicates, so that we can easily find out when no plugins plug in
    //a field filter, hence skip the mappings filtering part as a whole, as it requires parsing mappings into a map.
    if (first == MapperPlugin.NOOP_FIELD_FILTER) {
        return second;
    }
    if (second == MapperPlugin.NOOP_FIELD_FILTER) {
        return first;
    }
    return index -> {
        Predicate<String> firstPredicate = first.apply(index);
        Predicate<String> secondPredicate = second.apply(index);
        if (firstPredicate == MapperPlugin.NOOP_FIELD_PREDICATE) {
            return secondPredicate;
        }
        if (secondPredicate == MapperPlugin.NOOP_FIELD_PREDICATE) {
            return firstPredicate;
        }
        return firstPredicate.and(secondPredicate);
    };
}
 
Example #2
Source File: MetaData.java    From crate with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static MappingMetaData filterFields(MappingMetaData mappingMetaData, Predicate<String> fieldPredicate) throws IOException {
    if (fieldPredicate == MapperPlugin.NOOP_FIELD_PREDICATE) {
        return mappingMetaData;
    }
    Map<String, Object> sourceAsMap = XContentHelper.convertToMap(mappingMetaData.source().compressedReference(), true).v2();
    Map<String, Object> mapping;
    if (sourceAsMap.size() == 1 && sourceAsMap.containsKey(mappingMetaData.type())) {
        mapping = (Map<String, Object>) sourceAsMap.get(mappingMetaData.type());
    } else {
        mapping = sourceAsMap;
    }

    Map<String, Object> properties = (Map<String, Object>)mapping.get("properties");
    if (properties == null || properties.isEmpty()) {
        return mappingMetaData;
    }

    filterFields("", properties, fieldPredicate);

    return new MappingMetaData(mappingMetaData.type(), sourceAsMap);
}
 
Example #3
Source File: ESTestCase.java    From crate with Apache License 2.0 6 votes vote down vote up
/** Creates an IndicesModule for testing with the given mappers and metadata mappers. */
public static IndicesModule newTestIndicesModule(Map<String, Mapper.TypeParser> extraMappers,
                                                 Map<String, MetadataFieldMapper.TypeParser> extraMetadataMappers) {
    return new IndicesModule(Collections.singletonList(
        new MapperPlugin() {
            @Override
            public Map<String, Mapper.TypeParser> getMappers() {
                return extraMappers;
            }
            @Override
            public Map<String, MetadataFieldMapper.TypeParser> getMetadataMappers() {
                return extraMetadataMappers;
            }
        }
    ));
}
 
Example #4
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 #5
Source File: IndicesModule.java    From crate with Apache License 2.0 5 votes vote down vote up
private Map<String, Mapper.TypeParser> getMappers(List<MapperPlugin> mapperPlugins) {
    Map<String, Mapper.TypeParser> mappers = new LinkedHashMap<>();

    // builtin mappers
    for (NumberFieldMapper.NumberType type : NumberFieldMapper.NumberType.values()) {
        mappers.put(type.typeName(), new NumberFieldMapper.TypeParser(type));
    }
    mappers.put(BooleanFieldMapper.CONTENT_TYPE, new BooleanFieldMapper.TypeParser());
    mappers.put(DateFieldMapper.CONTENT_TYPE, new DateFieldMapper.TypeParser());
    mappers.put(IpFieldMapper.CONTENT_TYPE, new IpFieldMapper.TypeParser());
    mappers.put(TextFieldMapper.CONTENT_TYPE, new TextFieldMapper.TypeParser());
    mappers.put(KeywordFieldMapper.CONTENT_TYPE, new KeywordFieldMapper.TypeParser());
    mappers.put(ObjectMapper.CONTENT_TYPE, new ObjectMapper.TypeParser());
    mappers.put(FieldAliasMapper.CONTENT_TYPE, new FieldAliasMapper.TypeParser());
    mappers.put(GeoPointFieldMapper.CONTENT_TYPE, new GeoPointFieldMapper.TypeParser());

    if (ShapesAvailability.JTS_AVAILABLE && ShapesAvailability.SPATIAL4J_AVAILABLE) {
        mappers.put(GeoShapeFieldMapper.CONTENT_TYPE, new GeoShapeFieldMapper.TypeParser());
    }

    for (MapperPlugin mapperPlugin : mapperPlugins) {
        for (Map.Entry<String, Mapper.TypeParser> entry : mapperPlugin.getMappers().entrySet()) {
            if (mappers.put(entry.getKey(), entry.getValue()) != null) {
                throw new IllegalArgumentException("Mapper [" + entry.getKey() + "] is already registered");
            }
        }
    }
    return Collections.unmodifiableMap(mappers);
}
 
Example #6
Source File: IndicesModule.java    From crate with Apache License 2.0 5 votes vote down vote up
private static Map<String, MetadataFieldMapper.TypeParser> getMetadataMappers(List<MapperPlugin> mapperPlugins) {
    Map<String, MetadataFieldMapper.TypeParser> metadataMappers = new LinkedHashMap<>();
    int i = 0;
    Map.Entry<String, MetadataFieldMapper.TypeParser> fieldNamesEntry = null;
    for (Map.Entry<String, MetadataFieldMapper.TypeParser> entry : BUILT_IN_METADATA_MAPPERS.entrySet()) {
        if (i < BUILT_IN_METADATA_MAPPERS.size() - 1) {
            metadataMappers.put(entry.getKey(), entry.getValue());
        } else {
            assert entry.getKey().equals(FieldNamesFieldMapper.NAME) : "_field_names must be the last registered mapper, order counts";
            fieldNamesEntry = entry;
        }
        i++;
    }
    assert fieldNamesEntry != null;

    for (MapperPlugin mapperPlugin : mapperPlugins) {
        for (Map.Entry<String, MetadataFieldMapper.TypeParser> entry : mapperPlugin.getMetadataMappers().entrySet()) {
            if (entry.getKey().equals(FieldNamesFieldMapper.NAME)) {
                throw new IllegalArgumentException("Plugin cannot contain metadata mapper [" + FieldNamesFieldMapper.NAME + "]");
            }
            if (metadataMappers.put(entry.getKey(), entry.getValue()) != null) {
                throw new IllegalArgumentException("MetadataFieldMapper [" + entry.getKey() + "] is already registered");
            }
        }
    }

    // we register _field_names here so that it has a chance to see all the other mappers, including from plugins
    metadataMappers.put(fieldNamesEntry.getKey(), fieldNamesEntry.getValue());
    return Collections.unmodifiableMap(metadataMappers);
}
 
Example #7
Source File: IndicesModule.java    From crate with Apache License 2.0 5 votes vote down vote up
private static Function<String, Predicate<String>> getFieldFilter(List<MapperPlugin> mapperPlugins) {
    Function<String, Predicate<String>> fieldFilter = MapperPlugin.NOOP_FIELD_FILTER;
    for (MapperPlugin mapperPlugin : mapperPlugins) {
        fieldFilter = and(fieldFilter, mapperPlugin.getFieldFilter());
    }
    return fieldFilter;
}
 
Example #8
Source File: MetaData.java    From crate with Apache License 2.0 5 votes vote down vote up
private static ImmutableOpenMap<String, MappingMetaData> filterFields(ImmutableOpenMap<String, MappingMetaData> mappings,
                                                                      Predicate<String> fieldPredicate) throws IOException {
    if (fieldPredicate == MapperPlugin.NOOP_FIELD_PREDICATE) {
        return mappings;
    }
    ImmutableOpenMap.Builder<String, MappingMetaData> builder = ImmutableOpenMap.builder(mappings.size());
    for (ObjectObjectCursor<String, MappingMetaData> cursor : mappings) {
        builder.put(cursor.key, filterFields(cursor.value, fieldPredicate));
    }
    return builder.build(); // No types specified means return them all
}
 
Example #9
Source File: IndicesModule.java    From crate with Apache License 2.0 4 votes vote down vote up
public IndicesModule(List<MapperPlugin> mapperPlugins) {
    this.mapperRegistry = new MapperRegistry(getMappers(mapperPlugins), getMetadataMappers(mapperPlugins),
            getFieldFilter(mapperPlugins));
    registerBuiltinWritables();
}