*elasticsearch(后面简称es)

  • 背景: 目前项目应用中对es的操作用的是http(自己封装)的一套方法;有些数据处理起来还是需要定制开发处理,不是很方便。正好需要对本项目重新进行改造,于是采用第三方工具包jest 对es的操作进行重新的梳理改造。

  • why use jest

    官方有个大致的介绍:Jest is a Java HTTP Rest client for ElasticSearch.It is actively developed and tested by Searchly.

    jset优势

    1)提供Restful API, 原生ES API不具备;

    2)Jest支持不同版本的es基本操作 主要是http rest client;

  • maven 管理项目:

    <dependency>
    <groupId>io.searchbox</groupId>
    <artifactId>jest</artifactId>
    <version>2.0.3</version>
    </dependency>
  • jest使用

    网上有很多(上述图片中)的方法实例,下面就不具体的介绍了(链接如下)

    http://blog.csdn.net/u010466329/article/details/75020956

    https://github.com/searchbox-io/Jest

    结合源代码封装了一个可以直接传递url的method,该方法的实现就是结合了jest 源码,实现了自己想要的接口(用于用户页面自定义查询 更方便)。 代码如下:

    AbstractLocalHttpAction (本地HttpAction)

       /**
    * methodName "http" "delete" "put" "get" “post”
    * queryParam 参数
    * url 请求链接
    */
    public abstract class AbstractLocalHttpAction <T extends JestResult> extends AbstractAction<T> { protected String methodName; private String queryParam; private String url; public AbstractLocalHttpAction(AbstractLocalHttpAction.Builder builder) {
    super(builder);
    this.queryParam = builder.queryParam;
    this.methodName = builder.methodName;
    this.url = builder.url;
    } public String getMethodName() {
    return this.methodName;
    } public String getQueryParam() {
    return this.queryParam;
    } public String getUrl() {
    return this.url;
    } protected String buildURI() {
    StringBuilder sb = new StringBuilder(super.buildURI()); return sb.toString();
    } protected abstract static class Builder<T extends AbstractLocalHttpAction, K> extends io.searchbox.action.AbstractAction.Builder<T, K> {
    private String url;
    private String methodName;
    private String queryParam; protected Builder() {
    } public K url(String url) {
    this.url = url;
    return (K)this;
    } public K methodName(String methodName) {
    this.methodName = methodName;
    return (K)this;
    } public K queryParam(String queryParam) {
    this.queryParam = queryParam;
    return (K)this;
    } }
    }

LocalResultAbstractAction

        public abstract class LocalResultAbstractAction extends AbstractLocalHttpAction<JestResult> {

        public LocalResultAbstractAction(Builder builder) {
super(builder);
} public JestResult createNewElasticSearchResult(String responseBody, int statusCode, String reasonPhrase, Gson gson) {
return (JestResult)this.createNewElasticSearchResult(new JestResult(gson), responseBody, statusCode, reasonPhrase, gson);
}
}

JestLocalHttpClient(用法build入口)

    public class JestLocalHttpClient extends LocalResultAbstractAction {

    private String query;

    protected JestLocalHttpClient(JestLocalHttpClient.Builder builder) {
super(builder);
this.setURI(this.buildURI()+""+getUrl());
this.query = getQueryParam();
} public String getRestMethodName() {
return getMethodName();
} public String getData(Gson gson) {
String data = this.query;
return data;
}
public static class Builder extends AbstractLocalHttpAction.Builder<JestLocalHttpClient, JestLocalHttpClient.Builder> { public Builder(String url, String methodName,String queryParam) {
this.url(url);
this.methodName(methodName);
this.queryParam(queryParam);
}
public JestLocalHttpClient build() {
return new JestLocalHttpClient(this);
}
} }

jestManager 封装如下

   public class JestManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(JestManager.class);
/**
* 获取JestClient对象
* @return
*/
public static JestClient getJestClient(String clustName) {
JestClientFactory factory = new JestClientFactory();
Cluster cluster = CLUSTERMAP.get(clusterName);
try {
notes.add("http://"+cluster.getHost()+":"+cluster.getHttprt());
HttpClientConfig.Builder httpClientConfig = new HttpClientConfig
.Builder(notes)
.connTimeout(1500)
.readTimeout(3000)
.multiThreaded(true);
factory.setHttpClientConfig(httpClientConfig.build());
}catch (Exception e){
e.printStackTrace();
LOGGER.error("初始化jestclient实例失败:"+e.getMessage());
}
return factory.getObject();
}
}

上面自己的封装方法调用如下

    public JestResult httpProxy(String clustName, String url, String methodName, String queryParam) {
JestResult result = null ;
try {
JestLocalHttpClient jestLocalHttpClient = new JestLocalHttpClient.Builder(url,methodName,queryParam).build();
result = JestManager.getJestClient(clustName).execute(jestLocalHttpClient);
} catch (IOException e) {
e.printStackTrace();
LOGGER.error("jestLocalHttpClient失败:"+e.getStackTrace());
}
return result ;
}

测试如下

url:/demo/_search?pretty
method:get
queryParam:null 调用接口:返回信息
"{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
"

jest 用起来很方便,方法的封装让我们代码写起来更为简单。以上的内容希望能对大家有所帮助。

jest for elasticsearch的更多相关文章

  1. 【Es】jest操作elasticsearch

    https://blog.csdn.net/niuchenliang524/article/details/82869319 操作es的客房端有多个,在此例出三种(具体区别自行百度),本文讲的是jes ...

  2. jest操作 Elasticsearch

    package com.lgmall.search; import com.lgmall.search.esEntity.Article;import com.lgmall.search.esEnti ...

  3. springboot集成elk 四:springboot + Elasticsearch+Jest

    依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spri ...

  4. Spring Boot 整合 elasticsearch

    一.简介 我们的应用经常需要添加检索功能,开源的 ElasticSearch 是目前全文搜索引擎的 首选.他可以快速的存储.搜索和分析海量数据.Spring Boot通过整合Spring Data E ...

  5. SpringBoot笔记十六:ElasticSearch

    目录 ElasticSearch官方文档 ElasticSearch安装 ElasticSearch简介 ElasticSearch操作数据,RESTful风格 存储 检查是否存在 删除 查询 更新 ...

  6. ELK学习笔记之ElasticSearch简介

    0x00 什么是Elasticsearch Elasticsearch (ES)是一个基于 Lucene 的开源搜索引擎,它不但稳定.可靠.快速,而且也具有良好的水平扩展能力,是专门为分布式环境设计的 ...

  7. Elasticsearch Java API深入详解

    0.题记 之前Elasticsearch的应用比较多,但大多集中在关系型.非关系型数据库与Elasticsearch之间的同步.以上内容完成了Elasticsearch所需要的基础数据量的供给.但想要 ...

  8. 3.2_springBoot2.1.x检索之JestClient操作ElasticSearch

    这里介绍Jest方式交互, 导入jest版本 <!--导入jest--> <dependency> <groupId>io.searchbox</groupI ...

  9. spring boot application.properties 属性详解

    2019年3月21日17:09:59 英文原版: https://docs.spring.io/spring-boot/docs/current/reference/html/common-appli ...

随机推荐

  1. 作为新手,SEO要避免的五大误区

    越来越多人在做网站的时候关注的不是网站的界面,而是网站的seo排名.Seo其实没有我们相信中的那么简单,特别对于新手,在实际操作过程中很容易遇到一些误区,今天我们简单说说新手要避免的五大seo误区. ...

  2. Elasticsearch中Head插件的使用

    在学习Elasticsearch的过程中,必不可少需要通过一些工具查看es的运行状态以及数据.如果都是通过rest请求,未免太过麻烦,而且也不够人性化.此时,head可以完美的帮助你快速学习和使用El ...

  3. 程序员的自我救赎---11.3:WinService服务

    <前言> (一) Winner2.0 框架基础分析 (二)PLSQL报表系统 (三)SSO单点登录 (四) 短信中心与消息中心 (五)钱包系统 (六)GPU支付中心 (七)权限系统 (八) ...

  4. javascript设计模式——策略模式

    前面的话 在程序设计中,常常遇到类似的情况,要实现某一个功能有多种方案可以选择.比如一个压缩文件的程序,既可以选择zip算法,也可以选择gzip算法.这些算法灵活多样,而且可以随意互相替换.这种解决方 ...

  5. Sql Server 的服务器类型

    Sql Server 提供了四种服务器类型: 如图所示  : 1, 数据库引擎  2, Analysis Services (分析服务 )  3, Reporting  Services (报告服务) ...

  6. 时间紧任务重---extjs的学习就这么开始吧

    我们的extjs借助了一个模板引擎--artTemplate,它是一个开源的项目,不多说,给个链接吧:http://aui.github.io/artTemplate/ 直接上代码: <!DOC ...

  7. C#中的Explicit和Implicit

    今天在Review一个老项目的时候,看到一段奇怪的代码. if (dto.Payment == null) continue; var entity = entries.FirstOrDefault( ...

  8. Android PopupWindows

    今天了解到PopupWindows这个布局,PopupWindow这个类用来实现一个弹出框,能够使用随意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的. 以下是一个实例 xml ...

  9. 解决EJB本地调用“java.lang.ClassCastException: $Proxy96 cannot be cast to com.tgb.ejb.UserManager”异常

    EJB本地调用方式:把Webclient和EJB服务端部署到同一个JBoss,client和server通过一个JVM进行通信. Web客户端本地调用时.需引用EJB服务端打包的jar,不需引用JBo ...

  10. [Android] AutoCompleteTextView:自己主动完毕输入内容的控件(自己主动补全)

    AutoCompleteTextView是EditText的直接子类,与普通EditText的最大不同就是.在用户输入的过程中,能够列出可供选择的输入项.方便使用者. AutoCompleteText ...