elasticsearch插件开发教程

时间:2021-05-20

检索引擎Elasticsearch支持插件模式。有些时候你可能须要安装一些插件。甚至自己开发插件,这里就提供一个開始ES插件开发演示样例,ES版本号为1.5.2。

一、插件类继承自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()); @Override public String name() { //插件名称 return "HelloWorld"; } @Override public String description() { //插件描写叙述 return "Hello World Plugin"; } //处理模块,由于系统中有非常多种Module,所以须要对其类型进行推断 @Override public void processModule(Module module) { if(module instanceof RestModule) { ((RestModule)module).addRestAction(HelloWorldHandler.class); } if(module instanceof HelloModule) { logger.info("############## process hello module #####################"); } } @Override public Collection<Module> modules(Settings settings) { //创建自己的模块集合 //假设没有自己定义模块,则能够返回空 HelloModule helloModule = new HelloModule(); ArrayList<Module> list = new ArrayList<>(); list.add(helloModule); Collections.unmodifiableList(list); return list; } @SuppressWarnings("rawtypes") @Override public Collection<Class<? extends LifecycleComponent>> services() { //创建自己的服务类集合,服务类须要继承自LifecycleComponent。ES会自己主动创建出服务类实例,并调用其start方法 //假设没有自己定义服务类。则能够返回空 Collection<Class<? extends LifecycleComponent>> list = new ArrayList<>(); list.add(HelloService.class); return list; } }

Module类事实上就是定义了依赖注入规则。假设不清楚,能够去查看Google Guice的文档,基本上是一致的。如上例中的HelloModule:

package org.elasticsearch.plugin.helloworld;import org.elasticsearch.common.inject.AbstractModule;import org.elasticsearch.common.inject.Scopes;public class HelloModule extends AbstractModule { @Override protected void configure() { //将InjectableService接口类型绑定到InjectableServiceImpl实现类 //在须要注入InjectableService的地方,就会使用InjectableServiceImpl实例 bind(InjectableService.class).to(InjectableServiceImpl.class); //使HelloService为单例状态 bind(HelloService.class).in(Scopes.SINGLETON); } }

不同的模块有不同的处理方式,比如样例中对于RestModule,加入了一个Handler:

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 protected HelloWorldHandler(Settings settings, RestController controller, Client client) { super(settings, controller, client); //将该Handler绑定到某訪问路径 controller.registerHandler(Method.GET, "/hello/", this); controller.registerHandler(Method.GET, "/hello/{name}", this); } //处理绑定路径的请求訪问 @Override protected 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); }}

最后在类路径根文件夹下加入一个名为es-plugin.properties属性文件,指定插件实现类:

plugin=org.elasticsearch.plugin.helloworld.HelloWorldPlugin

二、将插件打成jar包后安装

如果ES_HOME代表Elasticsearch安装文件夹。在ES_HOME/plugins文件夹下创建一个名为HelloWorld的文件夹。该文件夹名称必须与插件名称同样(区分大写和小写),然后将jar包拷贝至HelloWorld文件夹,又一次启动就可以,当你运行:
curl -GET localhost:9200/hello,就会返回对应结果了。

三、为插件加入页面

假设你想为你的插件加入訪问页面。则能够在ES_HOME/plugins/HelloWorld文件夹下创建一个名为"_site"的文件夹,该文件夹名称必须为_site,然后将对应的html页面放置进_site文件夹就可以,假设放置了一个名为index.html文件,则能够通过

localhost:9200/_plugin/HelloWorld/index.html进行訪问。
因为Elasticsearch提供了jsclientAPI。所以使用html静态页面与js就能够完毕对应的功能了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章