elasticsearch java api 学习笔记(7.15.0)

Posted 我得支棱起来啊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch java api 学习笔记(7.15.0)相关的知识,希望对你有一定的参考价值。

Java API 环境准备

  1. 新建maven 工程,并将所需依赖引入:
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.76</version>
</dependency>
<!-- elastic 相关依赖 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<scope>runtime</scope>
	<optional>true</optional>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<optional>true</optional>
	<scope>provided</scope>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>
  1. 初始化一个配置类,在Spring 中初始化一个Bean ,这个Bean 具有连接elasticsearch 的作用。在用到连接数据库的时候,直接从Spring 中拿(由于会频繁的用到Elasticsearch 连接和关闭的功能,所以将他的创建权交给Spring,我们不去每次都进行连接操作和关闭操作了)。
package com.lbk.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticSearchConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http")));
        return client;
    }
}
  1. 然后新建一个测试类ESTest ,之后所有对Elasticsearch 的操作测试都在这里进行
public class ESTest {
    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;
    @Test
    // 测试创建索引
    void testCreateIndex() throws IOException {
     	...
    }
	...
}

Java API 的各种操作

1. 对索引的操作

  • 创建索引
	@Test
    // 测试创建索引
    void testCreateIndex() throws IOException {
        // 创建索引
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("user-lbk2");
        // 发送请求 ,获得响应
        CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);

        boolean acknowledged = createIndexResponse.isAcknowledged();
        System.out.println("操作状态 = " + acknowledged);

        // 关闭客户端连接
        client.close();
    }
    @Test
    // 测试查询索引
    void testSearchIndex() throws IOException {
        // 查询索引,创建对象
        GetIndexRequest getIndexRequest = new GetIndexRequest("user-lbk");
        // 发送请求,获取响应
        GetIndexResponse getIndexResponse = client.indices().get(getIndexRequest, RequestOptions.DEFAULT);

        System.out.println(getIndexResponse.getAliases());
        System.out.println(getIndexResponse.getMappings());
        System.out.println(getIndexResponse.getIndices());

        // 关闭客户端连接
        client.close();
    }
	@Test
    // 测试删除索引
    void delIndex() throws  IOException {
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("user-lbk2");
        AcknowledgedResponse response = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        System.out.println("操作结果= " + response.isAcknowledged());
    }

2. 对文档的操作

 	@Test
    // 测试文档的新增
    void testDocAdd() throws IOException {
        // 封装请求体
        IndexRequest indexRequest = new IndexRequest("user-lbk");
        User user = new User("zhangsan",245);
        indexRequest.id("1001");
        indexRequest.source(JSON.toJSONString(user), XContentType.JSON);

        // 发送请求, 获得响应
        IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(response.status());
        System.out.println(response.toString());
    }

    @Test
    // 测试文档的修改
    void testDocUpdate() throws  IOException {
        // 修改文档的请求对象
        UpdateRequest updateRequest = new UpdateRequest();
        // 封装请求体,配置参数
        updateRequest.index("user-lbk").id("1001");
        updateRequest.doc(XContentType.JSON, "name", "lbk2");
        // 发送请求,获取响应
        UpdateResponse updateResponse = client.update(updateRequest,RequestOptions.DEFAULT);

        // 打印响应结果
        System.out.println("status: " + updateResponse.status());
        System.out.println("_index: " + updateResponse.getIndex());
        System.out.println("_result: " + updateResponse.getResult());
    }

    @Test
    // 测试文档的删除
    void testDocDel() throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("user-lbk");
        deleteRequest.id("1001");
        DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);

        // 打印信息
        System.out.println(deleteResponse.status());

    }
    @Test
    // 测试文档的查询
    void testDocSearch() throws IOException {
        // 创建请求对象
        GetRequest getRequest = new GetRequest().index("user-lbk").id("1001");
        // 客户段发送请求,返回响应对象
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        // 打印返回结果
        System.out.println("_source:" + getResponse.getSource());
        System.out.println("_index:" + getResponse.getIndex());
        System.out.println("_source:" + getResponse.getSourceAsString());
    }

3. 对文档进行批量操作

 	@Test
    // 测试文档的批量添加
    void testBatchDocAdd() throws IOException {
        // 创建批量新增请求对象
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(new IndexRequest().index("user-lbk").id("2002").source(XContentType.JSON,"name","赵雷","age",30));
        bulkRequest.add(new IndexRequest().index("user-lbk").id("2003").source(XContentType.JSON,"name","艾怡良","age",20));
        bulkRequest.add(new IndexRequest().index("user-lbk").id("2004").source(XContentType.JSON,"name","单依纯","age",18));
        bulkRequest.add(new IndexRequest().index("user-lbk").id("2005").source(XContentType.JSON,"name","宋东野","age",45));
        bulkRequest.add(new IndexRequest().index("user-lbk").id("2006").source(XContentType.JSON,"name","马頔","age",30));
        // 客户端发送请求,获取响应对象
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println("took:" + bulkResponse.getTook());
        System.out.println("items:" + bulkResponse.getItems());
    }

    @Test
    // 测试文档的批量删除
    void testBatchDocDel() throws IOException {
        // 创建批量删除请求对象
        BulkRequest bulkRequest = new BulkRequest();
        // 封装请求体
        bulkRequest.add(new DeleteRequest("user-lbk").id("2002"));
        bulkRequest.add(new DeleteRequest("user-lbk").id("2003"));
        bulkRequest.add(new DeleteRequest("user-lbk").id("2004"));
        bulkRequest.add(new DeleteRequest("user-lbk").id("2005"));
        bulkRequest.add(new DeleteRequest("user-lbk").id("2006"));

        // 客户端发送请求,获取响应对象
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);

        // 打印结果信息
        System.out.println("took:" + bulkResponse.getTook());
        System.out.println("items:" + bulkResponse.getItems());

    }

4. 高级查询

	@Test
    // 查询所有数据
    void testQueryAll() throws IOException {
        // 构建搜索请求对象
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user-lbk");
        // 构建查询的请求体
        SearchSourceBuilder builder = new SearchSourceBuilder();
        // 查询所有的数据
        builder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(builder);

        // 发送请求,获得响应
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        // 查询匹配
        SearchHits hits = response.getHits();
        System.out.println("took:" + response.getTook());
        System.out.println("total:" + hits.getTotalHits());
        System.out.println(" hits ==================");
        // 将每条查询到的数据以字符串形式输出
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }

    @Test
    // 条件查询
    void testQueryDocByCondiTion() throws IOException {
        // 构架查询的请求对象
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user-lbk");
        // 构建查询条件的条件体
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.termQuery("name","zhangsan"));
        searchRequest.source(builder);
        // 发送请求,获得响应
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        // 输出返回结果
        SearchHits hits = response.getHits();
        System.out.println("took:" + response.getTook());
        System.out.println("total:" + hits.getTotalHits());
        System.out.println("=============================");
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }

    }

    @Test
    // 分页查询
    void testSearchPage() throws IOException {
        // 构建查询的请求对象
        SearchRequest searchRequest = new SearchRequest("user-lbk");
        // 构建查询的请求体
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchAllQuery());

        // 构建分页
        builder.from(0);
        builder.size(2);
        // 将请求体放到请求对象中
        searchRequest.source(builder);
        // 发送请求,获取响应
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        System.out.println("took:" + response.getTook());
        System.out.println("timeout:" + response.isTimedOut());
        System.out.println("total:" + hits.getTotalHits());
        System.out.println("=====================");
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }

    @Test
    // 查询排序
    void testSearchAndOrder() throws IOException {
        // 构建查询的请求对象
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user-lbk");
        // 构建请求体
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchAllQuery());
        // 排序   ASC 增序   DESC 降序
        builder.sort("age", SortOrder.DESC);
        // 将请求体放到请求中
        searchRequest.source(builder);
        // 发送请求,获得响应
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }

    @Test
    // 组合查询
    void testSearchAndOrNot() throws IOException {
        // 构建请求对象
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user-lbk");
        // 构建请求对象
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // 必须包含   must ; 必须不包含  mustNot
//        boolQueryBuilder.mustNot(QueryBuilders.matchQuery("age","18"));
        // 可能包含
        boolQueryBuilder.should(QueryBuilders.matchQuery("age","20"));
        builder.query(boolQueryBuilder);
        // 将请求体放到请求中
        searchRequest.source(builder);
        // 发送请求,返回响应
        SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("======================");
        System.out.println("took:" + response.getTook());
        System.out.println("total:" + hits.getTotalHits());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
            System.out.println(hit.getScore());
        }
    }

    @Test
    // 范围查询
    void testSearchLimit() throws IOException {
        SearchRequest searchRequest = new SearchRequest("user-lbk");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
        rangeQueryBuilder.lte("40");
        rangeQueryBuilder.gte("10");
        builder.query(rangeQueryBuilder);
        searchRequest.source(builder);
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) {
            System.out.println("score:" + hit.getScore());
            System.out.println(hit.getSourceAsString());
        }
    }

    @Test
    // 模糊查询
    void testSearchFuzzy() throws IOException {
        SearchRequest searchRequest = new SearchRequest("user-lbk");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.fuzzyQuery("name","宋").fuzziness(Fuzziness.ONE));
        searchRequest.source(builder);

        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();

        System.out.println("maxScore:" + hits.getMaxScore());
        System.out.println("took:" + response.getTook());
        System.out.println("total:" + hits.getTotalHits());

        for (SearchHit hit : hits) {
            System.out.println("score:" + hit.getScore());
            System.out.println(hit.getSourceAsString());
        }
    }

    @Test
    // 高亮查询
    void testSearchHighlight() throws IOException {
        // 创建请求对象
        SearchRequest searchRequest = new SearchRequest("user-lbk");
        // 创建请求体
        SearchSourceBuilder builder = new SearchSourceBuilder();
        // 构建查询方式
        /*
        *  这里关于中文查不到的情况找到了 用 name.keywword 的方法
        *  或者是将 term 换成 matchPhraseQuery
        * */
        MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("name", "单依纯");
        // 将查询方式放入请求体中
        builder.query(matchPhraseQueryBuilder);
        // 开始构建高亮字段
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color=\'red\'>");
        highlightBuilder.postTags("</font>");
        highlightBuilder.field("name.keyword");
        // 将高亮对象放入请求体中
        builder.highlighter(highlightBuilder);
        // 将请求体放入请求对象中
        searchRequest.source(builder);
        // 客户端发送请求,获取响应对象
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

        // 打印响应结果
        SearchHits hits = response.getHits();
        System.out.println("took:" + response.getTook());
        System.out.println("total:" + hits.getTotalHits());
        for (SearchHit hit : hits) {
            String sourceAsString = hit.getSourceAsString();
            System.out.println(sourceAsString);
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            System.out.println(highlightFields);
        }
    }

    @Test
    // 最大值查询
    void testSearchMax() throws IOException {
        SearchRequest searchRequest = new SearchRequest().indices("user-lbk");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.aggregation(AggregationBuilders.max("maxAge").field("age"));

        // 设置请求体
        searchRequest.source(builder);
        // 发送请求,获取响应对象
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        // 打印响应数据
        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }

    }
    @Test
    //分组查询
    void testSearchGroup() throws IOException {
        SearchRequest searchRequest = new SearchRequest("user-lbk");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        // 设置请求体
        builder.aggregation(AggregationBuilders.terms("age_groupby").field("age"));
        searchRequest.source(builder);
        // 请求客户端 ,获取响应对象
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }

总结

通过以上的方法,可以实现对elasticsearch 进行简单的增删改查、数据的批量增加和批量删除;可以完成一些高级查询,如聚合、分组等。

ElasticSearch API for JAVA 学习笔记

本篇学习笔记来源于ES官方网站的指导文档:ES JAVA API

Client

Client是一个类,通过这个类可以实现对ES集群的各种操作:Index, Get, Delete , Search,以及对ES集群的管理任务。 Client的构造需要基于TransportClient

TransportClient

TransportClient可以远程连接ES集群,通过一个传输模块,但是它不真正的连接到集群,只是获取集群的一个或多个初始传输地址,在每次请求动作时,才真正连接到ES集群。

Settings

Settings类主要是在启动Client之前,配置一些属性参数,主要配置集群名称cluster.name,还有其他参数: client.transport.sniff  是否为传输client添加嗅探功能
client.transport.ignore_cluster_name 设为true,忽略连接节点的集群名称验证
client.transport.ping_timeout 设置ping节点时的时间限,默认5s
client.transport.nodes_sampler_interval 设置sample/ping nodes listed 间隔时间,默认5s

		//通过Settings类设置属性参数
		Settings settings = Settings.settingsBuilder().put("cluster.name","index-name").build();
		
		//启动Client
		Client client = TransportClient.builder().settings(settings).build().
				addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.xxx.xxx"),9300));
		
		//如果不需要设置参数,直接如下
		/*Client client = TransportClient.builder().build().
				addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.xxx.xxx"),9300));*/
		
		//关闭Clinet
		client.close();

Document API

主要分为以下类:Index API , Get API , Delete API , Update API, Multi Get API, Bulk API

es中的增删改查

Index API可以索引一个典型的JSON文档到指定的索引中,并且可以使它可以检索。

产生JSON

JSON产生可以有以下几种方式: 手动拼接一个JSON字符串 使用Map 使用第三方库,比如Jackson 使用内置的XContentFactory.jsonBuilder() 每种类型都会转换为byte[],因此如果对象已经是这种形式,可以直接使用,jsonBuilder是一个高度优化了的JSON产生器,它直接构造byte[] 通过下边的代码讲解四种方法: index-api, get-api, delete-api, update-api
/**
	 * es-api的方法学习:
	 * 1.prepareIndex方法:索引数据到ElasticSearch
	 * 2.prepareGet方法:获取信息
	 * 3.prepareDelete方法:删除信息
	 * 4.update方法:更新信息
	 *   4.1 upsert:在使用update方法时:
	 *       a:针对文档不存在的情况时,做出index数据的操作,update无效;
	 *        b:如果文档存在,那么index数据操作无效,update有效;
	 */
	public static void main(String[] args) throws IOException, InterruptedException, ExecutionException 
		//通过Settings类设置属性参数  
		Settings settings = Settings.settingsBuilder().put("cluster.name","myApp").build();  
		  
		//启动Client  
		Client client = null;
		try 
			client = TransportClient.builder().settings(settings).build().  
			        addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("101.200.124.27"),9300));
		 catch (UnknownHostException e) 
			e.printStackTrace();
		  
		//执行操作
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		XContentBuilder jsonBuilder = XContentFactory.jsonBuilder()
				.startObject()
				.field("user","yuchen")
				.field("interest","reading book")
				.field("insert_time",df.format(new Date()))
				.endObject();
		//1.prepareIndex方法:索引数据到ElasticSearch
		IndexResponse response = client.prepareIndex("index-test","weibo","4")
			.setSource(jsonBuilder)
			.get();
		
		String _index = response.getIndex();
		String _type = response.getType();
		String _id = response.getId();
		long _version = response.getVersion();
		boolean created = response.isCreated();
		System.out.println(_index+" "+_type+" "+_id+" "+_version+" "+created);
		
		//2.prepareGet方法:获取信息
		GetResponse getResponse = client.prepareGet("index-test","weibo","1").get();
		System.out.println(getResponse.getSourceAsString());
		
		//3.prepareDelete方法:删除信息
		DeleteResponse deleteResponse = client.prepareDelete("index-test","weibo","4").get();
		System.out.println(deleteResponse.isFound());
		
		//4.update方法:更新信息
		UpdateRequest updateRequest = new UpdateRequest();
		updateRequest.index("index-test");
		updateRequest.type("weibo");
		updateRequest.id("1");
		updateRequest.doc(XContentFactory.jsonBuilder().startObject().field("interest","music").endObject());
		UpdateResponse updateResponse = client.update(updateRequest).get();
		System.out.println(updateResponse.isCreated());
		//update方法: 可以为已有的文档添加新的字段
		UpdateResponse updateResponse2 = client.prepareUpdate("index-test", "weibo", "1")
			.setDoc(XContentFactory.jsonBuilder()
				.startObject()
				.field("interest2","reading")
				.endObject()).get();
		System.out.println(updateResponse2.isCreated());
		//4.1 upsert:在使用update方法时,a:针对文档不存在的情况时,做出index数据的操作,update无效;
		//						  b:如果文档存在,那么index数据操作无效,update有效;
		//先构建一个IndexRequest
		IndexRequest indexRequest = new IndexRequest("index-test","weibo","14");
		indexRequest.source(XContentFactory.jsonBuilder()
				.startObject()
				.field("user","yuchen2")
				.field("interest","eating")
				.field("insert_time",df.format(new Date()))
				.endObject());
		//再构建一个UpdateRequest,并用IndexRequest关联
		UpdateRequest updateRequest3 = new UpdateRequest("index-test","weibo","14");
		updateRequest3.doc(XContentFactory.jsonBuilder()
				.startObject()
				.field("interest2","love")
				.endObject()
				).upsert(indexRequest);
		client.update(updateRequest3).get();
		
		if(client != null)
			client.close();  
		
	

批量操作

Multi Get Api 和 Bulk Api可进行批量的增删改查 使用Multi Get Api 批量获取:
		//1. Muti-get Api
		//可以指定单个id,也在index,type下指定一个id-list;也可以指定别的index/type
		MultiGetResponse multiGetResponse = client.prepareMultiGet()
				.add("index-test","weibo","1")//指定单个id
				.add("index-test","weibo","11","13","14")//指定一个id-list
				.add("index-other","news","1","3").get();//指定别的index/type
		for(MultiGetItemResponse item:multiGetResponse)
			GetResponse response = item.getResponse();
			System.out.println(response.getSourceAsString());
		
Bulk Api批量增加:
		//2.Bulk Api:可以进行批量index和批量删除操作
		//2.1批量增加
		BulkRequestBuilder bulkRequest = client.prepareBulk();
		bulkRequest.add(client.prepareIndex("index-test", "weibo", "20")
	        .setSource(XContentFactory.jsonBuilder()
	                .startObject()
	                    .field("user", "yuchen20")
	                    .field("postDate", new Date())
	                    .field("message", "trying out Elasticsearch")
	                .endObject()
	              )
          );
		bulkRequest.add(client.prepareIndex("index-test", "weibo", "21")
		        .setSource(XContentFactory.jsonBuilder()
		                .startObject()
		                    .field("user", "yuchen21")
		                    .field("postDate", new Date())
		                    .field("message", "trying out Elasticsearch")
		                .endObject()
		              )
	          );
		
		BulkResponse bulkResponse = bulkRequest.get();
		if(bulkResponse.hasFailures())
			//...
		
Bulk Api批量删除:
		//2.2批量删除
		BulkRequestBuilder bulkRequest = client.prepareBulk();
		bulkRequest.add(client.prepareDelete("index-test", "weibo", "20")
          );
		bulkRequest.add(client.prepareDelete("index-test", "weibo", "21")
	          );
		
		BulkResponse bulkResponse = bulkRequest.get();
		if(bulkResponse.hasFailures())
			System.out.println("bulk error:"+bulkResponse.buildFailureMessage());
		
Bulk Api 批量更新
		//2.3批量更新
		BulkRequestBuilder bulkRequest = client.prepareBulk();
		bulkRequest.add(client.prepareUpdate("index-test", "weibo", "11").setDoc(XContentFactory
				.jsonBuilder().startObject()
				.field("country","China")//新添加字段
				.endObject()
				)
          );
		bulkRequest.add(client.prepareUpdate("index-test", "weibo", "13").setDoc(XContentFactory
				.jsonBuilder().startObject()
				.field("user","yuchen13")//更新字段
				.endObject()
				)
	          );
		
		BulkResponse bulkResponse = bulkRequest.get();
		if(bulkResponse.hasFailures())
			System.out.println("bulk error:"+bulkResponse.buildFailureMessage());
		

BulkProcessor设置批量请求的属性

		//BulkProcessor
		BulkProcessor bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener() 
			@Override
			public void beforeBulk(long arg0, BulkRequest arg1) 
				//批量执行前做的事情
				System.out.println("bulk api action starting...");
			
			@Override
			public void afterBulk(long arg0, BulkRequest arg1, Throwable arg2) 
				System.out.println("exception:bukl api action ending...:"+arg2.getMessage());
			
			@Override
			public void afterBulk(long arg0, BulkRequest arg1, BulkResponse arg2) 
				//正常执行完毕后...
				System.out.println("normal:bukl api action ending...");
			
		)
		//设置多种条件,对批量操作进行限制,达到限制中的任何一种触发请求的批量提交
		.setBulkActions(1000)//设置批量操作一次性执行的action个数,根据请求个数批量提交
		//.setBulkSize(new ByteSizeValue(1,ByteSizeUnit.KB))//设置批量提交请求的大小允许的最大值
		//.setFlushInterval(TimeValue.timeValueMillis(100))//根据时间周期批量提交请求
		//.setConcurrentRequests(1)//设置允许并发请求的数量
		//设置请求失败时的补偿措施,重复请求3次
		//.setBackoffPolicy(BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(100), 3))
		.build();
		for(int i =0;i<100000;i++)
			bulkProcessor.add(new IndexRequest("index-test","weibo2",""+i).source(
					XContentFactory
					.jsonBuilder()
					.startObject()
					.field("name","yuchen"+i)
					.field("interest","love"+i)
					.endObject()));
		
		bulkProcessor.awaitClose(5, TimeUnit.MINUTES);//释放bulkProcessor资源
		System.out.println("load succeed!");

默认的参数:
  • sets bulkActions to 1000
  • sets bulkSize to 5mb
  • does not set flushInterval
  • sets concurrentRequests to 1
  • sets backoffPolicy to an exponential backoff with 8 retries and a start delay of 50ms. The total wait time is roughly 5.1 seconds.








以上是关于elasticsearch java api 学习笔记(7.15.0)的主要内容,如果未能解决你的问题,请参考以下文章

elasticsearch java api 学习笔记(7.15.0)

大数据技术之_20_Elasticsearch学习_01_概述 + 快速入门 + Java API 操作 + 创建删除索引 + 新建搜索更新删除文档 + 条件查询 + 映射操作

Elasticsearch Java Rest Client API 整理总结 ——Building Queries

关于在黑马 SpringCloud 黑马旅游案例中使用 elasticsearch 7.17.9 Java API

ElasticSearch Java API

如何导入elasticsearch的JAVA API?