当前位置: 移动技术网 > IT编程>开发语言>Java > elasticsearch插件开发教程

elasticsearch插件开发教程

2019年07月22日  | 移动技术网IT编程  | 我要评论

检索引擎elasticsearch支持插件模式。有些时候你可能须要安装一些插件。甚至自己开发插件,这里就提供一个開始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<module> modules(settings settings) {
 //创建自己的模块集合
 //假设没有自己定义模块,则能够返回空
 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() {
 //创建自己的服务类集合,服务类须要继承自lifecyclecomponent。es会自己主动创建出服务类实例,并调用其start方法
 //假设没有自己定义服务类。则能够返回空
 collection<class<?
 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.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属性文件,指定插件实现类:

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文件夹就可以,假设放置了一个名为文件,则能够通过

localhost:9200/_plugin/helloworld/进行訪问。
因为elasticsearch提供了jsclientapi。所以使用html静态页面与js就能够完毕对应的功能了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网