==================================================================
需要添加下面的maven依赖
org.elasticsearch
elasticsearch
7.13.4
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.13.4
org.apache.logging.log4j
log4j-api
2.8.2
org.apache.logging.log4j
log4j-core
2.8.2
com.fasterxml.jackson.core
jackson-databind
2.9.9
junit
junit
4.12
二、具体代码以及操作
======================================================================
1、创建索引
public static void Index_Create() throws Exception{
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”,9200,“http”))
);
// 创建索引
CreateIndexRequest request = new CreateIndexRequest(“user”);
CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);
//响应状态
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println(“索引操作:”+ acknowledged);
//关闭ES客户端
esClient.close();
}
2、查询索引
//查询索引
public static void Index_Search() throws Exception{
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”,9200,“http”))
);
// 查询索引
GetIndexRequest request = new GetIndexRequest(“user”);
GetIndexResponse getIndexResponse = esClient.indices().get(request, RequestOptions.DEFAULT);
//信息打印
System.out.println(getIndexResponse.getAliases());
System.out.println(getIndexResponse.getMappings());
System.out.println(getIndexResponse.getSettings());
//关闭ES客户端
esClient.close();
}
3、删除索引
//删除索引
public static void Index_Delete() throws Exception{
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”,9200,“http”))
);
// 查询索引
DeleteIndexRequest request = new DeleteIndexRequest(“user”);
AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT);
//响应状态
System.out.println(response.isAcknowledged());
//关闭ES客户端
esClient.close();
}
4、创建文档(插入数据)
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
// 插入数据
IndexRequest request = new IndexRequest();
request.index(“user”).id(“1001”);
User user = new User();
user.setName(“zhangsan”);
user.setAge(30);
user.setSex(“男”);
// 向ES插入数据,必须将数据转换位JSON格式
ObjectMapper mapper = new ObjectMapper();
String userJson = mapper.writeValueAsString(user);
request.source(userJson, XContentType.JSON);
IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
esClient.close();
}
批量插入数据:
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
// 批量插入数据
BulkRequest request = new BulkRequest();
request.add(new IndexRequest().index(“user”).id(“1001”).source(XContentType.JSON, “name”, “zhangsan”, “age”,30,“sex”,“男”));
request.add(new IndexRequest().index(“user”).id(“1002”).source(XContentType.JSON, “name”, “lisi”, “age”
【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
开源分享完整内容戳这里
,30,“sex”,“女”));
request.add(new IndexRequest().index(“user”).id(“1003”).source(XContentType.JSON, “name”, “wangwu”, “age”,40,“sex”,“男”));
request.add(new IndexRequest().index(“user”).id(“1004”).source(XContentType.JSON, “name”, “wangwu1”, “age”,40,“sex”,“女”));
request.add(new IndexRequest().index(“user”).id(“1005”).source(XContentType.JSON, “name”, “wangwu2”, “age”,50,“sex”,“男”));
request.add(new IndexRequest().index(“user”).id(“1006”).source(XContentType.JSON, “name”, “wangwu3”, “age”,50,“sex”,“男”));
request.add(new IndexRequest().index(“user”).id(“1007”).source(XContentType.JSON, “name”, “wangwu44”, “age”,60,“sex”,“男”));
request.add(new IndexRequest().index(“user”).id(“1008”).source(XContentType.JSON, “name”, “wangwu555”, “age”,60,“sex”,“男”));
request.add(new IndexRequest().index(“user”).id(“1009”).source(XContentType.JSON, “name”, “wangwu66666”, “age”,60,“sex”,“男”));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());
System.out.println(response.getItems());
esClient.close();
}
5、修改文档
将上面的数据中的性别改成女
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
// 修改数据
UpdateRequest request = new UpdateRequest();
request.index(“user”).id(“1001”);
request.doc(XContentType.JSON, “sex”, “女”);
UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
esClient.close();
}
6、普通的查询文档
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
// 查询数据
GetRequest request = new GetRequest();
request.index(“user”).id(“1001”);
GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
esClient.close();
}
7、删除文档
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
DeleteRequest request = new DeleteRequest();
request.index(“user”).id(“1001”);
DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
esClient.close();
}
批量删除:
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 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”));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());
System.out.println(response.getItems());
esClient.close();
}
8、高级查询文档
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
// 1. 查询索引中全部的数据
// SearchRequest request = new SearchRequest();
// request.indices(“user”);
// request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
// SearchHits hits = response.getHits();
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// 2. 条件查询 : termQuery
// SearchRequest request = new SearchRequest();
// request.indices(“user”);
// request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery(“age”, 30)));
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
// SearchHits hits = response.getHits();
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// 3. 分页查询
// SearchRequest request = new SearchRequest();
// request.indices(“user”);
// SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
// // (当前页码-1)*每页显示数据条数
// builder.from(2);
// builder.size(2);
// request.source(builder);
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
// SearchHits hits = response.getHits();
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }