Search in sources :

Example 1 with MapperPlugin

use of org.opensearch.plugins.MapperPlugin in project OpenSearch by opensearch-project.

the class IndicesModuleTests method testGetFieldFilter.

public void testGetFieldFilter() {
    List<MapperPlugin> mapperPlugins = Arrays.asList(new MapperPlugin() {

        @Override
        public Function<String, Predicate<String>> getFieldFilter() {
            return MapperPlugin.NOOP_FIELD_FILTER;
        }
    }, new MapperPlugin() {

        @Override
        public Function<String, Predicate<String>> getFieldFilter() {
            return index -> index.equals("hidden_index") ? field -> false : MapperPlugin.NOOP_FIELD_PREDICATE;
        }
    }, new MapperPlugin() {

        @Override
        public Function<String, Predicate<String>> getFieldFilter() {
            return index -> field -> field.equals("hidden_field") == false;
        }
    }, new MapperPlugin() {

        @Override
        public Function<String, Predicate<String>> getFieldFilter() {
            return index -> index.equals("filtered") ? field -> field.equals("visible") : MapperPlugin.NOOP_FIELD_PREDICATE;
        }
    });
    IndicesModule indicesModule = new IndicesModule(mapperPlugins);
    MapperRegistry mapperRegistry = indicesModule.getMapperRegistry();
    Function<String, Predicate<String>> fieldFilter = mapperRegistry.getFieldFilter();
    assertNotSame(MapperPlugin.NOOP_FIELD_FILTER, fieldFilter);
    assertFalse(fieldFilter.apply("hidden_index").test(randomAlphaOfLengthBetween(3, 5)));
    assertTrue(fieldFilter.apply(randomAlphaOfLengthBetween(3, 5)).test(randomAlphaOfLengthBetween(3, 5)));
    assertFalse(fieldFilter.apply(randomAlphaOfLengthBetween(3, 5)).test("hidden_field"));
    assertFalse(fieldFilter.apply("filtered").test(randomAlphaOfLengthBetween(3, 5)));
    assertFalse(fieldFilter.apply("filtered").test("hidden_field"));
    assertTrue(fieldFilter.apply("filtered").test("visible"));
    assertFalse(fieldFilter.apply("hidden_index").test("visible"));
    assertTrue(fieldFilter.apply(randomAlphaOfLengthBetween(3, 5)).test("visible"));
    assertFalse(fieldFilter.apply("hidden_index").test("hidden_field"));
}
Also used : IdFieldMapper(org.opensearch.index.mapper.IdFieldMapper) Arrays(java.util.Arrays) IgnoredFieldMapper(org.opensearch.index.mapper.IgnoredFieldMapper) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) MapperPlugin(org.opensearch.plugins.MapperPlugin) Version(org.opensearch.Version) Function(java.util.function.Function) ArrayList(java.util.ArrayList) AllFieldMapper(org.opensearch.index.mapper.AllFieldMapper) DataStreamFieldMapper(org.opensearch.index.mapper.DataStreamFieldMapper) MapperRegistry(org.opensearch.indices.mapper.MapperRegistry) VersionUtils(org.opensearch.test.VersionUtils) Map(java.util.Map) VersionFieldMapper(org.opensearch.index.mapper.VersionFieldMapper) IndexFieldMapper(org.opensearch.index.mapper.IndexFieldMapper) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) FieldNamesFieldMapper(org.opensearch.index.mapper.FieldNamesFieldMapper) Mapper(org.opensearch.index.mapper.Mapper) SourceFieldMapper(org.opensearch.index.mapper.SourceFieldMapper) TypeFieldMapper(org.opensearch.index.mapper.TypeFieldMapper) List(java.util.List) TextFieldMapper(org.opensearch.index.mapper.TextFieldMapper) MetadataFieldMapper(org.opensearch.index.mapper.MetadataFieldMapper) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) RoutingFieldMapper(org.opensearch.index.mapper.RoutingFieldMapper) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) SeqNoFieldMapper(org.opensearch.index.mapper.SeqNoFieldMapper) Function(java.util.function.Function) MapperPlugin(org.opensearch.plugins.MapperPlugin) MapperRegistry(org.opensearch.indices.mapper.MapperRegistry) Matchers.containsString(org.hamcrest.Matchers.containsString) Predicate(java.util.function.Predicate)

Example 2 with MapperPlugin

use of org.opensearch.plugins.MapperPlugin in project OpenSearch by opensearch-project.

the class IndicesModuleTests method testDuplicateFieldNamesMapper.

public void testDuplicateFieldNamesMapper() {
    List<MapperPlugin> plugins = Arrays.asList(new MapperPlugin() {

        @Override
        public Map<String, MetadataFieldMapper.TypeParser> getMetadataMappers() {
            return Collections.singletonMap(FieldNamesFieldMapper.NAME, PARSER);
        }
    });
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new IndicesModule(plugins));
    assertThat(e.getMessage(), containsString("cannot contain metadata mapper [_field_names]"));
}
Also used : MetadataFieldMapper(org.opensearch.index.mapper.MetadataFieldMapper) MapperPlugin(org.opensearch.plugins.MapperPlugin) Map(java.util.Map)

Example 3 with MapperPlugin

use of org.opensearch.plugins.MapperPlugin in project OpenSearch by opensearch-project.

the class IndicesModule method getMappers.

public static 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(), type.parser());
    }
    for (RangeType type : RangeType.values()) {
        mappers.put(type.typeName(), type.parser());
    }
    mappers.put(BooleanFieldMapper.CONTENT_TYPE, BooleanFieldMapper.PARSER);
    mappers.put(BinaryFieldMapper.CONTENT_TYPE, BinaryFieldMapper.PARSER);
    DateFieldMapper.Resolution milliseconds = DateFieldMapper.Resolution.MILLISECONDS;
    mappers.put(milliseconds.type(), DateFieldMapper.MILLIS_PARSER);
    DateFieldMapper.Resolution nanoseconds = DateFieldMapper.Resolution.NANOSECONDS;
    mappers.put(nanoseconds.type(), DateFieldMapper.NANOS_PARSER);
    mappers.put(IpFieldMapper.CONTENT_TYPE, IpFieldMapper.PARSER);
    mappers.put(TextFieldMapper.CONTENT_TYPE, TextFieldMapper.PARSER);
    mappers.put(KeywordFieldMapper.CONTENT_TYPE, KeywordFieldMapper.PARSER);
    mappers.put(ObjectMapper.CONTENT_TYPE, new ObjectMapper.TypeParser());
    mappers.put(ObjectMapper.NESTED_CONTENT_TYPE, new ObjectMapper.TypeParser());
    mappers.put(CompletionFieldMapper.CONTENT_TYPE, CompletionFieldMapper.PARSER);
    mappers.put(FieldAliasMapper.CONTENT_TYPE, new FieldAliasMapper.TypeParser());
    mappers.put(GeoPointFieldMapper.CONTENT_TYPE, new GeoPointFieldMapper.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);
}
Also used : DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) GeoPointFieldMapper(org.opensearch.index.mapper.GeoPointFieldMapper) MapperPlugin(org.opensearch.plugins.MapperPlugin) FieldAliasMapper(org.opensearch.index.mapper.FieldAliasMapper) LinkedHashMap(java.util.LinkedHashMap) RangeType(org.opensearch.index.mapper.RangeType) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ObjectMapper(org.opensearch.index.mapper.ObjectMapper)

Example 4 with MapperPlugin

use of org.opensearch.plugins.MapperPlugin in project OpenSearch by opensearch-project.

the class IndicesModule method getMetadataMappers.

public 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 : builtInMetadataMappers.entrySet()) {
        if (i < builtInMetadataMappers.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);
}
Also used : MapperPlugin(org.opensearch.plugins.MapperPlugin) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with MapperPlugin

use of org.opensearch.plugins.MapperPlugin in project OpenSearch by opensearch-project.

the class MapperServiceTestCase method createMapperService.

/**
 * Create a {@link MapperService} like we would for an index.
 */
protected final MapperService createMapperService(Version version, XContentBuilder mapping) throws IOException {
    IndexMetadata meta = IndexMetadata.builder("index").settings(Settings.builder().put("index.version.created", version)).numberOfReplicas(0).numberOfShards(1).build();
    IndexSettings indexSettings = new IndexSettings(meta, getIndexSettings());
    MapperRegistry mapperRegistry = new IndicesModule(getPlugins().stream().filter(p -> p instanceof MapperPlugin).map(p -> (MapperPlugin) p).collect(toList())).getMapperRegistry();
    ScriptModule scriptModule = new ScriptModule(Settings.EMPTY, getPlugins().stream().filter(p -> p instanceof ScriptPlugin).map(p -> (ScriptPlugin) p).collect(toList()));
    ScriptService scriptService = new ScriptService(getIndexSettings(), scriptModule.engines, scriptModule.contexts);
    SimilarityService similarityService = new SimilarityService(indexSettings, scriptService, emptyMap());
    MapperService mapperService = new MapperService(indexSettings, createIndexAnalyzers(indexSettings), xContentRegistry(), similarityService, mapperRegistry, () -> {
        throw new UnsupportedOperationException();
    }, () -> true, scriptService);
    merge(mapperService, mapping);
    return mapperService;
}
Also used : IndicesModule(org.opensearch.indices.IndicesModule) BytesReference(org.opensearch.common.bytes.BytesReference) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ScriptModule(org.opensearch.script.ScriptModule) SimilarityService(org.opensearch.index.similarity.SimilarityService) ToXContent(org.opensearch.common.xcontent.ToXContent) CheckedConsumer(org.opensearch.common.CheckedConsumer) CompressedXContent(org.opensearch.common.compress.CompressedXContent) MapperPlugin(org.opensearch.plugins.MapperPlugin) Version(org.opensearch.Version) AnalyzerScope(org.opensearch.index.analysis.AnalyzerScope) MapperRegistry(org.opensearch.indices.mapper.MapperRegistry) Directory(org.apache.lucene.store.Directory) XContentFactory(org.opensearch.common.xcontent.XContentFactory) Collections.singletonMap(java.util.Collections.singletonMap) Mockito.anyString(org.mockito.Mockito.anyString) ScriptService(org.opensearch.script.ScriptService) Collections.emptyMap(java.util.Collections.emptyMap) NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) ScriptPlugin(org.opensearch.plugins.ScriptPlugin) Collections.emptyList(java.util.Collections.emptyList) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Collection(java.util.Collection) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Plugin(org.opensearch.plugins.Plugin) SearchLookup(org.opensearch.search.lookup.SearchLookup) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Collectors.toList(java.util.stream.Collectors.toList) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) BytesArray(org.opensearch.common.bytes.BytesArray) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) IndexSettings(org.opensearch.index.IndexSettings) QueryShardContext(org.opensearch.index.query.QueryShardContext) XContentType(org.opensearch.common.xcontent.XContentType) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Mockito.any(org.mockito.Mockito.any) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Collections(java.util.Collections) IndexReader(org.apache.lucene.index.IndexReader) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) Mockito.mock(org.mockito.Mockito.mock) ScriptService(org.opensearch.script.ScriptService) ScriptModule(org.opensearch.script.ScriptModule) IndicesModule(org.opensearch.indices.IndicesModule) MapperRegistry(org.opensearch.indices.mapper.MapperRegistry) MapperPlugin(org.opensearch.plugins.MapperPlugin) IndexSettings(org.opensearch.index.IndexSettings) SimilarityService(org.opensearch.index.similarity.SimilarityService) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ScriptPlugin(org.opensearch.plugins.ScriptPlugin)

Aggregations

MapperPlugin (org.opensearch.plugins.MapperPlugin)11 Map (java.util.Map)7 Matchers.containsString (org.hamcrest.Matchers.containsString)5 MetadataFieldMapper (org.opensearch.index.mapper.MetadataFieldMapper)5 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 Predicate (java.util.function.Predicate)3 Version (org.opensearch.Version)3 AllFieldMapper (org.opensearch.index.mapper.AllFieldMapper)3 DataStreamFieldMapper (org.opensearch.index.mapper.DataStreamFieldMapper)3 FieldNamesFieldMapper (org.opensearch.index.mapper.FieldNamesFieldMapper)3 IdFieldMapper (org.opensearch.index.mapper.IdFieldMapper)3 IgnoredFieldMapper (org.opensearch.index.mapper.IgnoredFieldMapper)3 IndexFieldMapper (org.opensearch.index.mapper.IndexFieldMapper)3 Mapper (org.opensearch.index.mapper.Mapper)3 RoutingFieldMapper (org.opensearch.index.mapper.RoutingFieldMapper)3 SeqNoFieldMapper (org.opensearch.index.mapper.SeqNoFieldMapper)3 SourceFieldMapper (org.opensearch.index.mapper.SourceFieldMapper)3 TextFieldMapper (org.opensearch.index.mapper.TextFieldMapper)3 TypeFieldMapper (org.opensearch.index.mapper.TypeFieldMapper)3