Elasticsearch-Java API操作(一)API基本操作(6)【新建文档(源数据map方式添加json)】
Posted on 2020-08-14 23:15 MissRong 阅读(331) 评论(0) 收藏 举报大数据技术之Elasticsearch-Java API操作(一)API基本操作
新建文档(源数据map方式添加json)
1)源代码
|
@Test public void createIndexByMap() {
// 1 文档数据准备 Map<String, Object> json = new HashMap<String, Object>(); json.put("id", "2"); json.put("title", "基于Lucene的搜索服务器"); json.put("content", "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口");
// 2 创建文档 IndexResponse indexResponse = client.prepareIndex("blog", "article", "2").setSource(json).execute().actionGet();
// 3 打印返回的结果 System.out.println("index:" + indexResponse.getIndex()); System.out.println("type:" + indexResponse.getType()); System.out.println("id:" + indexResponse.getId()); System.out.println("version:" + indexResponse.getVersion()); System.out.println("result:" + indexResponse.getResult());
// 4 关闭连接 client.close(); } |
2)结果查看

********自己操作********
Java代码:
// 三、新建文档--使用map
@Test
public void createDocByMap() {
// 源数据map构建器添加json
Map<String, Object> json = new HashMap<String, Object>();
json.put("id", "2");
json.put("title", "基于Lucene的搜索服务器");
json.put("content", "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口");
// 创建文档
// 注意:这里没有传第三个数-ID,但是ES依然会自动产生ID的
IndexResponse indexResponse = client.prepareIndex("blog", "article").setSource(json).execute().actionGet();
// 打印返回结果
System.out.println("index:" + indexResponse.getIndex());
System.out.println("type:" + indexResponse.getType());
System.out.println("id:" + indexResponse.getId());
System.out.println("version:" + indexResponse.getVersion());
System.out.println("result:" + indexResponse.getResult());
// 关闭连接
client.close();
}
结果:


浙公网安备 33010602011771号