[JAVA] Make your own Elasticsearch plugin

Introduction

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.

Plugin type

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).

Find helpful source code

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.

Project structure

The required files are the following four.

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

Plugin implementation

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.

public class ConcatenationTokenFilterPlugin extends Plugin implements AnalysisPlugin {
    @Override
    public Map<String, AnalysisProvider<TokenFilterFactory>> getTokenFilters() {
        return singletonMap("concatenation", ConcatenationTokenFilterFactory::new);
    }
}
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);
    }
}
public class ConcatenationTokenFilter extends TokenFilter {
  ...
}

Build and install

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:).

Use plugin

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.

Summary

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.

reference

Recommended Posts

Make your own Elasticsearch plugin
Programmatically import a Maven project from your own Eclipse plugin
Create your own Java annotations
Make your own simple server in Java and understand HTTP
Make your own keyboard QMK in Docker. Volume unique to Windows
Incorporate Elasticsearch into your Rails app
Create your own Solr Function Query
Use LocationAwareLogger for your own Logger
Handle your own annotations in Java
Creating an Elasticsearch Plugin Series (2) Search
How to make a Jenkins plugin
Create your own encode for String.getBytes ()
Articles are getting messy, so make a link collection of your own articles