elasticsearch插件開發(fā)教程
檢索引擎Elasticsearch支持插件模式。有些時候你可能須要安裝一些插件。甚至自己開發(fā)插件,這里就提供一個開始ES插件開發(fā)演示樣例,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"; } //處理模塊,由于系統(tǒng)中有非常多種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) { //創(chuàng)建自己的模塊集合 //假設沒有自己定義模塊,則能夠返回空 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() { //創(chuàng)建自己的服務類集合,服務類須要繼承自LifecycleComponent。ES會自己主動創(chuàng)建出服務類實例,并調用其start方法 //假設沒有自己定義服務類。則能夠返回空 Collection<Class<? extends LifecycleComponent>> list = new ArrayList<>(); list.add(HelloService.class); return list; } }
Module類事實上就是定義了依賴注入規(guī)則。假設不清楚,能夠去查看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實現(xiàn)類 //在須要注入InjectableService的地方,就會使用InjectableServiceImpl實例 bind(InjectableService.class).to(InjectableServiceImpl.class); //使HelloService為單例狀態(tài) 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屬性文件,指定插件實現(xiàn)類:
plugin=org.elasticsearch.plugin.helloworld.HelloWorldPlugin
二、將插件打成jar包后安裝
如果ES_HOME代表Elasticsearch安裝文件夾。在ES_HOME/plugins文件夾下創(chuàng)建一個名為HelloWorld的文件夾。該文件夾名稱必須與插件名稱同樣(區(qū)分大寫和小寫),然后將jar包拷貝至HelloWorld文件夾,又一次啟動就可以,當你運行:
curl -GET localhost:9200/hello,就會返回對應結果了。
三、為插件加入頁面
假設你想為你的插件加入訪問頁面。則能夠在ES_HOME/plugins/HelloWorld文件夾下創(chuàng)建一個名為"_site"的文件夾,該文件夾名稱必須為_site,然后將對應的html頁面放置進_site文件夾就可以,假設放置了一個名為index.html文件,則能夠通過
localhost:9200/_plugin/HelloWorld/index.html進行訪問。
因為Elasticsearch提供了jsclientAPI。所以使用html靜態(tài)頁面與js就能夠完畢對應的功能了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
java并發(fā)編程中ReentrantLock可重入讀寫鎖
這篇文章主要介紹了java并發(fā)編程中ReentrantLock可重入讀寫鎖,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05Tk.mybatis零sql語句實現(xiàn)動態(tài)sql查詢的方法(4種)
有時候,查詢數(shù)據(jù)需要根據(jù)條件使用動態(tài)查詢,這時候需要使用動態(tài)sql,本文主要介紹了Tk.mybatis零sql語句實現(xiàn)動態(tài)sql查詢的方法,感興趣的可以了解一下2021-12-12