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. 二、Zabbix-zabbix server部署-LNMP

    部署Zabbix server主要分为两部分(软件基本都是yum安装,不要问我为什么不用源码,因为没有必须用源码的需求) 一.部署LNMP/LAMP环境,已提供zabbix的界面展示,已经zabbix ...

  2. 异步Promise及Async/Await可能最完整入门攻略

    此文只介绍Async/Await与Promise基础知识与实际用到注意的问题,将通过很多代码实例进行说明,两个实例代码是setDelay和setDelaySecond. tips:本文系原创转自我的博 ...

  3. nginx动静分离与网关

    当我们请求一个网页的时候,可能会加载很多css,js,img等静态文件:一般这些文件是很久都不会变化的,所以我们为了提高页面响应速度,完全可以将这些文件缓存到浏览器中(可以理解为cookie信息),这 ...

  4. Jpa-Spec oracle函数bitand,instr等扩展

    jpa-spec github: https://github.com/wenhao/jpa-spec 使用这个框架可以简化我们拼条件的复杂度,如下代码: public Page<Person& ...

  5. asp.net Excel导入和导出

    1.Excel数据导入到数据库中: //该方法实现从Excel中导出数据到DataSet中,其中filepath为Excel文件的绝对路径,sheetname为表示那个Excel表:        p ...

  6. AFNetworking2.0源码解析<四>

    结构 AFURLResponseSerialization负责解析网络返回数据,检查数据是否合法,把NSData数据转成相应的对象,内置的转换器有json,xml,plist,image,用户可以很方 ...

  7. wamp2.5最简单的虚拟主机配置

    1.配置host文件 2.配置httpd.conf 去掉# LoadModule rewrite_module modules/mod_rewrite.so Include conf/extra/ht ...

  8. Nginx优化_数据包头部信息过大问题

    如果客户端发出请求的URL头部信息过大,网站将不能及时响应,并通过状态码414报错. <center><h1>414 Request-URI Too Large</h1& ...

  9. 微信授权获取code/openid

    微信网页授权 如果用户在微信客户端中访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑. 关于网页授权回调域名的说明 1.在微信公众号请求用户网页授权之前,开发者需要 ...

  10. VB中输入函数InputBox的用法

    格式:InputBox(pronpt[,title][,default][,xpos,ypos]).其中的pronpt为窗口的提示词,title为输入窗口的标题,default为输入窗口的默认内容,x ...