Java Code Examples for org.elasticsearch.client.Client#prepareBulk()

The following examples show how to use org.elasticsearch.client.Client#prepareBulk() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: hsweb-learning   File: ElasticSearch.java    License: Apache License 2.0 6 votes vote down vote up
private static void BulkIndex(Client client) throws IOException {

        BulkRequestBuilder requestBuilder = client.prepareBulk();

        requestBuilder.add(client.prepareIndex("twitter","tweet","4")
                .setSource(jsonBuilder()
                        .startObject()
                        .field("user","niekaijie")
                        .field("school","beiyou")
                        .field("address","haidianqu")
                        .endObject())
        );
        requestBuilder.add(client.prepareIndex("twitter","tweet","3")
                .setSource(jsonBuilder()
                        .startObject()
                        .field("user","林志颖aa")
                        .field("school","台湾大学")
                        .field("address","台湾")
                        .endObject())
        );
        BulkResponse response = requestBuilder.get();


    }
 
Example 2
/**
 * bulkIndex
 * @throws Exception
 */
private static void bulkIndex() throws Exception{
	Client client = createTransportClient();
	BulkRequestBuilder bulkRequest = client.prepareBulk();
	bulkRequest.add(client.prepareIndex("book", "book", "3")
					.setSource(jsonBuilder()
									.startObject()
									.field("name", "Docker开发实践")
									.field("author", "曾金龙 肖新华 刘清")
									.field("pubinfo", "人民邮电出版社")
									.field("pubtime", "2015-07-01")
									.field("desc", "《Docker开发实践》由浅入深地介绍了Docker的实践之道,首先讲解Docker的概念、容器和镜像的相关操作、容器的数据管理等内容,接着通过不同类型的应用说明Docker的实际应用,然后介绍了网络、安全、API、管理工具Fig、Kubernetes、shipyard以及Docker三件套(Machine+Swarm+Compose)等,最后列举了常见镜像、Docker API等内容。")
									.endObject()
					)
	);

	bulkRequest.add(client.prepareIndex("book", "book", "4")
					.setSource(jsonBuilder()
									.startObject()
									.field("name", "图灵程序设计丛书:Hadoop基础教程")
									.field("author", "张治起")
									.field("pubinfo", "人民邮电出版社")
									.field("pubtime", "2014-01-01")
									.field("desc", "《图灵程序设计丛书:Hadoop基础教程》包括三个主要部分:第1~5章讲述了Hadoop的核心机制及Hadoop的工作模式;第6~7章涵盖了Hadoop更多可操作的内容;第8~11章介绍了Hadoop与其他产品和技术的组合使用。《图灵程序设计丛书:Hadoop基础教程》目的在于帮助读者了解什么是Hadoop,Hadoop是如何工作的,以及如何使用Hadoop从数据中提取有价值的信息,并用它解决大数据问题")
									.endObject()
					)
	);

	BulkResponse bulkResponse = bulkRequest.execute().actionGet();
	if (bulkResponse.hasFailures()) {
		BulkItemResponse[] bulkItemResponse = bulkResponse.getItems();
		for (int i = 0; i <bulkItemResponse.length ; i++) {
			System.out.println(bulkItemResponse[i].getItemId()+":"+bulkItemResponse[i].getIndex()+":"+bulkItemResponse[i].getFailureMessage());
		}
	}
}
 
Example 3
/**
 * 删除查询到的文档
 * @param index
 * @param name
 * @param value
 */
private static void scrollSearchDelete(String index,String name,String value){
	Client client = createTransportClient();
	QueryBuilder qb = termQuery(name, value);
	SearchResponse scrollResp = client.prepareSearch(index)
			.setSearchType(SearchType.SCAN)
			.setScroll(new TimeValue(60000))
			.setQuery(qb)
			.setSize(100).execute().actionGet(); //100 hits per shard will be returned for each scroll

	BulkRequestBuilder bulkRequest = client.prepareBulk();

	while (true) {
		for (SearchHit hit : scrollResp.getHits().getHits()) {
			bulkRequest.add(client.prepareDelete(hit.getIndex(),hit.getType(),hit.getId()));
		}
		scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
		if (scrollResp.getHits().getHits().length == 0) {
			break;
		}
	}
	BulkResponse bulkResponse = bulkRequest.execute().actionGet();
	if (bulkResponse.hasFailures()) {
		BulkItemResponse[] bulkItemResponse = bulkResponse.getItems();
		for (int i = 0; i <bulkItemResponse.length ; i++) {
			System.out.println(bulkItemResponse[i].getItemId()+":"+bulkItemResponse[i].getIndex()+":"+bulkItemResponse[i].getFailureMessage());
		}
	}
}
 
Example 4
public ElasticsearchBulkUpdater(Client client) {
	this.client = client;
	this.bulkRequest = client.prepareBulk();
}
 
Example 5
public RequestBuffer (Client client, int size) {
    this.client = client;
    this.size = size;
    this.bulkRequest = client.prepareBulk();
}
 
Example 6
public static BulkResponse deleteByQuery(final Client client, final String index, final String[] types,
        final QueryBuilder queryBuilder) {

    //TODO use delete by query plugin
    //https://github.com/elastic/elasticsearch/tree/2.3/plugins/delete-by-query
    final int size = 10000;
    
    final SearchRequestBuilder searchBuilder = client.prepareSearch().setIndices(index).setTypes(types).setQuery(queryBuilder).setSize(size);
    
    SearchResponse deleteResponse = searchBuilder.get();

    final BulkRequestBuilder brb = client.prepareBulk();

    while (deleteResponse.getHits().getTotalHits() > 0) {
        for (final SearchHit hit : deleteResponse.getHits()) {
            brb.add(new DeleteRequest(hit.getIndex(), hit.getType(), hit.getId()));
        }
        
        brb.setRefresh(true).get();
        
        deleteResponse = searchBuilder.get();
    }
    
    return null;
}