本文以7.10版本elasticsearch源码为示例
elasticsearch把插件抽象成多个分类,不同分类的插件作用不同,具体分类可以看 org.elasticsearch.plugins.Plugin 类:
/**
*
* - {@link ActionPlugin}
*
- {@link AnalysisPlugin}
*
- {@link ClusterPlugin}
*
- {@link DiscoveryPlugin}
*
- {@link IngestPlugin}
*
- {@link MapperPlugin}
*
- {@link NetworkPlugin}
*
- {@link RepositoryPlugin}
*
- {@link ScriptPlugin}
*
- {@link SearchPlugin}
*
- {@link ReloadablePlugin}
*
*/
public abstract class Plugin implements Closeable {
// 省略。。。
}
插件类型介绍【官网】:https://www.elastic.co/guide/en/elasticsearch/plugins/master/intro.html
以下摘自网上:
如果我们需要对文本做处理,那么我们写的插件应该定义成AnalysisPlugin类型;我们知道elasticsearch提供了很多内置的插件,可以看看这个类
org.elasticsearch.analysis.common.CommonAnalysisPlugin
这个类注册了很多常用的分析器、分词器、过滤器、分词过滤器,自定义插件可以学习里面的写法。
接下来我们来写一个对字符加密的插件,在参看了icu插件的部分源码(icu_normalizer)后,按照下面步骤:
package com.xx.plugin.es.enc;
import com.xx.plugin.es.enc.token.EncCharTokenFilterFactory;
import org.elasticsearch.index.analysis.TokenFilterFactory;
import org.elasticsearch.indices.analysis.AnalysisModule;
import org.elasticsearch.plugins.AnalysisPlugin;
import org.elasticsearch.plugins.MapperPlugin;
import org.elasticsearch.plugins.Plugin;
import java.util.HashMap;
import java.util.Map;
public class EncPlugin extends Plugin implements AnalysisPlugin, MapperPlugin {
@Override
public Map> getTokenFilters() {
Map> extra = new HashMap<>();
extra.put("enc_normalizer", EncCharTokenFilterFactory::new);
return extra;
}
}
这个类可以通过实现 AnalysisPlugin,重写方法向es返回CharFilters、TokenFilters、Tokenizers、Analyzers等。关于这几个概念的关系可以查看 :
【Elastic知识简报】normalizer与analyzer的区别】 https://developer.aliyun.com/article/1082061
【Elasticsearch中什么是 tokenizer、analyzer、filter】 https://www.cnblogs.com/a-du/p/16272901.html
package com.xx.plugin.es.enc.token;
import org.apache.lucene.analysis.TokenStream;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.analysis.AbstractTokenFilterFactory;
import org.elasticsearch.index.analysis.NormalizingTokenFilterFactory;
/**
* 参考:
* org.elasticsearch.analysis.common.PatternReplaceCharFilterFactory
* org.elasticsearch.analysis.common.MappingCharFilterFactory
*/
public class EncCharTokenFilterFactory extends AbstractTokenFilterFactory implements NormalizingTokenFilterFactory {
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(EncCharTokenFilterFactory.class);
private EncTokenNormalizer normalizer = null;
public EncCharTokenFilterFactory(IndexSettings indexSettings, String name, Settings settings) {
super(indexSettings, name, settings);
normalizer = new EncTokenNormalizer();
}
public EncCharTokenFilterFactory(IndexSettings indexSettings, Environment environment, String name, Settings settings) {
super(indexSettings, name, settings);
}
@Override
public TokenStream create(TokenStream tokenStream) {
return new EncTokenFilter(tokenStream, normalizer);
}
}
一个空类,逻辑后续再实现。
package com.xx.plugin.es.enc.token;
public class EncTokenNormalizer {
}
package com.xx.plugin.es.enc.token;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import java.io.IOException;
public class EncTokenFilter extends TokenFilter {
private EncTokenNormalizer normalizer;
@Override
public boolean incrementToken() throws IOException {
System.out.println(">>>>>>>>>>>");
// 根据实际情况实现
return false;
}
public EncTokenFilter(TokenStream input, EncTokenNormalizer normalizer) {
super(input);
this.normalizer = normalizer;
}
}
在 resources 目录下新增两个配置文件
classname=${elasticsearch.plugin.classname}
name=${elasticsearch.plugin.name}
description=${project.description}
version=${project.version}
elasticsearch.version=${elasticsearch.version}
java.version=${maven.compiler.target}
grant {
permission java.security.AllPermission;
};
在assembly目录新增打包配置文件,打包成zip文件
enc-filter
true
true
${project.basedir}/config
config
0755
true
enc-filter/
0755
true
enc-filter/
zip
plugin-develop
false
注意:plugin-descriptor.properties和plugin-security.policy 不能 打进zip包
4.0.0
org.example
enc-plugin
1.0-SNAPSHOT
14
14
7.10.1
UTF-8
UTF-8
com.xx.plugin.es.enc.EncPlugin
enc_plugin
this is a test
org.elasticsearch
elasticsearch
${elasticsearch.version}
provided
org.apache.maven.plugins
maven-assembly-plugin
2.6
false
${basedir}/src/main/assembly/plugin.xml
${project.build.directory}/releases/
single
package
maven-compiler-plugin
org.apache.maven.plugins
3.8.1
${project.build.sourceEncoding}
${maven.compiler.target}
src/main/resources
*.properties
*.policy
false
todo
todo
本文自定义插件demo上传到了 【gitee】 https://gitee.com/aqu415/enc-plugin
参考:
https://cloud.tencent.com/developer/article/2213726