1.注入RestHighLevelClient
@Configuration
public class ESconfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
//配置自己的本地接口(仓库)
return new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));
}
}
2.创建索引
public void testCreatIndex() throws IOException {//创建索引
CreateIndexRequest mb_index = new CreateIndexRequest("mb_index");
CreateIndexResponse indexResponse = restHighLevelClient.indices().create(mb_index, RequestOptions.DEFAULT);
System.out.println(indexResponse);
}
3.判断索引存不存在
public void existIndex() throws Exception{//判断索引存不存在
GetIndexRequest mb_index = new GetIndexRequest("mb_index");
boolean mb_index1 = restHighLevelClient.indices().exists(mb_index,RequestOptions.DEFAULT);
System.out.println(mb_index1);//true or false,这里返回的是true,表明存在
}
4.删除索引
public void deleteIndex() throws Exception{//删除索引
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("mb_index");
AcknowledgedResponse response = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
}
5.添加文档
public void addDocument() throws Exception{//添加文档
User user = new User("张三", 18, "12");
IndexRequest indexRequest = new IndexRequest("mb_index");
//indexRequest.id("1");//不自己定义id则会生成随机id
//默认type(表)为_doc
indexRequest.source(JSON.toJSONString(user),XContentType.JSON);
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());//json具体信息
System.out.println(indexResponse.status());//状态
}
6.判断文档是否存在
public void existDocument() throws Exception{//判断文档是否存在
GetRequest getRequest = new GetRequest("mb_index","1");//type默认为_doc
//跟据文档编号查看是否存在文档
boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
7.跟据编号获取文档
public void getDocument() throws Exception{//跟据编号获取文档
GetRequest mb_index = new GetRequest("mb_index", "1");
GetResponse documentFields = restHighLevelClient.get(mb_index, RequestOptions.DEFAULT);
System.out.println(documentFields.getSourceAsString());//返回是是json对象
System.out.println(documentFields.getSource());
System.out.println(documentFields.getSource().get("age"));
}
8.修改文档
public void updateDocument() throws Exception{//修改文档
UpdateRequest updateRequest = new UpdateRequest("mb_index","1");
User user = new User("李四", 18, "12");
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(updateResponse.toString());
System.out.println(updateResponse.getResult());
}
9.删除文档
public void deleteDocument() throws Exception{//删除文档
DeleteRequest deleteRequest = new DeleteRequest("mb_index","1");
DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(deleteResponse.status());//ok or not_found
}
10.批量操作
public void bulkAddDocument() throws Exception{//批量操作
BulkRequest bulkRequest = new BulkRequest();
IndexRequest indexRequest = new IndexRequest("mb_index");
List<User> list = new ArrayList<>();
list.add(new User("小红1",18,"1"));
list.add(new User("小红",18,"2"));
list.add(new User("小华",29,"3"));
for (User user : list) {
bulkRequest.add(new IndexRequest("mb_index").id(user.getId()).source(JSON.toJSONString(user),XContentType.JSON));
}
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.hasFailures());//成功:false
}
11.搜索
public void search() throws Exception{//搜索
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("name","小"));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//System.out.println(searchResponse.toString());
System.out.println(JSON.toJSONString(searchResponse.getHits().getHits()));
System.out.println(JSON.toJSONString(searchResponse.getHits().toString()));
}