Elasticsearch Plug-in development

Source: Internet
Author: User

Search engine Elasticsearch support plug-in mode, sometimes you may need to install some plug-ins, or even develop their own plug-in, here is a start ES plug-in development example, ES version 5.2.0.

First, the plug-in class inherits from Org.elasticsearch.plugins.AbstractPlugin


Package Org.elasticsearch.plugin.helloworld;import Java.util.arraylist;import Java.util.collection;import Java.util.collections;import Org.elasticsearch.common.component.lifecyclecomponent;import Org.elasticsearch.common.inject.module;import Org.elasticsearch.common.logging.eslogger;import Org.elasticsearch.common.logging.loggers;import Org.elasticsearch.common.settings.settings;import Org.elasticsearch.plugins.abstractplugin;import Org.elasticsearch.rest.restmodule;public class HelloWorldPlugin Extends Abstractplugin {final Eslogger logger = Loggers.getlogger (GetClass ()); @Overridepublic String name () {// Plug-in Name return "HelloWorld";} @Overridepublic String Description () {//Plugin description return "Hello World Plugin";} Processing module, because there are many kinds of modules in the system, it is necessary to determine the type of @overridepublic void Processmodule (module module) {if (module instanceof Restmodule) {((restmodule) module). Addrestaction (Helloworldhandler.class);} if (module instanceof hellomodule) {logger.info ("############## process Hello module #####################");}} @Overridepublic collection<module> Modules (Settings Settings) {//Create your own module collection//If there are no custom modules, you can return null Hellomodule Hellomodule = new Hellomodule (); arraylist<module> list = new arraylist<> (); List.add (Hellomodule); Collections.unmodifiablelist (list); return list;} @SuppressWarnings ("Rawtypes") @Overridepublic collection<class<? Extends Lifecyclecomponent>> services () {//Create your own collection of service classes, the service class needs to inherit from Lifecyclecomponent,es automatically creates an instance of the service class and calls its Start method/ /If you do not have a custom service class, you can return an empty collection<class<? Extends lifecyclecomponent>> list = new arraylist<> (); List.add (helloservice.class); return list;}}

The module class actually defines the dependency injection rules, which, if not clear, can be used to view Google Guice documents, which are basically consistent. As in the example above hellomodule:


Package Org.elasticsearch.plugin.helloworld;import Org.elasticsearch.common.inject.abstractmodule;import Org.elasticsearch.common.inject.scopes;public class Hellomodule extends Abstractmodule {@Overrideprotected void Configure () {//Binds the Injectableservice interface type to the Injectableserviceimpl implementation class//Where injectableservice needs to be injected, The Injectableserviceimpl instance bind (Injectableservice.class) is used. to (injectableserviceimpl.class);// Make HelloService a singleton state bind (Helloservice.class). in (Scopes.singleton);}}

Different modules have different processing methods, for example, for Restmodule, a handler is added:

Package Org.elasticsearch.plugin.helloworld;import Org.elasticsearch.client.client;import Org.elasticsearch.common.inject.inject;import Org.elasticsearch.common.settings.settings;import Org.elasticsearch.rest.baseresthandler;import Org.elasticsearch.rest.bytesrestresponse;import Org.elasticsearch.rest.restchannel;import Org.elasticsearch.rest.restcontroller;import Org.elasticsearch.rest.restrequest;import Org.elasticsearch.rest.reststatus;import Org.elasticsearch.rest.restrequest.method;import Org.elasticsearch.rest.restresponse;public Class HelloWorldHandler extends Baseresthandler {//Inject object @Inject protected HelloWorldHandler (Settings Settings, Restcontrol LER controller, client client) {Super (settings, controller, client);//Bind the handler to an Access path Controller.registerhandler ( Method.get, "/hello/", this); Controller.registerhandler (Method.get, "/hello/{name}", this);} Request access to process binding path @overrideprotected void HandleRequest (restrequest request, Restchannel Channel, client client) throws Exception {Logger.debug ("helloworldaction.handlerequest called"); final String name = Request.hasparam ("name")? Request.param (" Name "):" World "; String content = "{\" success\ ": true, \" message\ ": \" Hello "+name+" \ "}"; Restresponse response = new Bytesrestresponse (Reststatus.ok, Bytesrestresponse.text_content_type, CONTENT); Channel.sendresponse (response);}}

Finally, under the Classpath root, add a file named Es-plugin.properties that specifies the plug-in implementation class:
Plugin=org.elasticsearch.plugin.helloworld.helloworldplugin


Second, the plug into the jar package after installation

Suppose Es_home represents the Elasticsearch installation directory. In the Es_home/plugins directory, create a directory named HelloWorld, which must be the same name as the plug-in (case-sensitive), and then copy the jar package to the HelloWorld directory and restart it when you execute:

Curl-get Localhost:9200/hello, it will return the corresponding results.

Third, add pages for plugins
If you want to add access pages for your plugin, you can create a directory named "_site" in the Es_home/plugins/helloworld directory, which must be _site, and then place the corresponding HTML page in the _site directory. If a file named Index.html is placed, you can

Localhost:9200/_plugin/helloworld/index.html for access.
Since Elasticsearch provides the JS client API, the HTML static page and JS can be used to complete the corresponding functions.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Elasticsearch Plug-in development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.