檢索引擎Elasticsearch支持插件模式。有些時(shí)候你可能須要安裝一些插件。甚至自己開發(fā)插件,這里就提供一個(gè)開始ES插件開發(fā)演示樣例,ES版本號(hào)為1.5.2。
一、插件類繼承自org.elasticsearch.plugins.AbstractPlugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
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,所以須要對(duì)其類型進(jìn)行推斷 @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<? extends LifecycleComponent>> services() { //創(chuàng)建自己的服務(wù)類集合,服務(wù)類須要繼承自LifecycleComponent。ES會(huì)自己主動(dòng)創(chuàng)建出服務(wù)類實(shí)例,并調(diào)用其start方法 //假設(shè)沒有自己定義服務(wù)類。則能夠返回空 Collection<Class<? extends LifecycleComponent>> list = new ArrayList<>(); list.add(HelloService. class ); return list; } } |
Module類事實(shí)上就是定義了依賴注入規(guī)則。假設(shè)不清楚,能夠去查看Google Guice的文檔,基本上是一致的。如上例中的HelloModule:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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實(shí)現(xiàn)類 //在須要注入InjectableService的地方,就會(huì)使用InjectableServiceImpl實(shí)例 bind(InjectableService. class ).to(InjectableServiceImpl. class ); //使HelloService為單例狀態(tài) bind(HelloService. class ).in(Scopes.SINGLETON); } } |
不同的模塊有不同的處理方式,比如樣例中對(duì)于RestModule,加入了一個(gè)Handler:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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 { //注入對(duì)象 @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 ); } //處理綁定路徑的請(qǐng)求訪問 @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); } } |
最后在類路徑根文件夾下加入一個(gè)名為es-plugin.properties屬性文件,指定插件實(shí)現(xiàn)類:
1
|
plugin=org.elasticsearch.plugin.helloworld.HelloWorldPlugin |
二、將插件打成jar包后安裝
如果ES_HOME代表Elasticsearch安裝文件夾。在ES_HOME/plugins文件夾下創(chuàng)建一個(gè)名為HelloWorld的文件夾。該文件夾名稱必須與插件名稱同樣(區(qū)分大寫和小寫),然后將jar包拷貝至HelloWorld文件夾,又一次啟動(dòng)就可以,當(dāng)你運(yùn)行:
curl -GET localhost:9200/hello,就會(huì)返回對(duì)應(yīng)結(jié)果了。
三、為插件加入頁面
假設(shè)你想為你的插件加入訪問頁面。則能夠在ES_HOME/plugins/HelloWorld文件夾下創(chuàng)建一個(gè)名為"_site"的文件夾,該文件夾名稱必須為_site,然后將對(duì)應(yīng)的html頁面放置進(jìn)_site文件夾就可以,假設(shè)放置了一個(gè)名為index.html文件,則能夠通過
localhost:9200/_plugin/HelloWorld/index.html進(jìn)行訪問。
因?yàn)镋lasticsearch提供了jsclientAPI。所以使用html靜態(tài)頁面與js就能夠完畢對(duì)應(yīng)的功能了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。