Elasticsearch has a variety of plugins that allow you to customize and enhance features such as search and analysis. There are many plugins created by the community as well as the official ones included in the Elasticsearch project. Here, we will explain how to create your own plugin.
Elasticsearch plugins include Analysis Plugin and [Mapper Plugin](https: / /www.elastic.co/guide/en/elasticsearch/plugins/6.5/mapper.html) and so on. Before you write your own plugin, you need to be clear about what features you want to add to Elasticseach and what kind of plugins those features are.
For example, if you want to improve the search function in Japanese, divide it with Japanese (kuromoji) Analysis Filter. If you need to do more processing on the tokens, you need to make your own Analysis plugin (token filter).
There is very little information about making your own Elasticsearch plugins, very limited in Japanese and in recent versions.
There are various plugin projects on GitHub, but only a few are compatible with the new version. So, first, look for a reference implementation (source code) from the official Elasticsearch plugins (Core Plugins).
If you make your own Analysis plugin, refer to the source code of the plugin whose name starts with ʻanalysis-` in the following repository.
The required files are the following four.
pom.xml
plugin.xml
plugin-descriptor.properties
(Plugin name) Plugin.java
The Java class of the plug-in itself, which is built, is loaded at startup.Instead of writing these files from scratch, it's a good idea to copy the referenced plugin project, make sure the build is successful, and then gradually customize it.
For example, in the elasticsearch-concatenation-token-filter that I created, it is as follows.
.
├── README.md
├── pom.xml
├── src
│ └── main
│ ├── assemblies
│ │ └── plugin.xml
│ ├── java
│ │ └── com
│ │ └── github
│ │ └── ryohashimoto
│ │ └── elasticsearch
│ │ ├── index
│ │ │ └── analysis
│ │ │ ├── ConcatenationTokenFilter.java
│ │ │ └── ConcatenationTokenFilterFactory.java
│ │ └── plugin
│ │ └── analysis
│ │ └── ConcatenationTokenFilterPlugin.java
| └── resources
| └── plugin-descriptor.properties
└── target
When your project is ready, implement the plugin source code in Java.
Again, the Java class to implement is based on the source code of the existing plugin.
As an example, elasticsearch-concatenation-token-filter implements the TokenFilter class and its Factory class.
ConcatenationTokenFilterPlugin.java
)public class ConcatenationTokenFilterPlugin extends Plugin implements AnalysisPlugin {
@Override
public Map<String, AnalysisProvider<TokenFilterFactory>> getTokenFilters() {
return singletonMap("concatenation", ConcatenationTokenFilterFactory::new);
}
}
ConcatenationTokenFilterFactory.java
)public class ConcatenationTokenFilterFactory extends AbstractTokenFilterFactory {
public ConcatenationTokenFilterFactory(IndexSettings indexSettings, Environment environment, String name, Settings settings) {
super(indexSettings, name, settings);
}
@Override
public TokenStream create(TokenStream tokenStream) {
return new ConcatenationTokenFilter(tokenStream);
}
}
ConcatenationTokenFilter.java
)public class ConcatenationTokenFilter extends TokenFilter {
...
}
Check the pom.xml
setting because you need to match the version of Elasticsearch used when building the plugin with the version of Elasticsearch you are installing.
For example, if you want to use Elasticsearch 6.5.0 for both build and install:
<elasticsearch.version>6.5.0</elasticsearch.version>
Build with the following command.
mvn package
If the output BUILD SUCCESS
is output as shown below, it means that the build was successful.
[INFO] Building zip: /Users/ryo/Repos/elasticsearch-concatenation-token-filter/target/releases/elasticsearch-concatenation-token-filter-6.5.0.1.zip
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.833 s
[INFO] Finished at: 2018-12-09T23:22:14+09:00
[INFO] ------------------------------------------------------------------------
Use the following command to perform the installation.
elasticsearch-plugin install file://(Plugin file path)
In the (plug-in file path)
part here, specify the path of the ZIP file output by mvn package
(the part afterBuilding zip:
).
If the installation is successful, set it according to the type of plug-in and try using it.
For the Analysis plugin, try applying it to the analyzer settings to make sure you get the correct results.
It's easy, but I've summarized how to create and use an Elasticsearch plugin. I'm glad if you can use it as a reference.
Recommended Posts