准备工作
springboot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
elasticsearch版本7.6.1,是我本机安装的es版本
<!--elasticsearch-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.6.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.6.1</version>
</dependency>
增
GetIndexRequest request = new GetIndexRequest("isgsg_index");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
if (exists == false) {
CreateIndexRequest createIndexRequest = new CreateIndexRequest("isgsg_index");
client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println("********************** isgsg_index创建成功 *****************");
}
System.out.println("********************** isgsg_index已存在 *****************");
User user = new User("isgsg", 21);
IndexRequest indexRequest = new IndexRequest("isgsg_index");
indexRequest.id("50");
indexRequest.timeout(TimeValue.timeValueSeconds(2));
indexRequest.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println("添加是否成功:" + response.status());
删
DeleteRequest deleteRequest = new DeleteRequest("isgsg_index", "1");
deleteRequest.timeout(TimeValue.timeValueSeconds(6));
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println("删除是否成功:" + deleteResponse.status());
改
User user = new User("1001", "gsg",20);
UpdateRequest updateRequest = new UpdateRequest("isgsg_index",user.getUserId());
updateRequest.timeout("6s");
updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println("更改是否成功:" + updateResponse.status());
查
SearchRequest searchRequest = new SearchRequest("isgsg_index");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
SearchSourceBuilder query = sourceBuilder.query(matchAllQueryBuilder);
searchRequest.source(query);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
List<Map<String,Object>> user= new ArrayList<>();
for (SearchHit hit : searchResponse.getHits().getHits()) {
user.add(hit.getSourceAsMap());
}
GetRequest getRequest = new GetRequest("isgsg_index", "3");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
Map<String, Object> map = getResponse.getSourceAsMap();
- 复合查询 (相当于 mysql 的 where 和 and 查询)这里重点说一下:复合查询的条件有:must ,filter ,mustNot ,should 等。可以根据自己的需要添加查询条件,也可以同时多个must或者其它条件。
SearchRequest searchRequest = new SearchRequest("isgsg_index");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termQuery("name","黎明"))
.must(QueryBuilders.termQuery("age",20))
.filter(QueryBuilders.termQuery("sex","male"))
.mustNot(QueryBuilders.termQuery("hight","178"));
sourceBuilder.query(boolQueryBuilder);
sourceBuilder.timeout(new TimeValue(60,TimeUnit.SECONDS));
searchRequest.source(sourceBuilder);
SearchResponse search = client.search(searchRequest, DEFAULT);
List<Map<String,Object>> user = new ArrayList<>();
for (SearchHit hit : search.getHits().getHits()) {
user.add(hit.getSourceAsMap());
}
|