Help us understand the problem. What is going on with this article?

Elasticsearch Plugin 作成

More than 5 years have passed since last update.

Elasticsearch Plugin 作成

Elasticsearch Plugin 作成手順のメモ

Directory 構成

├── pom.xml
├── src
│   └── main
│       ├── assemblies
│       │   └── plugin.xml
│       ├── java
│       │   └── org
│       │       └── elasticsearch
│       │           └── plugin
│       │               └── example
│       │                   ├── ExamplePlugin.java
│       │                   ├── ExampleSimilarityProvider.java
│       │                   └── ExampleSimilarity.java
│       └── resources
│           └── es-plugin.properties
│
$ mkdir example-plugin
$ cd example-plugin
$ mkdir -p src/main/{java,resources,assemblies}
$ mkdir -p src/main/java/org/elasticsearch/plugin/example

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <name>example-plugin</name>

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.elasticsearch.plugin.example</groupId>
    <artifactId>example-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <properties>
        <elasticsearch.version>1.4.3</elasticsearch.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>${elasticsearch.version}</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <outputDirectory>${project.build.directory}/releases/</outputDirectory>
                    <descriptors>
                        <descriptor>${basedir}/src/main/assemblies/plugin.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

src/main/assemblies/plugin.xml

<?xml version="1.0"?>
<assembly>
    <id>plugin</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <useTransitiveFiltering>true</useTransitiveFiltering>
            <excludes>
                <exclude>org.elasticsearch:elasticsearch</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

src/main/resources/es-plugin.properties

plugin=org.elasticsearch.plugin.example.ExamplePlugin

ExamplePlugin.java

package org.elasticsearch.plugin.example;

import org.elasticsearch.plugins.AbstractPlugin;
import org.elasticsearch.index.similarity.SimilarityModule;

import java.util.Collection;


public class ExamplePlugin extends AbstractPlugin {
    @Override public String name() {
        return "example-plugin";
    }

    @Override public String description() {
        return "Example Plugin Description";
    }

    public void onModule(SimilarityModule module){
        module.addSimilarity("similarity", ExampleSimilarityProvider.class);
    }
}

ExampleSimilarityProvider.java

package org.elasticsearch.plugin.example;

import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.assistedinject.Assisted;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.similarity.AbstractSimilarityProvider;

public class ExampleSimilarityProvider extends AbstractSimilarityProvider {

    private final ExampleSimilarity similarity;

    @Inject
    public ExampleSimilarityProvider( @Assisted String name, @Assisted Settings settings) {
    super(name);
    this.similarity = new ExampleSimilarity();
    }

    public ExampleSimilarity get() {
    return similarity;
    }
}

ExampleSimilarity.java

package org.elasticsearch.plugin.example;

import java.io.IOException;

import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.FieldInvertState;
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.search.CollectionStatistics;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.TermStatistics;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.search.similarities.DefaultSimilarity;

public class ExampleSimilarity extends DefaultSimilarity {

    public ExampleSimilarity() {
    super();
    }

    @Override
    public float tf(float freq){
    return 1.0f;
    }

    @Override
    public float idf(long docFreq, long numDocs){
        return 1.0f;
    }
}

パッケージ作成

$ mvn package

pluginインストール

$ cd elasticsearch
$ ./bin/plugin --url file:///[exaple-plugin-path]/example-plugin/target/releases/example-plugin-1.0-SNAPSHOT.zip install example-plugin

サンプルソース

https://github.com/shinjiikeda/elasticsearch_plugin_example/tree/master

参考

https://www.found.no/foundation/writing-a-plugin/

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away