elasticsearch RestHighLevelClient 关于document的常用操作 ---------- 新增篇
es中的新增操作分类两大类:普通新增和批量新增
普通新增使用IndexRequest即可
批量新增使用BulkRequest通过循环的方式将数据进行统一装载最后执行bulk操作
普通新增:
//es单条操作--添加文档(记录)
public String addEsDocument() throws IOException { //开始设置属性
Elasticsearch elasticsearch = new Elasticsearch();
elasticsearch.setUserName("张三");
elasticsearch.setAge(161);
elasticsearch.setSex("男");
elasticsearch.setLastName("sss");
elasticsearch.setHeight(175);
elasticsearch.setWeight(160);
elasticsearch.setEducation("硕士");
elasticsearch.setTotalScore(205.36); //创建新增文档的请求对象
IndexRequest indexRequest = new IndexRequest(); //指定对那个索引进行操作
// indexRequest.index("user").id("1002");//此处不建议指定id值,可使用系统提供的全局uuid
indexRequest.index("user"); //将设置好的属性值转为json格式
ObjectMapper objectMapper = new ObjectMapper();
String elasticsearchJson = objectMapper.writeValueAsString(elasticsearch); //以json格式添加文档信息
indexRequest.source(elasticsearchJson , XContentType.JSON); //像es服务发送请求
IndexResponse esAddInfo = this.client.index(indexRequest, RequestOptions.DEFAULT); System.out.println("es index _index is : " + esAddInfo.getIndex());//获取当前索引
System.out.println("es index _id is : " + esAddInfo.getId());//获取生成的ID
System.out.println("es index _result is : " + esAddInfo.getResult());//获取操作结果 return "add es document complete";
}
批量新增:
//批量插入
public String bulkAddEsDocument(){ //获取数据源
List<Map<String , Object>> testData = getTestData(); //创建批量新增文档请求
BulkRequest bulkRequest = new BulkRequest(); //批量为属性赋值
int size = testData.size();
for (int i=0; i<size; i++) {
bulkRequest.add(new IndexRequest().index("user").source(testData.get(i)));
} //发送批量新新增请求
try {
BulkResponse bulk = this.client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.status());
} catch (IOException e) {
e.printStackTrace();
} return "bulk add es document complete";
} //批量插入的数据源
private static List<Map<String , Object>> getTestData(){ HashMap<String, Object> map1 = new HashMap<>();
map1.put("userName" , "marray");
map1.put("sex" , "男");
map1.put("age" , 26);
map1.put("lastName" , "aa");
map1.put("height" , 180);
map1.put("weight" , 120);
map1.put("education" , "本科");
map1.put("totalScore" , 269.36); HashMap<String, Object> map2 = new HashMap<>();
map2.put("userName" , "Tom");
map2.put("sex" , "男");
map2.put("age" , 17);
map2.put("lastName" , "bb");
map2.put("height" , 190);
map2.put("weight" , 230);
map2.put("education" , "本科");
map2.put("totalScore" , 278.21); HashMap<String, Object> map3 = new HashMap<>();
map3.put("userName" , "Jerry");
map3.put("sex" , "女");
map3.put("age" , 19);
map3.put("lastName" , "cc");
map3.put("height" , 165);
map3.put("weight" , 80);
map3.put("education" , "小学");
map3.put("totalScore" , 2693.02); HashMap<String, Object> map4 = new HashMap<>();
map4.put("userName" , "Tank");
map4.put("sex" , "女");
map4.put("age" , 23);
map4.put("lastName" , "dd");
map4.put("height" , 123);
map4.put("weight" , 11);
map4.put("education" , "本科");
map4.put("totalScore" , 222.22); HashMap<String, Object> map5 = new HashMap<>();
map5.put("userName" , "张三");
map5.put("sex" , "男");
map5.put("age" , 43);
map5.put("lastName" , "ee");
map5.put("height" , 198);
map5.put("weight" , 280);
map5.put("education" , "高中");
map5.put("totalScore" , 36.25); HashMap<String, Object> map6 = new HashMap<>();
map6.put("userName" , "李四");
map6.put("sex" , "男");
map6.put("age" , 25);
map6.put("lastName" , "ff");
map6.put("height" , 169);
map6.put("weight" , 110);
map6.put("education" , "本科");
map6.put("totalScore" , 85.36); List<Map<String , Object>> objects = new ArrayList<>();
objects.add(map1);
objects.add(map2);
objects.add(map3);
objects.add(map4);
objects.add(map5);
objects.add(map6); return objects;
}
特殊处理
针对map数据类型的新增与其他新增无区别也需要依托实体类来进行操作
public String addEsDocument() throws IOException { //开始设置属性
Map map = new HashMap();
map.put("语文" , "11");
map.put("数学" , "22");
map.put("地理" , "33");
map.put("生物" , "44");
map.put("化学" , "55");
map.put("历史" , "66");
Person person = new Person("王五" , 21 , map); //创建新增文档的请求对象
IndexRequest indexRequest = new IndexRequest(); //指定对那个索引进行操作
// indexRequest.index("user").id("1002");//此处不建议指定id值,可使用系统提供的全局uuid
indexRequest.index("map_test"); //将设置好的属性值转为json格式
ObjectMapper objectMapper = new ObjectMapper();
String elasticsearchJson = objectMapper.writeValueAsString(person); //以json格式添加文档信息
indexRequest.source(elasticsearchJson , XContentType.JSON); //像es服务发送请求
IndexResponse esAddInfo = this.client.index(indexRequest, RequestOptions.DEFAULT); System.out.println("es index _index is : " + esAddInfo.getIndex());//获取当前索引
System.out.println("es index _id is : " + esAddInfo.getId());//获取生成的ID
System.out.println("es index _result is : " + esAddInfo.getResult());//获取操作结果 return "add es document complete";
}
elasticsearch RestHighLevelClient 关于document的常用操作 ---------- 新增篇的更多相关文章
- Elasticsearch本地环境安装和常用操作
本篇文章首发于我的头条号Elasticsearch本地环境安装和常用操作,欢迎关注我的头条号和微信公众号"大数据技术和人工智能"(微信搜索bigdata_ai_tech)获取更多干 ...
- 数据结构之链表-链表实现及常用操作(C++篇)
数据结构之链表-链表实现及常用操作(C++篇) 0.摘要 定义 插入节点(单向链表) 删除节点(单向链表) 反向遍历链表 找出中间节点 找出倒数第k个节点 翻转链表 判断两个链表是否相交,并返回相交点 ...
- Pandas常用操作 - 新增数据列
初始化测试数据 df = pd.DataFrame({'stu_name': ['Nancy', 'Tony', 'Tim', 'Jack', 'Lucy'], 'stu_age': [17, 16, ...
- ElasticSearch之映射常用操作
本文案例操作,建议先阅读我之前的文章<ElasticSearch之安装及基本操作API> Mapping (映射)类似关系型数据库中的表的结构定义.我们将数据以 JSON 格式存入到 El ...
- Elasticsearch(ES)API 增删查改常用操作
常用操作 查询所有数据 POST http://192.168.97.173:27009/logstash_test_2018/doc/_search { "query": { & ...
- elasticsearch RestHighLevelClient 使用方法及封装工具
目录 EsClientRHL 更新日志 开发原因: 使用前你应该具有哪些技能 工具功能范围介绍 工具源码结构介绍 开始使用 未来规划 git地址:https://gitee.com/zxporz/ES ...
- Elasticsearch学习系列二(基础操作)
本文将分为3块讲解Es的基础操作.分别为:索引(index).映射(mapping).文档(document). 索引操作 创建索引库 语法: PUT /索引名称{ "settings&qu ...
- DBA必备:MySQL数据库常用操作和技巧
DBA必备:MySQL数据库常用操作和技巧 2011-02-25 15:31 kaduo it168 字号:T | T MySQL数据库可以说是DBA们最常见和常用的数据库之一,为了方便大家使用,老M ...
- DataGridView常用操作
一.DataGridView列右击菜单事件处理 (1). 添加一个快捷菜单contextMenuStrip1:(2). 给dataGridView1的CellMouseDown事件添加处理程序: pr ...
- 【3】Chrome 的一些常用操作
记录一些 Chrome 的常用操作 1. 让页面可以编辑 1). 在 控制台 输入 document.designMode = 'on'; 链接地址>>
随机推荐
- 自动化-Yaml文件读取函数封装
1.文件布局 打开文件修改读取方式为w load函数加载文件 class ReadConfiYaml: def __init__(self,yaml_file): self.yaml_file=yam ...
- FastAPI 错误处理与自定义错误消息完全指南:构建健壮的 API 应用 🛠️
title: FastAPI 错误处理与自定义错误消息完全指南:构建健壮的 API 应用 ️ date: 2025/3/12 updated: 2025/3/12 author: cmdragon e ...
- Netty基础—3.基础网络协议
大纲 1.网络基础的相关问题总结 2.七层模型和四层模型 3.物理层(网线 + 光缆 + 01电信号) 4.数据链路层(以太网协议 + 网卡mac地址) 5.网络层(IP协议 + 子网划分 + 路由器 ...
- ubuntu 刷新 hosts 命令
systemd-resolved 服务 sudo systemctl restart systemd-resolved 这个命令将重启 systemd-resolved 服务,该服务负责 DNS 解析 ...
- go 结构体根据某个字段进行排序
前言 在任何编程语言中,关乎到数据的排序都会有对应的策略,我们来看下 Golang 是怎样对数据进行排序,以及我们如何优化处理使用 go 排序 go 可以针对任何对象排序,虽然很多情况下是一个 sli ...
- JDK各个版本发布时间和版本名称
版权 版本 名称 发行日期 JDK 1.0 Oak(橡树) 1996-01-23 JDK 1.1 1997-02-19 JDK 1.1.4 Sparkler(宝石) 1997-09-12 JDK ...
- oracle调整sga、pga大小
展开修改sga大小1-1查看当前sga大小SQL> show parameter sga1-2修改sga_max_size为24GSQL> alter system set sga_max ...
- BandiZip无广告版安装
BandiZip无广告版安装 Bandizip 是一款压缩软件,它支持Zip.7-Zip 和 RAR 以及其它压缩格式.它拥有非常快速的压缩和解压缩的算法,从大学用到现在,但是现在最新的版本在每次压缩 ...
- “你觉得客户需要”是杀死TA的最后一根稻草 | IPD集成产品开发
这个米老鼠洗衣机,大家眼熟吗? 相信最近热衷于在网上冲浪的朋友们,对这款形似米老鼠的"懒人洗衣机"并不陌生,甚至算是小小地参与了一下这个产品研发项目.在海尔的周云杰总裁爆火出圈后, ...
- emmy断点调试
package.cpath = package.cpath .. ';C:/Users/Administrator/AppData/Roaming/JetBrains/IntelliJIdea2021 ...