一、使用步骤
1.加入依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.13.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
2.创建配置类
package com.it.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 ElasticClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
return restHighLevelClient;
}
}
3.测试
package com.it;
import com.alibaba.fastjson.JSON;
import com.it.mapper.JprecommendMapper;
import com.it.pojo.Jprecommend;
import com.it.pojo.User;
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.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
@SpringBootTest
public class Expert_lecture_ApplicationTest {
@Resource
private RestHighLevelClient restHighLevelClient;
@Resource
private JprecommendMapper jprecommendMapper;
@Test
void createIndexTest() throws IOException {
CreateIndexRequest firstIndex = new CreateIndexRequest("jprecommend");
CreateIndexResponse response = restHighLevelClient.indices().create(firstIndex, RequestOptions.DEFAULT);
System.out.println(response);
}
@Test
void deleteIndexTest() throws IOException {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("jprecommend");
AcknowledgedResponse result = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSON(result));
}
@Test
void addDocumentTest() throws IOException {
User user = new User();
user.setId(1);
user.setCreateTime(new Date());
user.setUAge(21);
user.setUSex("男");
IndexRequest indexRequest = new IndexRequest("jprecommend");
indexRequest.id("1");
indexRequest.timeout("1s");
indexRequest.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(index));
}
@Test
void getDocumentTest() throws IOException {
GetRequest getRequest = new GetRequest("jprecommend", "1");
boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
if (!exists) {
System.out.println(">>>>>>>> 当前索引:jprecommend对应的id:1 不存在");
return;
}
System.out.println(">>>>>>>> 当前索引:jprecommend对应的id:1 存在");
GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
System.out.println(JSON.toJSONString(response));
}
@Test
void updateDocmentTest() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("jprecommend", "1");
updateRequest.timeout("1s");
User user = new User();
user.setId(1);
user.setCreateTime(new Date());
user.setUAge(21);
user.setUSex("男");
updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(updateResponse));
}
@Test
void deleteDocmentTest() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("jprecommend", "1");
DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(response));
}
@Test
void addDocmentByBatchTest() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("2s");
List<Jprecommend> jprecommends = jprecommendMapper.selectList(null);
for (int i = 0; i < jprecommends.size(); i++) {
String data = JSON.toJSONString(jprecommends.get(i));
IndexRequest indexRequest = new IndexRequest("jprecommend").source(data, XContentType.JSON);
bulkRequest.add(indexRequest);
}
BulkResponse responses = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(responses.hasFailures());
System.out.println(JSON.toJSONString(responses));
}
@Test
void searchTest() throws IOException {
SearchRequest searchRequest = new SearchRequest("jprecommend");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "句");
sourceBuilder.query(termQueryBuilder);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(response));
System.out.println(JSON.toJSONString(response.getHits().getHits()));
}
}