开发者学堂课程【ElasticSearch 最新快速入门教程:批量操作 bulk】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/642/detail/10646
批量操作 bulk
内容介绍:
一、批量操作 bulk 提纲
二、演示操作
一、批量操作 bulk 提纲
@ Test
public void testBulkinsert
() {
String deptDev ="{"name":"研发部","deptNo ":20}";
String deptMarket ="{"name":"市场部","deptNo ":30}";
String deptoffice ="{"name":"行政部","deptNo ":40}";
client. prepareBulk
()
.
add( new IndexRequest( index, " dep" , " 1) , source(deptDev))
.
add( new IndexRequest( index, " dep" , 2) .
source(deptMarket)
.
add( new IndexRequest( index, " dep" , 3) , source(deptoffice))
.
add( new DeleteRequest( index, type,3))
.
get
();
}
二、演示操作
需求:测试获得指定的索引库中特定的 type 中 document 的条数
@
Test
Public void testGetCount() {
Long totalHits = client.prepareSearch(…indices:INDEX)
setTypes(TYPE)
.execute()
actionGet()
getHits()
getTotalHits();
System.out.println(
“查询到的总记录数是:” +totalHits
)
;
}
@Test
public void testBulkDealwith() {
步骤:
1、准备需要进行批量操作的数据
(1)批量新增
IndexRequestBuilder IndexRequestBuilder = client.prepareIndex(INDEX,TYPE).setSource(JSON,toJSON
String(new Product”sqoop”,”Jacson”,)))
IndexRequestBuilder IndexRequestBuilder2 = client.prepareIndex(INDEX,TYPE).setSource(JSON,toJSON
String(new Product”Flume”,”
舍我其谁
”,)))
(2)批零删除
DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete(INDEX,TYPE,id”3”);
2、批量处理实施
BulkResponse responses = client.prepareBulk()
add(indexRequestbuilder)
add(indexRequestbuilder2)
add(deleteRequestbuilder)
get();
}
3、分析批处理之后的结果
BulkItemReponse[] items = responses.getItem();
for(BulkItemResponse item:items){
System.out.println(item);
}
}