Java使用elasticsearch基础API使用案例讲解

1.依赖
我用的是 springboot 2.2.5.RELEASE 版本,这里只贴出主要依赖:

org.springframework.bootspring-boot-starter-data-elasticsearch

2.配置+测试源码
这里根据elasticsearch服务端的地址进行配置:
@Configurationpublic class ElasticSearchConfig {@Beanpublic RestHighLevelClient restHighLevelClient() {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))); return client; }}

以下是详细的测试代码:
@SpringBootTestclass EsApiApplicationTests { @Autowired @Qualifier(value = "https://www.it610.com/article/restHighLevelClient") private RestHighLevelClient client; // 1.创建索引 @Test void createIndex() throws IOException {// 创建请求CreateIndexRequest request = new CreateIndexRequest("yz_index"); // 执行请求,获取响应CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); System.out.println("creatIndex--" + response); } // 2.获取索引,判断是否存在 @Test void getIndex() throws IOException {// 创建请求GetIndexRequest request = new GetIndexRequest("yz_index"); // 执行请求,获取响应boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println("getIndex--exists--" + exists); } // 3.删除索引 @Test void deleteIndex() throws IOException {// 创建请求DeleteIndexRequest request = new DeleteIndexRequest("yz_index"); // 执行请求AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT); System.out.println("deleteIndex--" + response); } // 4.添加文档 @Test void addDocument() throws IOException {// 创建对象User user = new User("Java虚拟机", 30); // 创建索引,即获取索引IndexRequest indexRequest = new IndexRequest("yz_index"); // 添加规则 /index/_doc/idindexRequest.id("1"); indexRequest.timeout(TimeValue.timeValueSeconds(1)); // 存入对象indexRequest.source(JSON.toJSONString(user), XContentType.JSON); // 发送请求IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); System.out.println("addDocument--" + response); } // 5.获取文档是否存在 @Test void getDocument() throws IOException {// 创建get请求GetRequest getRequest = new GetRequest("yz_index", "1"); // 不获取返回的_source的上下文getRequest.fetchSourceContext(new FetchSourceContext(false)); getRequest.storedFields("_none_"); // 发送请求boolean exists = client.exists(getRequest, RequestOptions.DEFAULT); System.out.println("addDocument--" + exists); // 6.获取文档信息if (exists) {// 创建请求GetRequest getRequest1 = new GetRequest("yz_index", "1"); // 发送请求GetResponse response = client.get(getRequest1, RequestOptions.DEFAULT); System.out.println("source--" + response.getSource()); System.out.println("getDocument--" + response.getSourceAsString()); }// 7.更新文档信息if (exists) {// 创建请求UpdateRequest updateRequest = new UpdateRequest("yz_index", "1"); updateRequest.timeout("1s"); // 修改内容User user = new User("小米", 10); updateRequest.doc(JSON.toJSONString(user), XContentType.JSON); UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT); System.out.println("updateDocument--" + response.status()); }// 8.删除文档信息if (exists) {// 创建请求DeleteRequest deleteRequest = new DeleteRequest("yz_index", "1"); deleteRequest.timeout("1s"); // 修改内容DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println("deleteDocument--" + response.status()); } } // 9.批量添加文档 @Test void batchAddDocument() throws IOException {// 批量请求BulkRequest bulkRequest = new BulkRequest(); bulkRequest.timeout("10s"); // 创建对象ArrayList userArrayList = new ArrayList<>(); userArrayList.add(new User("小米1", 1)); userArrayList.add(new User("小米2", 2)); userArrayList.add(new User("小米3", 3)); userArrayList.add(new User("小米4", 4)); userArrayList.add(new User("小米5", 5)); // 添加请求for (int i = 0; i < userArrayList.size(); i++) {bulkRequest.add(new IndexRequest("yz_index").id("" + (i + 2)).source(JSON.toJSONString(userArrayList.get(i)), XContentType.JSON)); }// 执行请求BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println("batchAddDocument--" + response.status()); } // 10.查询 @Test void search() throws IOException {// 创建请求SearchRequest searchRequest = new SearchRequest("jd_index"); // 构建搜索条件SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "杜伽"); // 精确查询searchSourceBuilder.query(termQueryBuilder); searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); searchRequest.source(searchSourceBuilder); // 执行请求SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); // 解析查询结果System.out.println("search--getHists--" + JSON.toJSONString(response.getHits())); for (SearchHit documentFields : response.getHits()) {System.out.println("search--getHist--" + documentFields.getSourceAsMap()); } }}

【Java使用elasticsearch基础API使用案例讲解】 到此这篇关于Java使用elasticsearch基础API使用案例讲解的文章就介绍到这了,更多相关Java使用elasticsearch基础API内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读