12
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Elasticsearch Plugin 作成

Last updated at Posted at 2015-02-24

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

サンプルソース

参考

12
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?