package com.atgulgu.es.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.xcontent.XContentType;
/**
* ClassName: ESTest_Client
* Package: com.atgulgu.es.test
*
* @Author
* @Create 2024/10/7 0007 23:37
* @Version 1.0
*/
public class ESTest_Doc_Insert_Batch {
public static void main(String[] args) throws Exception {
//创建ES客户单
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.1.108",9200,"http"))
);
//批量插入数据
BulkRequest request = new BulkRequest();
request.add( new IndexRequest().index("user").id("1001").source(XContentType.JSON,"name","zhangsan"));
request.add( new IndexRequest().index("user").id("1002").source(XContentType.JSON,"name","lisi"));
request.add( new IndexRequest().index("user").id("1003").source(XContentType.JSON,"name","wangwu"));
request.add( new IndexRequest().index("user").id("1004").source(XContentType.JSON,"name","libai"));
request.add( new IndexRequest().index("user").id("1005").source(XContentType.JSON,"name","dufu"));
BulkResponse reponse = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(reponse.getTook());
System.out.println(reponse.getItems());
//关闭ES客户端
esClient.close();
}
}
执行上述代码的结果:
批量删除文档的代码
package com.atgulgu.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.xcontent.XContentType;
/**
* ClassName: ESTest_Client
* Package: com.atgulgu.es.test
*
* @Author
* @Create 2024/10/7 0007 23:37
* @Version 1.0
*/
public class ESTest_Doc_Delete_Batch {
public static void main(String[] args) throws Exception {
//创建ES客户单
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.1.108",9200,"http"))
);
//批量删除数据
BulkRequest request = new BulkRequest();
request.add( new DeleteRequest().index("user").id("1001"));
request.add( new DeleteRequest().index("user").id("1002"));
request.add( new DeleteRequest().index("user").id("1003"));
request.add( new DeleteRequest().index("user").id("1004"));
request.add( new DeleteRequest().index("user").id("1005"));
BulkResponse reponse = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(reponse.getTook());
System.out.println(reponse.getItems());
//关闭ES客户端
esClient.close();
}
}