开发者学堂课程【ElasticSearch 最新快速入门教程:Map 方式】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/642/detail/10637
Map 方式
增加素引的据格式有4种,
json、map、bean、 es helper
●JSON
@
Teat
public void testAddIndexJSON()【
string source ="(\"name\": \ "hadoop\", \"author\" \"Doug
Couting\")";
IndexResponse response
=
client
.prepareIndex(index, type, "1 ")
.
setSource(source).get (
);
System.out. println("version:+ response.getVersion());
}
●Map
@
Test
public void testAddIndexMap(){
Map map
=
new HashMap()
;
map. put("name", "HBase");
map. put("version", 1.1);
IndexResponse response =client
prepareIndex(index,type,"2). setSource(map)-get
();
System.out. println("version:"+ response. getversion())
;
先构建 map,然后将 map 封装到 source 中索引具体的内容,然后 get 的生效就完成。
使用 map 的方式新增索引,要准备一个 map 的实例,map 的值一般是 string。
代码:
*使用 map 的方式新增索引
@Test
public void testNewAddIndexMapWay(){
System.outprintin正式开始使用【使用 Map 的方式新增素引】了哦");
Map map =new LinkedHashMap<>();
map.put(name","storm");
map.put(author,睥睨天下);
map.put(version",2.4.6");
IndexResponse response= client. preparelndex(/NDEX, TYPE, "2").setSource(map).get();
System.outprintf(待新增的索引信息是:%s,es 集群反馈的结果是:%s%n,mapresponse.toString())
}
刷新一下,这里没有 storm,执行一下,看一下效果。答应的日志前处理操作,这里是 map 的实例,数据封装进去了 还是一样的索引库,索引标识手动指定 ,不指定的话,通常默认有一个索引的标志,随机生成一个字符串,
最后 es 集群继续反馈的信息是后面有一个叫 successful,然后刷新一下,应该多来一条记录,叫做睥睨天下,这个version叫2.4.6, 就成功了 。