1.es配置
package com.nt.es.esapi.config; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ElasticSearchClinetConfig { @Bean public RestHighLevelClient restHighLevelClient(){ RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("10.10.49.90",9200,"http"), new HttpHost("10.10.49.91",9200,"http") ) ); return client; } }
2.user对象
package com.nt.es.esapi.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.stereotype.Component; @Data @AllArgsConstructor @NoArgsConstructor @Component public class User { private String name; private int age; }
3.es api测试
package com.nt.es.esapi; import com.alibaba.fastjson.JSON; import com.nt.es.esapi.mapper.ServOverTimeMapper; import com.nt.es.esapi.pojo.ESBean; import com.nt.es.esapi.pojo.OverTimeWarnConfig; import com.nt.es.esapi.pojo.User; import com.nt.es.esapi.utils.ESconst; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.*; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @SpringBootTest class EsApiApplicationTests { @Autowired private RestHighLevelClient restHighLevelClient; //索引的创建 Request请求 @Test void testCreateIndex() throws IOException { //1.创建索引请求 CreateIndexRequest request = new CreateIndexRequest(ESconst.ES_INDEX); //2.客户端执行创建请求,获取响应 CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); System.out.println(createIndexResponse); } //获取索引,判断是否存在 @Test void testExistIndex() throws IOException{ GetIndexRequest request = new GetIndexRequest(ESconst.ES_INDEX); boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); } //删除索引 @Test void testDelIndex() throws IOException{ DeleteIndexRequest request = new DeleteIndexRequest("test_index"); AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT); System.out.println(delete.isAcknowledged()); } //添加文档 @Test void testAddDocunment() throws IOException { //创建对象 User user = new User("花花haha",4); //创建请求 IndexRequest request = new IndexRequest(ESconst.ES_INDEX); //规则 put/index/_doc/1 request.id("1"); request.timeout(TimeValue.timeValueSeconds(1)); //将数据放入请求 json request.source(JSON.toJSONString(user), XContentType.JSON); //客户端发送请求,获取响应的结果 IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT); System.out.println(index.toString()); System.out.println(index.status()); } //获取文档是否存在 get /index/_doc/1 @Test void testIsExists() throws IOException { GetRequest request = new GetRequest(ESconst.ES_INDEX, "1"); //不获取返回的上下文 request.fetchSourceContext(new FetchSourceContext(false)); boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT); System.out.println(exists); } //获取文档信息 @Test void testGetDocument() throws IOException { GetRequest request = new GetRequest(ESconst.ES_INDEX, "1"); GetResponse getResponse = restHighLevelClient.get(request, RequestOptions.DEFAULT); System.out.println(getResponse.getSourceAsString()); System.out.println(getResponse); } //更新文档记录 @Test void testUpdataDocument() throws IOException { UpdateRequest request = new UpdateRequest(ESconst.ES_INDEX, "1"); request.timeout("1s"); User user = new User("绿绿", 20); request.doc(JSON.toJSONString(user),XContentType.JSON); UpdateResponse update = restHighLevelClient.update(request, RequestOptions.DEFAULT); System.out.println(update.status()); } //删除文档记录 @Test void testDelDocument() throws IOException{ DeleteRequest request = new DeleteRequest(ESconst.ES_INDEX, "2"); request.timeout("1s"); DeleteResponse delete = restHighLevelClient.delete(request, RequestOptions.DEFAULT); System.out.println(delete.status()); } //批量插入数据 @Test void testBulkRequest() throws IOException{ BulkRequest bulkRequest = new BulkRequest(); bulkRequest.timeout("10s"); ArrayList<User> userList = new ArrayList<>(); userList.add(new User( "huahua", "11", )); userList.add(new User( "lvlv", "12", )); //批处理 for (int i = 0; i < userList.size(); i++) { bulkRequest.add(new IndexRequest(ESconst.ES_INDEX) // .id(""+(i+4)) .source(JSON.toJSONString(userList.get(i)),XContentType.JSON) ); } BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulk.hasFailures()); //是否失败 false代表成功 } //批量查询 @Test void testSearch() throws IOException { SearchRequest searchRequest = new SearchRequest(ESconst.ES_INDEX); //构建搜索条件,使用QueryBuilders工具类实现 //QueryBuilders.matchAllQuery() 查询所有 //QueryBuilders.termQuery() 精确查询 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery(); // TermQueryBuilder duration = QueryBuilders.termQuery("error", false); searchSourceBuilder.query(matchAllQueryBuilder); // searchSourceBuilder.from(); // searchSourceBuilder.size(); 分页 searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); searchRequest.source(searchSourceBuilder); SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); // System.out.println( // JSON.toJSONString(search.getHits()) // ); System.out.println("======================"); for (SearchHit hit : search.getHits().getHits()) { System.out.println(hit.getSourceAsMap()); } } }