elasticsearch插件开发教程

网友投稿 343 2023-05-04


elasticsearch插件开发教程

检索引擎Elasticsearch支持插件模式。有些时候你可能须要安装一些插件。甚至自己开发插件http://,这里就提供一个開始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 modules(Settings settings) {

//创建自己的模块集合

//假设没有自己定义模块,则能够返回空

HelloModule helloModule = new HelloModule();

ArrayList list = new ArrayList<>();

list.add(helloModule);

Collections.unmodifiableList(list);

return list;

}

@SuppressWarnings("rawtypes")

@Override

public Collection> services() {

//创建自己的服务类集合,服务类须要继承自LifecycleComponent。ES会自己主动创建出服务类实例,并调用其start方法

//假设没有自己定义服务类。则能够返回空

Collection

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.elasticsearcUqCsgh.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就能够完毕对应的功能了。

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.elasticsearcUqCsgh.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就能够完毕对应的功能了。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解vue数据渲染出现闪烁问题
下一篇:详解基于spring多数据源动态调用及其事务处理
相关文章

 发表评论

暂时没有评论,来抢沙发吧~