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/索 ...
随机推荐
- Java实验3与第五周总结
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码.结果截图.) •统计该字符串中字母s出现的次数. •统计该字符串中子串" ...
- numpy库的认识以及数组的创建
numpy库 numpy是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础.numpy十分高效,基于NumPy的算法要比纯Python快10到100倍(甚至 ...
- 对C++拷贝构造函数的一点理解
一. 什么是拷贝构造函数 先看一个简单的例子: #include <iostream> using namespace std; class CExample { private: int ...
- XADC
XADC实验 1.XADC概述 Xilinx7系列内部自带一个双通道12位分辨率的高速(1MSPS 1M sample per second)采样速率的模拟混合信号处理模块,双通道的ADC支持单极和差 ...
- elementUI 等 UI框架中,@change方法传递参数
有些业务中,在使用 @change 回调的时候需要动态获取当前循环下的特定值,但是@change方法一旦传递参数就会覆盖原本的数据,对此,有两种方法解决: // 这种方法据说会改变 this 指向 ...
- springmvc,springboot单元测试配置
1. springmvc单元测试配置 <dependency> <groupId>junit</groupId> <artifactId>junit&l ...
- numpy.random.randn()和numpy.random.rand()
1 numpy.random.rand() (1)numpy.random.rand(d0,d1,…,dn) rand函数根据给定维度生成[0,1)之间的数据,包含0,不包含1 dn表格每个维度 返回 ...
- Apache 配置外网站点
基于域名,一般是对外网站 www.etiantian.org/var/www/html/www blog.etiantian.org /var/www/html/blog bbs.tiantian.o ...
- Java web项目搭建系列之二 Jetty下运行项目
在项目pom.xml文件中添加Jetty运行配置 在pom.xml文件project节点下插入如下代码: <build> <plugins> <plugin> &l ...
- evpp tcpclient
重点函数讲解①:消息回调函数——void evpp::TCPClient::SetMessageCallback(const evpp::MessageCallback& cb) 注:设置消息 ...