elasticsearch插件开发教程
检索引擎Elasticsearch支持插件模式。有些时候你可能须要安装一些插件。甚至自己开发插件,这里就提供一个開始ES插件开发演示样例,ES版本号为1.5.2。
一、插件类继承自org.elasticsearch.plugins.AbstractPlugin
packageorg.elasticsearch.plugin.helloworld;
importjava.util.ArrayList;
importjava.util.Collection;
importjava.util.Collections;
importorg.elasticsearch.common.component.LifecycleComponent;
importorg.elasticsearch.common.inject.Module;
importorg.elasticsearch.common.logging.ESLogger;
importorg.elasticsearch.common.logging.Loggers;
importorg.elasticsearch.common.settings.Settings;
importorg.elasticsearch.plugins.AbstractPlugin;
importorg.elasticsearch.rest.RestModule;
publicclassHelloWorldPluginextendsAbstractPlugin{
finalESLoggerlogger=Loggers.getLogger(getClass());
@Override
publicStringname(){
//插件名称
return"HelloWorld";
}
@Override
publicStringdescription(){
//插件描写叙述
return"HelloWorldPlugin";
}
//处理模块,由于系统中有非常多种Module,所以须要对其类型进行推断
@Override
publicvoidprocessModule(Modulemodule){
if(moduleinstanceofRestModule){
((RestModule)module).addRestAction(HelloWorldHandler.class);
}
if(moduleinstanceofHelloModule){
logger.info("##############processhellomodule#####################");
}
}
@Override
publicCollectionmodules(Settingssettings){
//创建自己的模块集合
//假设没有自己定义模块,则能够返回空
HelloModulehelloModule=newHelloModule();
ArrayListlist=newArrayList<>();
list.add(helloModule);
Collections.unmodifiableList(list);
returnlist;
}
@SuppressWarnings("rawtypes")
@Override
publicCollection>services(){
//创建自己的服务类集合,服务类须要继承自LifecycleComponent。ES会自己主动创建出服务类实例,并调用其start方法
//假设没有自己定义服务类。则能够返回空
Collection>list=newArrayList<>();
list.add(HelloService.class);
returnlist;
}
}
Module类事实上就是定义了依赖注入规则。假设不清楚,能够去查看GoogleGuice的文档,基本上是一致的。如上例中的HelloModule:
packageorg.elasticsearch.plugin.helloworld;
importorg.elasticsearch.common.inject.AbstractModule;
importorg.elasticsearch.common.inject.Scopes;
publicclassHelloModuleextendsAbstractModule{
@Override
protectedvoidconfigure(){
//将InjectableService接口类型绑定到InjectableServiceImpl实现类
//在须要注入InjectableService的地方,就会使用InjectableServiceImpl实例
bind(InjectableService.class).to(InjectableServiceImpl.class);
//使HelloService为单例状态
bind(HelloService.class).in(Scopes.SINGLETON);
}
}
不同的模块有不同的处理方式,比如样例中对于RestModule,加入了一个Handler:
packageorg.elasticsearch.plugin.helloworld;
importorg.elasticsearch.client.Client;
importorg.elasticsearch.common.inject.Inject;
importorg.elasticsearch.common.settings.Settings;
importorg.elasticsearch.rest.BaseRestHandler;
importorg.elasticsearch.rest.BytesRestResponse;
importorg.elasticsearch.rest.RestChannel;
importorg.elasticsearch.rest.RestController;
importorg.elasticsearch.rest.RestRequest;
importorg.elasticsearch.rest.RestStatus;
importorg.elasticsearch.rest.RestRequest.Method;
importorg.elasticsearch.rest.RestResponse;
publicclassHelloWorldHandlerextendsBaseRestHandler{
//注入对象
@Inject
protectedHelloWorldHandler(Settingssettings,RestControllercontroller,Clientclient){
super(settings,controller,client);
//将该Handler绑定到某訪问路径
controller.registerHandler(Method.GET,"/hello/",this);
controller.registerHandler(Method.GET,"/hello/{name}",this);
}
//处理绑定路径的请求訪问
@Override
protectedvoidhandleRequest(RestRequestrequest,RestChannelchannel,Clientclient)throwsException{
logger.debug("HelloWorldAction.handleRequestcalled");
finalStringname=request.hasParam("name")?request.param("name"):"world";
Stringcontent="{\"success\":true,\"message\":\"hello"+name+"\"}";
RestResponseresponse=newBytesRestResponse(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-GETlocalhost:9200/hello,就会返回对应结果了。
三、为插件加入页面
假设你想为你的插件加入訪问页面。则能够在ES_HOME/plugins/HelloWorld文件夹下创建一个名为"_site"的文件夹,该文件夹名称必须为_site,然后将对应的html页面放置进_site文件夹就可以,假设放置了一个名为index.html文件,则能够通过
localhost:9200/_plugin/HelloWorld/index.html进行訪问。
因为Elasticsearch提供了jsclientAPI。所以使用html静态页面与js就能够完毕对应的功能了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。