当前位置: 首页 > elasticsearch, 搜索 > 正文

elasticsearch java client:批量添加删除索引

1 星2 星3 星4 星5 星 (3 次投票, 评分: 5.00, 总分: 5)
Loading ... Loading ...
baidu_share

elasticsearch支持批量添加或删除索引文档,java api里面就是通过构造BulkRequestBuilder,然后把批量的index/delete请求添加到BulkRequestBuilder里面,执行BulkRequestBuilder。下面是个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import static org.elasticsearch.common.xcontent.XContentFactory.*; 
 
        BulkRequestBuilder bulkRequest = client.prepareBulk(); 
 
        bulkRequest.add(client.prepareIndex("twitter", "tweet", "1") 
                .setSource(jsonBuilder() 
                            .startObject() 
                                .field("user", "kimchy") 
                                .field("postDate", new Date()) 
                                .field("message", "trying out Elastic Search") 
                            .endObject() 
                          ) 
                ); 
 
        bulkRequest.add(client.prepareIndex("twitter", "tweet", "2") 
                .setSource(jsonBuilder() 
                            .startObject() 
                                .field("user", "kimchy") 
                                .field("postDate", new Date()) 
                                .field("message", "another post") 
                            .endObject() 
                          ) 
                ); 
 
        BulkResponse bulkResponse = bulkRequest.execute().actionGet(); 
        if (bulkResponse.hasFailures()) { 
            //处理错误 
        }

参考资料:http://www.elasticsearch.org/guide/reference/java-api/bulk.html

本文固定链接: http://www.chepoo.com/elasticsearch-java-api-bulk-index.html | IT技术精华网

elasticsearch java client:批量添加删除索引:等您坐沙发呢!

发表评论