es之java操作插入文档
4方式:
、 使用json字符串直接创建
、 使用Map集合
、 使用第三方库来序列化 createDocumentBySerialize
、 使用内置的帮助器XContentFactory.jsonBuilder()
1: 使用JSON字符串创建
@Test
public void createDocumentByManually(){
String json = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
//IndexRequestBuilder prepareIndex(String index, String type)
final IndexResponse response = this.transportClient.prepareIndex("twitter", "tweet")
.setSource(json, XContentType.JSON).get();
//获取索引
final String _index = response.getIndex();
//获取类型
final String _type = response.getType();
// 文档ID
String _id = response.getId();
// 版本
long _version = response.getVersion();
// 返回的操作状态
RestStatus status = response.status();
System.out.println("索引名称:"+_index+" "+"类型 :" + _type + " 文档ID:"+_id+" 版本 :"+_version+" 返回的操作状态:"+status); }
2:使用Map集合
@Test
public void createDocumentByMap(){
Map<String, Object> json = new HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");
//this.transportClient.prepareIndex 可以传入id
final IndexResponse response = this.transportClient.prepareIndex("twitter", "tweet")
.setSource(json, XContentType.JSON).get();
//获取索引
final String _index = response.getIndex();
//获取类型
final String _type = response.getType();
// 文档ID
String _id = response.getId();
// 版本
long _version = response.getVersion();
// 返回的操作状态
RestStatus status = response.status();
System.out.println("索引名称:"+_index+" "+"类型 :" + _type + " 文档ID:"+_id+" 版本 :"+_version+" 返回的操作状态:"+status);
}
3:使用第三方库来序列化
/**
*这种方式是使用jsckson来序列化一个bean的方式进行操作的
* import com.fasterxml.jackson.databind.*;
* */ @Test
public void createDocumentBySerialize(){ try {
// insstance a json mapper
ObjectMapper mapper = new ObjectMapper(); // create once, reuse
//构造一个类
Person p = new Person();
p.setUser("kimchy");
p.setPostDate(new Date());
p.setMessage("trying out Elasticsearch");
// generate json
byte[] json = mapper.writeValueAsBytes(p);
IndexResponse response = this.client.prepareIndex("twitter3", "tweet")
.setSource(json, XContentType.JSON)
.get();
// 索引名称
String _index = response.getIndex();
// 类型
String _type = response.getType();
// 文档ID
String _id = response.getId();
// 版本
long _version = response.getVersion();
// 返回的操作状态
RestStatus status = response.status();
System.out.println("索引名称:"+_index+" "+"类型 :" + _type + " 文档ID:"+_id+" 版本 :"+_version+" 返回的操作状态:"+status); } catch (JsonProcessingException e) {
e.printStackTrace();
}
}
4:使用内置的帮助器jsonBuilder()
@Test
public void createDocumentByJsonBuilder(){
XContentBuilder builder = null;
try {
builder = jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject();
String json = builder.string();
IndexResponse response = this.client.prepareIndex("twitter4", "tweet")
.setSource(json, XContentType.JSON)
.get();
// 索引名称
String _index = response.getIndex();
// 类型
String _type = response.getType();
// 文档ID
String _id = response.getId();
// 版本
long _version = response.getVersion();
// 返回的操作状态
RestStatus status = response.status();
System.out.println("索引名称:"+_index+" "+"类型 :" + _type + " 文档ID:"+_id+" 版本 :"+_version+" 返回的操作状态:"+status); } catch (IOException e) {
e.printStackTrace();
} }
去elasticsearch的head页面查看:
es之java操作插入文档的更多相关文章
- Java操作Wrod文档的工具类
需要有jacob的jar包支持 import java.util.Iterator; import java.util.List; import java.util.HashMap; import c ...
- Java操作word文档使用JACOB和POI操作word,Excel,PPT需要的jar包
可参考文档: http://wibiline.iteye.com/blog/1725492 下载jar包 http://download.csdn.net/download/javashixiaofe ...
- java操作csv文档通用工具类
https://blog.csdn.net/rodge_rom/article/details/78898015 另: 参考该博主的关于FTP, EXCEL, WORD, 等工具类文章...
- Java文件操作系列[3]——使用jacob操作word文档
Java对word文档的操作需要通过第三方组件实现,例如jacob.iText.POI和java2word等.jacob组件的功能最强大,可以操作word,Excel等格式的文件.该组件调用的的是操作 ...
- ES入门三部曲:索引操作,映射操作,文档操作
ES入门三部曲:索引操作,映射操作,文档操作 一.索引操作 1.创建索引库 #语法 PUT /索引名称 { "settings": { "属性名": " ...
- C#操作Word文档(加密、解密、对应书签插入分页符)
原文:C#操作Word文档(加密.解密.对应书签插入分页符) 最近做一个项目,客户要求对已经生成好的RTF文件中的内容进行分页显示,由于之前对这方面没有什么了解,后来在网上也找了相关的资料,并结合自己 ...
- iText操作word文档总结
操作word文档的工具有很多,除了iText之外还有POI,但是POI擅长的功能是操作excel,虽然也可以操作word,但是能力有限,而且还有很多的bug,技术并不成熟,下面就重点介绍一种操作wor ...
- 整理关于Java进行word文档的数据动态数据填充
首先我们看下,别人整理的关于Java生成doc 的 资料. java生成word的几种方案 1. Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用 ...
- Elasticsearch操作Document文档
1.利用客户端操作Document文档数据 1.1 创建一个文档(创建数据的过程,向表中去添加数据) 请求方式:Post 请求地址:es所在IP:9200/索 ...
随机推荐
- phpstudy添加PHP
想在phpstudy2018里面增加一个php版本,操作如下: 一.下载php-7.2.19-ts文件,解压缩,放在相应的目录下: 二.修改Apache的配置文件1.修改httpd.conf 配置,D ...
- AcWing 92. 递归实现指数型枚举
题目链接:https://www.acwing.com/problem/content/description/94/ 题意:从 n 个数中选取数字,输出所有的选取可能 idea:枚举所有取数可能,就 ...
- jsoncpp解析
讲jsoncpp解析json的文章,很不错,可以参考: http://blog.csdn.net/hzyong_c/article/details/7163589 http://www.cnblogs ...
- offsetWidth clientWidth scrollWidth 的区别
了解 offsetWidth clientWidth scrollWidth 的区别 最近需要清除区分开元素的width,height及相应的坐标等,当前这篇用来区分offsetWidth clien ...
- json与导入模块目录
import json """主要用于不同语言的数据公用 """ info = {"a":1,"b" ...
- 2019 安洵杯 Re 部分WP
0x01.EasyEncryption 测试文件:https://www.lanzous.com/i7soysb 1.IDA打开 int sub_416560() { int v0; // eax i ...
- Python webdriver调用Chrome报错
报错信息如下: selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to b ...
- Ubuntu下通过apache建立虚拟主机
一个搞前端交互的,总会遇到这样那样的,不需要写代码去解决的问题,怎么搞?答:只能去大海里捞,问题很明确但答案不一定靠谱,因为回答的人不用去考虑你是否会给自己系统搞崩溃. 那么我只能把自己经过验证的答案 ...
- console 对象
JavaScript 原生中默认是没有 Console 对象,这是宿主对象(也就是游览器)提供的内置对象. 用于访问调试控制台,在不同的浏览器里效果可能不同.Console 对象方法:
- Hive常用数据库操作
1.创建表的三种姿势 第一种 //员工表 create table if not exists default.emp( empno int, ename string, job string, mg ...