一、索引操作

//创建索引public static void main(String[] args) throws IOException {// 1. 创建 ES 连接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 获取 ES 连接JestClient jestClient = jestClientFactory.getObject();// 4. 创建索引CreateIndex createIndex = new CreateIndex.Builder("my_index").build();JestResult result = jestClient.execute(createIndex);// 5. 输出创建结果System.out.println(result.getJsonString());}
//删除索引public static void main(String[] args) throws IOException {// 1. 创建 ES 连接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 获取 ES 连接JestClient jestClient = jestClientFactory.getObject();// 4. 删除索引DeleteIndex deleteIndex = new DeleteIndex.Builder("my_index").build();JestResult result = jestClient.execute(deleteIndex);// 5. 输出创建结果System.out.println(result.getJsonString());}
//设置Mappingpublic static void main(String[] args) throws IOException {// 1. 创建 ES 连接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 获取 ES 连接JestClient jestClient = jestClientFactory.getObject();// 4. 创建 json 格式的 mapping/*** {*     "mappings":{*         "properties":{*             "field1":{*                 "type":"keyword"*             },*             "field2":{*                 "type":"byte"*             }*         }*     }* }*/Map<String, Object> map = new HashMap<String, Object>() {{this.put("mappings", new HashMap<String, Object>() {{this.put("properties", new HashMap<String, Object>() {{this.put("name", new HashMap<String, String>() {{//this.put("type", "keyword");}});this.put("age", new HashMap<String, String>() {{//this.put("type", "integer");}});}});}});}};String mapping = JSONObject.toJSONString(map);// 5. 创建索引// PutMapping putMapping = new PutMapping.Builder(indexName, indexType, indexMapping).build();CreateIndex createIndex = new CreateIndex.Builder("my_index").settings(mapping).build();JestResult result = jestClient.execute(createIndex);// 6. 输出创建结果System.out.println(result.getJsonString());}
//获取Mappingpublic static void main(String[] args) throws IOException {// 1. 创建 ES 连接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 获取 ES 连接JestClient jestClient = jestClientFactory.getObject();// 4. 获取 mappingGetMapping getMapping = new GetMapping.Builder().addIndex("my_index").build();JestResult result = jestClient.execute(getMapping);// 6. 输出获取结果System.out.println(result.getJsonString());}

二、文档操作

public class Book {@JestId // 自动生成 id,private String id;private String name;private String author;private String desc;public Book(String id, String name, String author, String desc) {this.id = id;this.name = name;this.author = author;this.desc = desc;}
}
//新增/修改文档public static void main(String[] args) throws IOException {// 1. 创建 ES 连接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 获取 ES 连接JestClient jestClient = jestClientFactory.getObject();// 4. 准备数据Book book = new Book("001", "斗破苍穹", "天蚕土豆", "这是斗气的世界");// 4. 首先会判断索引是否存在,不存在则根据文档创建索引,然后判断 id 是否存在,存在就是更新文档Index index = new Index.Builder(book).index("my_index").type("_doc").build();JestResult result = jestClient.execute(index);// 5. 输出创建结果System.out.println(result.getJsonString());}
//批量新增public static void main(String[] args) throws IOException {// 1. 创建 ES 连接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 获取 ES 连接JestClient jestClient = jestClientFactory.getObject();// 4. 批量创建文档Bulk.Builder bulk = new Bulk.Builder().defaultIndex("my_index").defaultType("_doc");List<Book> list = new ArrayList<Book>() {{this.add(new Book("001", "斗破苍穹", "天蚕土豆", "这是斗气的世界"));this.add(new Book("002", "完美世界", "辰东", "遮天前传"));this.add(new Book("003", "盘龙", "我吃西红柿", "神奇的戒指"));this.add(new Book("004", "诛仙", "萧鼎", "天地不仁,以万物为刍狗"));this.add(new Book("005", "大主宰", "天蚕土豆", "大千世界,位面交汇"));}};for (Book book : list) {// 如未设置唯一 id 值,则唯一 id 会默认生成,索引操作为添加操作Index index = new Index.Builder(book).build();bulk.addAction(index);}BulkResult result = jestClient.execute(bulk.build());System.out.println(result.getJsonString());}
//根据id查询文档public static void main(String[] args) throws IOException {// 1. 创建 ES 连接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 获取 ES 连接JestClient jestClient = jestClientFactory.getObject();// 4. 查询文档Get get = new Get.Builder("my_index", "001").type("_doc").build();JestResult result = jestClient.execute(get);// 5. 输出查询结果System.out.println(result.getJsonString());}
//根据条件查询文档public static void main(String[] args) throws IOException {// 1. 创建 ES 连接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 获取 ES 连接JestClient jestClient = jestClientFactory.getObject();// 4. 查询文档// 4.1 构造查询条件// 4.1.1 单 field 不分词查询// TermQueryBuilder termQueryBuilder = new TermQueryBuilder(fieldName, value);// 4.1.2 单 field 多词不分词查询// TermsQueryBuilder termsQueryBuilder = new TermsQueryBuilder("", "value1", "value2");// 4.1.3 单 field 分词查询// MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder(fieldName, value);// 4.1.4 多 field 分词查询MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder("斗气", "desc");// 转化为 ES 查询,默认分词后 and 连接,可使用 defaultOperator(Operator.AND) 修改SearchSourceBuilder query = new SearchSourceBuilder().query(multiMatchQueryBuilder);// 4.2 构造查询Search search = new Search.Builder(query.toString()).build();// 4.3 执行查询JestResult result = jestClient.execute(search);// 5. 输出查询结果System.out.println(result.getJsonString());}
//区间搜索public static void main(String[] args) throws IOException {// 1. 创建 ES 连接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 获取 ES 连接JestClient jestClient = jestClientFactory.getObject();// 4.1 构建区间搜索条件RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("id").gt("002").lte("003");// 4.2 解析为 ES 查询SearchSourceBuilder query = new SearchSourceBuilder().query(rangeQueryBuilder);// 4.3 构建查询Search search = new Search.Builder(query.toString()).build();// 4.4 执行查询JestResult result = jestClient.execute(search);// 5. 输出查询结果System.out.println(result.getJsonString());}
//删除文档public static void main(String[] args) throws IOException {// 1. 创建 ES 连接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 获取 ES 连接JestClient jestClient = jestClientFactory.getObject();// 4. 删除文档Delete delete = new Delete.Builder("001").index("my_index").type("_doc").build();JestResult result = jestClient.execute(delete);// 5. 输出删除结果System.out.println(result.getJsonString());}
//分页public static void main(String[] args) throws IOException {// 1. 创建 ES 连接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 获取 ES 连接JestClient jestClient = jestClientFactory.getObject();// 4.1 构建区间搜索条件RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("id").gt("002").lte("003");// 4.2 解析为 ES 查询并添加分页SearchSourceBuilder query = new SearchSourceBuilder().query(rangeQueryBuilder).from(0).size(1);// 4.3 构建查询Search search = new Search.Builder(query.toString()).build();// 4.4 执行查询JestResult result = jestClient.execute(search);// 5. 输出查询结果System.out.println(result.getJsonString());}
//高亮public static void main(String[] args) throws IOException {// 1. 创建 ES 连接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 获取 ES 连接JestClient jestClient = jestClientFactory.getObject();// 4.1 构建搜索条件MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder("天世界", "author", "desc");// 4.2 转化为 ES 搜索SearchSourceBuilder query = new SearchSourceBuilder().query(multiMatchQueryBuilder);// 4.3 配置高亮HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.field("author");highlightBuilder.field("desc");highlightBuilder.preTags("<em>").postTags("</em>");highlightBuilder.fragmentSize(500);query.highlighter(highlightBuilder);// 4.4 构建查询Search search = new Search.Builder(query.toString()).build();// 4.5 执行查询, 使用 SearchResult 接收SearchResult result = jestClient.execute(search);// 5.1 遍历查询结果List<SearchResult.Hit<Book, Void>> hits = result.getHits(Book.class);for (SearchResult.Hit<Book, Void> hit : hits) {// 5.2 获取高亮集合Map<String, List<String>> highlight = hit.highlight;for (String s : highlight.keySet()) {// 5.3 输出高亮结果System.out.println("高亮显示:" + highlight.get(s).get(0));}}}

.三、导入包及pom文件引包

import com.alibaba.fastjson.JSONObject;
import com.sdcqjy.cloud.common.util.JsonUtils;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Bulk;
import io.searchbox.core.BulkResult;
import io.searchbox.core.Delete;
import io.searchbox.core.Get;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
import io.searchbox.indices.mapping.GetMapping;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;<dependency>                                                                                                                                                                                             <groupId>io.searchbox</groupId>                                                                                                                                                                                             <artifactId>jest</artifactId><version>6.3.1</version>                                                                                                                                                                                               </dependency> 

Elasticsearch JestClient 使用相关推荐

  1. elasticsearch新增_SpringBoot 使用JestClient操作Elasticsearch

    1.Jest介绍 操作Elasticsearch的客户端有很多,SpringBoot也提供了方式去操作,这里介绍另外一种方式去使用Elasticsearch --- JestClient JestCl ...

  2. JestClient - Elasticsearch 基本操作汇总

    目录 Elasticsearch 介绍 搭建 Elasticsearch 集群 Spring Boot 集成 Elasticsearch JestClient 介绍 JestClient 操作 ESQ ...

  3. 你知道什么是Jest吗?

    点击蓝色"程序猿DD"关注我哟 加个"星标",不忘签到哦 关注我,回复口令获取可获取独家整理的学习资料: - 001:领取<Spring Boot基础教程 ...

  4. 使用JestClient操作ElasticSearch

    可参考:  https://www.blog-china.cn/template/documentHtml/1484101683485.html https://github.com/searchbo ...

  5. jest java_使用JestClient操作ElasticSearch的简单demo

    elasticsearch rest api 学习记录 elasticsearch版本:1.4.1 学习记录 学习的博客社区 集群健康查看 epoch timestamp cluster status ...

  6. Java使用JestClient操作ElasticSearch

    个人使用Java操作Elasticsearch的记录,综合网络上很多的片段,自己进行修改后的,亲测可以使用,故上传做个备份. Java操作代码: package cn.xgs.JestClient;i ...

  7. es springboot 不设置id_原创 | 一篇解决Springboot 整合 Elasticsearch

    ElasticSearch 结合业务的场景,在目前的商品体系需要构建搜索服务,主要是为了提供用户更丰富的检索场景以及高速,实时及性能稳定的搜索服务. ElasticSearch是一个基于Lucene的 ...

  8. 聊聊springboot elasticsearch healthIndicator

    序 本文主要研究一下springboot elasticsearch healthIndicator ElasticsearchHealthIndicatorProperties spring-boo ...

  9. 使用Jest操作ElasticSearch 报错:No time zone indicator问题的解决方案

    问题描述: 在ElasticSearch中的时间格式是 yyyy-MM-dd'T'HH:mm:ss 的格式的,但是在使用Jest查询结果时 执行jestClient.execute(search) , ...

最新文章

  1. 32位x86处理器架构
  2. 图说开源许可协议:GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
  3. Java多线程:线程安全和非线程安全的集合对象
  4. matlab fft2怎么移动频率对称,fft2 二维快速傅里叶变换(Matlab)
  5. C# 设置Excel中的数字字符串格式
  6. 使用k-近邻算法进行分类
  7. 在Docker上运行Asp.Net Core示例网站
  8. aba问题实际中有什么影响_实际影响是什么?
  9. SpringMVC Hello World
  10. java 判断是否为车牌_java车牌检测识别库
  11. androidx.preference.PreferenceScreen 去除左边空白
  12. 《王亡於江上》之周昭王姬瑕
  13. 在系统中增加各银行卡刷卡消费分析
  14. 句法分析——CYK分析算法
  15. 介绍中国传统节日的网页html,关于中国传统节日介绍的作文
  16. 焦虑症和抑郁症有什么区别吗?
  17. 打开文件位置在计算机游戏里面,Win7电脑“打开或关闭windows功能”里面没有游戏文件夹的修复方法...
  18. 路由器DMZ简单解说
  19. python判断火车票座位号分布图_怎么从火车票座位号看自己是不是靠窗的位置
  20. 【项目总结】之——JS分割字符串

热门文章

  1. 使用HttpClient的时候报错java.io.IOException: Attempted read from closed stream
  2. 郭店楚简——原简整理,文物出版社
  3. MAC版本subline text快捷键大全
  4. SAP中销售处理到期发票清单VF04功能的应用
  5. @ds实现多数据源切换及解决事务失效问题
  6. 看过无数Java GC文章,这5个问题你也未必知道!
  7. 今天开始学Java 如果统计的个数相同,则按照ASII码由小到大排序输出 。
  8. java后台导出pdf,基础用法和样例
  9. 鲁大师便捷查看硬盘参数
  10. vue中控制浏览器滚动