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'; 链接地址>>
随机推荐
- openpyxl 写入字典
def write(self,data_path, sheetname,value): index = len(value) workbook = openpyxl.Workbook() sheet ...
- Vulnhub-sundown
总结:该靶机是一个wordpress管理系统,需要信息收集得到插件信息,然后搜索插件漏洞,得到一个文件包含exp,利用其得到一个普通用户,利用hydra爆破密码然后ssh连接,信息收集得到一个数据库配 ...
- go 逐行读取文件
前言 文件 I/O,特别是对文件的读写是编程语言中重要的功能.通常,我们需要逐行读取文件. GO 提供了 bufio 软件包,实现了有缓冲的 I/O.它包装一个 io.Reader 或 io.Writ ...
- go 限流器 rate
前言 Golang 官方提供的扩展库里就自带了限流算法的实现,即 golang.org/x/time/rate.该限流器也是基于 Token Bucket(令牌桶) 实现的. 限流器的内部结构 tim ...
- SpringBoot+微信支付-JSAPI
引入微信支付SDK Maven: com.github.wechatpay-apiv3:wechatpay-java-core:0.2.12 Maven: com.github.wechatpay-a ...
- Linux 系统出现异常排查思路
16 系统出现异常排查思路16.1 查看用户信息16.1.1查看当前的用户# who 04:39:39 up 1:30, 1 user, load average: 0.01, 0.01, 0. ...
- WIN2012域用户添加和批量添加工具
WIN2012域用户添加和批量添加,不需要进行复杂的进电脑管理去添加 直接在软件上就可单个用户添加,可批量添加,并把指定的用户加入组 可以自定义组织单位,使用起来比较简单方便. 链接:https:// ...
- 一文彻底搞清楚ArkUI
程序员Feri一名12年+的程序员,做过开发带过团队创过业,擅长Java相关开发.鸿蒙开发.人工智能等,专注于程序员搞钱那点儿事,希望在搞钱的路上有你相伴!君志所向,一往无前! 0.前言 在移动开发领 ...
- Visual Studio 自定义项目模版
以 Visual Studio 2017 为例. 在 Visual Studio 中用户项目模版就是我们俗称的自定义项目模版. 用户项目模版位置 在Visual Studio中打开[工具-选项-项目和 ...
- Lua中获取第二天凌晨的剩余时间
在时间这个问题上,lua提供两大方法来供开发者使用,一个是os.time(),一个是os.date(),这两大方法可以满足日常开发的需求. 那么我们如何准确运用这两大方法呢. 在这一文章中我们先讲os ...