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

localhost:9200/_plugin/HelloWorld/index.html进行訪问。

因为Elasticsearch提供了jsclientAPI。所以使用html静态页面与js就能够完毕对应的功能了。

elasticsearch插件开发的更多相关文章

  1. Elasticsearch( 插件开发)

    elasticsearch5.2.2 插件开发(一) Scripting plugins:这个插件本质来说,就是会调用用户的脚本,所以可以执行任何的程序,举例的话,可以通过这个插件,支持javascr ...

  2. 云计算平台(检索篇)-Elasticsearch

    前段时间为公司基于Elasticsearch(下面简称ES)做了一套检索平台,下面将这段时间积累的一些知识与大家分享,如有不对之处,欢迎大家多多批评与建议.针对Elasticsearch由于东西还是比 ...

  3. Elasticsearch 1.X 版本Java插件开发

    接上一篇<Elasticsearch 2.X 版本Java插件开发简述> 开发1.X版本elasticsearch java插件与2.X版本有一些不同,同时在安装部署上也有些不同,主要区别 ...

  4. Elasticsearch 2.X 版本Java插件开发简述

    1:elasticsearch插件分类简述 2:Java插件开发要点 3:如何针对不同版本elasticsearch提供多版本的插件 4:插件具有外部依赖时遇到的一些问题(2016-09-07更新) ...

  5. elasticsearch 自定义similarity 插件开发

    转自:http://www.chepoo.com/elasticsearch-similarity-custom-plug-in-development.html 在搜索开发中,我们要修改打分机制,就 ...

  6. elasticsearch源码分析及插件开发

    ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...

  7. HanLP Analysis for Elasticsearch

    基于 HanLP 的 Elasticsearch 中文分词插件,核心功能: 兼容 ES 5.x-7.x: 内置词典,无需额外配置即可使用: 支持用户自定义词典: 支持远程词典热更新(待开发): 内置多 ...

  8. Elasticsearch5.5.1插件开发指南

    Elasticsearch5.5.1插件开发指南 原文地址: https://www.elastic.co/guide/en/elasticsearch/plugins/5.5/plugin-auth ...

  9. ELK(Logstash+Elasticsearch+Kibana)的原理和详细搭建

    一. Elastic Stack Elastic Stack是ELK的官方称呼,网址:https://www.elastic.co/cn/products ,其作用是“构建在开源基础之上, Elast ...

随机推荐

  1. gitlab克隆报错:remote: HTTP Basic: Access denied;remote: You must use a personal access token with ‘api’ scope for Git over HTTP.

    错误: remote: HTTP Basic: Access denied remote: You must use a personal access token with ‘api’ scope ...

  2. T - Amusing Joke(map)

    Problem description So, the New Year holidays are over. Santa Claus and his colleagues can take a re ...

  3. php登陆和注册

    注册界面 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  4. Percona Xtrabackup备份及恢复

    1. http://www.percona.com/software/percona-xtrabackup下载并安装 2. 全量备份  a.全量备份到制定目录            innobacku ...

  5. 深圳面试一周记录——.NET(B/S)开发

    个人简单信息:2011年毕业,最高学历大专,最近一份工作在广州:有做架构设计经验,有一年的带团队(10人左右)经验:互联网和行业软件公司都待过. 为免不必要的争论,本文说地址的就不说公司行业,说公司行 ...

  6. C# 去掉代码前边空格(格式化代码)

    private void button1_Click(object sender, EventArgs e) { textBox2.Text = ""; string str = ...

  7. python排序sorted与sort比较 (转)

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. sorted(iterable,key=None,revers ...

  8. vue-awesome-swiper组件的使用

    一.轮播图组件是这样安装的 npm i --save-dev vue-awesome-swiper main.js里面 import 'swiper/dist/css/swiper.css' impo ...

  9. Swoole server函数列表(转载)

    swoole_server::__construct swoole_server::set swoole_server::on swoole_server::addlistener swoole_se ...

  10. SDWC2017游记

    果然我还是那么弱啊.——$Mingqi_H.$ Day -1 下午五点半回家.然而并没有什么事情可做.依旧是下载$Magical\,Mirai$,找一个黄油存起来. emmm...本来是打算去开发区那 ...