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");//设置每个文档的id,这里可以使用user的id来设置
indexRequest.timeout(TimeValue.timeValueSeconds(2));
indexRequest.source(JSON.toJSONString(user), XContentType.JSON);//用json格式传输实体类
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println("添加是否成功:" + response.status());
//根据文档id删除,这里的id可以是任何一个实体类的id,也可以传一个形参,但必须是string类型
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);
//根据userId来更改内容, 这里需要特别说明一下,这里的id如果文档中已经存在,
// 则会覆盖原来的内容,以达到实现修改的目的,但是如果不存在此id,则es会帮我们新建一个文档
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();//查询所有isgsg_index索引下的所有文档
SearchSourceBuilder query = sourceBuilder.query(matchAllQueryBuilder);
searchRequest.source(query);//执行搜索
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);//提交
List<Map<String,Object>> user= new ArrayList<>();//储存map对象
for (SearchHit hit : searchResponse.getHits().getHits()) {
user.add(hit.getSourceAsMap());//封装成一个map对象,并储存到list中
}
//查询id为3的用户
GetRequest getRequest = new GetRequest("isgsg_index", "3");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
Map<String, Object> map = getResponse.getSourceAsMap();//封装到map对象中
SearchRequest searchRequest = new SearchRequest("isgsg_index");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//构建搜索条件
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termQuery("name","黎明"))//必须是name为黎明的用户
.must(QueryBuilders.termQuery("age",20))//且年龄必须是20岁
.filter(QueryBuilders.termQuery("sex","male"))//过滤男性
.mustNot(QueryBuilders.termQuery("hight","178"));//且身高必须不能是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());
}