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操作插入文档的更多相关文章

  1. Java操作Wrod文档的工具类

    需要有jacob的jar包支持 import java.util.Iterator; import java.util.List; import java.util.HashMap; import c ...

  2. Java操作word文档使用JACOB和POI操作word,Excel,PPT需要的jar包

    可参考文档: http://wibiline.iteye.com/blog/1725492 下载jar包 http://download.csdn.net/download/javashixiaofe ...

  3. java操作csv文档通用工具类

    https://blog.csdn.net/rodge_rom/article/details/78898015 另: 参考该博主的关于FTP, EXCEL, WORD, 等工具类文章...

  4. Java文件操作系列[3]——使用jacob操作word文档

    Java对word文档的操作需要通过第三方组件实现,例如jacob.iText.POI和java2word等.jacob组件的功能最强大,可以操作word,Excel等格式的文件.该组件调用的的是操作 ...

  5. ES入门三部曲:索引操作,映射操作,文档操作

    ES入门三部曲:索引操作,映射操作,文档操作 一.索引操作 1.创建索引库 #语法 PUT /索引名称 { "settings": { "属性名": " ...

  6. C#操作Word文档(加密、解密、对应书签插入分页符)

    原文:C#操作Word文档(加密.解密.对应书签插入分页符) 最近做一个项目,客户要求对已经生成好的RTF文件中的内容进行分页显示,由于之前对这方面没有什么了解,后来在网上也找了相关的资料,并结合自己 ...

  7. iText操作word文档总结

    操作word文档的工具有很多,除了iText之外还有POI,但是POI擅长的功能是操作excel,虽然也可以操作word,但是能力有限,而且还有很多的bug,技术并不成熟,下面就重点介绍一种操作wor ...

  8. 整理关于Java进行word文档的数据动态数据填充

    首先我们看下,别人整理的关于Java生成doc 的 资料. java生成word的几种方案 1. Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用 ...

  9. Elasticsearch操作Document文档

    1.利用客户端操作Document文档数据        1.1 创建一个文档(创建数据的过程,向表中去添加数据)            请求方式:Post    请求地址:es所在IP:9200/索 ...

随机推荐

  1. for循环、列表的切片、元组

    一.遍历整个列表 使用for语句循环将列表每取出一个变量,然后存储在中间变量中,打印中间变量:循环取出: 1.简单for循环 示例: carssa = ['richan','fengtian','be ...

  2. git自动上传脚本及基本代码

    git_auto.bat git add . git add -A git add -u git commit -m "text" git pull --rebase origin ...

  3. mysql 恢复数据时中文乱码

    mysql恢复数据时中文乱码,解决办法. 用source命令导入mysql数据库怎么设置中文编码 1.导出数据时指定编码在导出mysql sql执行文件的时候,指定一下编码格式: mysqldump ...

  4. [转帖]linux screen 命令详解,xshell关掉窗口或者断开连接,查看断开前执行的命令

    linux screen 命令详解,xshell关掉窗口或者断开连接,查看断开前执行的命令 https://binwaer.com/post/12.html yun install -y screen ...

  5. shell学习笔记2---执行Shell脚本(多种方法)

    在新进程中运行 Shell 脚本 1) 将 Shell 脚本作为程序运行 切换到脚本所在位置(还要给脚本加上可执行权限) [mozhiyan@localhost demo]$ ./test.sh #执 ...

  6. 多线程测试工具groboutils的使用

    一直使用junit做为服务测试框架,感觉不错.最近有人反映在高并发的情况下,存在服务调不到.无奈再次打开单元测试模拟高并发的 情况,却发现junit不支持并发测试      引入groboutils ...

  7. JSP中九大内置对象及其作用

    jsp中有九大内置对象分别为:request,response,session,application,out,pageContext,page,config,exception. request:请 ...

  8. <input type="radio">单选按钮

    转自:http://www.divcss5.com/html/h490.shtml1 <form> 男性: <input type="radio" checked ...

  9. fpga错误总结

    Error (10200): Verilog HDL Conditional Statement error at ps2_con_cmd.v(11): cannot match operand(s) ...

  10. 事件,IO,select

    事件驱动模型 对于普通编程来说,代码遵循线性流程:开始-->代码A-->代码B-->代码C-->...-->结束,编程者知道代码的运行顺序,由编程者控制 事件驱动模型,流 ...