1. elasticSearch配置类
@Bean public RestHighLevelClient restHighLevelClient(){ RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("127.0.0.1",9200,"http") ) ); return client; }2. 测试es基本操作命令
@Autowired private RestHighLevelClient restHighLevelClient; /** * 创建索引 * @throws IOException */ @Test void testCreateIndex() throws IOException { //1、创建索引请求 CreateIndexRequest request = new CreateIndexRequest("test_index"); // 2、客户端执行请求 CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); System.out.println(createIndexResponse); } /** * 获取索引 * @throws IOException */ @Test void testExistIndex() throws IOException { GetIndexRequest request = new GetIndexRequest("test_index"); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); } /** * 删除索引 * @throws IOException */ @Test void testDeleteIndex() throws IOException { DeleteIndexRequest request = new DeleteIndexRequest("test_index"); AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT); System.out.println(response); } /** * 添加文档 * @throws IOException */ @Test void testAddDocument() throws IOException { UserEntity userEntity = new UserEntity("小米",3); //创建请求 IndexRequest request = new IndexRequest("test_index"); // 规则 put /kuang_index/_doc/1 request.id("1"); request.timeout(TimeValue.timeValueSeconds(1)); request.timeout("1s"); //添加数据 request.source(JSON.toJSONString(userEntity), XContentType.JSON); //客户端发送请求 IndexResponse response = client.index(request, RequestOptions.DEFAULT); System.out.println(response); } /** * 获取文档 */ @Test public void indexGetDocument() throws IOException { GetRequest getRequest = new GetRequest("test_index","1"); GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); System.out.println(getResponse.getSourceAsString()); } /** * 更新文档 */ @Test public void indexUpdateDocument() throws IOException { UpdateRequest updateRequest = new UpdateRequest("test_index","1"); updateRequest.timeout("1s"); UserEntity userEntity = new UserEntity("小仓",20); updateRequest.doc(JSON.toJSONString(userEntity),XContentType.JSON); UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT); System.out.println(updateResponse.status()); } /** * 删除文档 */ @Test public void indexDeleteDocument() throws IOException { DeleteRequest deleteRequest = new DeleteRequest("test_index", "1"); DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println(deleteResponse.status()); } /** * 批量插入数据 */ @Test public void addList() throws IOException { BulkRequest bulkRequest = new BulkRequest(); ArrayList<UserEntity> list = new ArrayList<>(); list.add(new UserEntity("xiaoming",19)); list.add(new UserEntity("xiaoming2",16)); list.add(new UserEntity("xiaoming3",12)); list.add(new UserEntity("xiaoming4",29)); list.add(new UserEntity("xiaoming5",14)); list.add(new UserEntity("xiaoming6",17)); //批量请求 for (int i = 0; i < list.size(); i++) { bulkRequest.add( new IndexRequest("test1") .id(""+(i+1)) .source(JSON.toJSONString(list.get(i)),XContentType.JSON) ); BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT); } } /** * 查询数据 */ @Test public void searchList() throws IOException { SearchRequest searchRequest = new SearchRequest("test1"); //构建搜索条件 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.highlighter(); //查询条件 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "xiaoming"); sourceBuilder.query(termQueryBuilder); searchRequest.source(sourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); System.out.println(JSON.toJSONString(searchResponse.getHits())); System.out.println("==========================="); for (SearchHit documentFields : searchResponse.getHits().getHits()) { System.out.println(documentFields.getSourceAsString()); } }