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"));
}
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]"));
}
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);
}
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);
}
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;
}
Aggregations