如何利用elasticsearch插件進行開發(fā)

如何利用elasticsearch插件進行開發(fā)?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司長期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為新榮企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、成都網(wǎng)站制作,新榮網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(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)建自己的模塊集合
 //假設(shè)沒有自己定義模塊,則能夠返回空
 HelloModule helloModule = new HelloModule();
 ArrayList<Module> list = new ArrayList<>();
 list.add(helloModule);
 Collections.unmodifiableList(list);
 return list;
 }
 
 @SuppressWarnings("rawtypes")
 @Override
 public Collection<Class<&#63; extends LifecycleComponent>> services() {
 //創(chuàng)建自己的服務(wù)類集合,服務(wù)類須要繼承自LifecycleComponent。ES會自己主動創(chuàng)建出服務(wù)類實例,并調(diào)用其start方法
 //假設(shè)沒有自己定義服務(wù)類。則能夠返回空
 Collection<Class<&#63;
 extends LifecycleComponent>> list = new ArrayList<>();
 list.add(HelloService.class);
 return list;
 }
 
}

Module類事實上就是定義了依賴注入規(guī)則。假設(shè)不清楚,能夠去查看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") &#63; 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文件夾,又一次啟動就可以,當(dāng)你運行:
curl -GET localhost:9200/hello,就會返回對應(yīng)結(jié)果了。

三、為插件加入頁面

假設(shè)你想為你的插件加入訪問頁面。則能夠在ES_HOME/plugins/HelloWorld文件夾下創(chuàng)建一個名為"_site"的文件夾,該文件夾名稱必須為_site,然后將對應(yīng)的html頁面放置進_site文件夾就可以,假設(shè)放置了一個名為index.html文件,則能夠通過

localhost:9200/_plugin/HelloWorld/index.html進行訪問。
因為Elasticsearch提供了jsclientAPI。所以使用html靜態(tài)頁面與js就能夠完畢對應(yīng)的功能了。

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

文章名稱:如何利用elasticsearch插件進行開發(fā)
鏈接地址:http://bm7419.com/article44/igieee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、域名注冊、App開發(fā)、網(wǎng)站策劃、App設(shè)計、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司