ElasticSearch就一个核心依赖,这里引入阿里的fastjson是因为ES只接受json类型的数据,不接受对象。
集成ES并使用 文章使用的连接对象org.springframework.boot spring-boot-starter-data-elasticsearch com.alibaba fastjson 1.2.33
ES Client的官方文档有两套,一套低版本,一套高版本。两个版本使用的方式不同。以下使用的高版本
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));
return client;
}
}
索引测试代码
@SpringBootTest
public class ESIndexTest {
@Autowired
@Qualifier("restHighLevelClient")//配置类中的方法名
private RestHighLevelClient client;
@Test
public void testCreateIndex() throws IOException {
// 创建索引请求
CreateIndexRequest request = new CreateIndexRequest("index_test1");
// 客户端执行请求
IndicesClient indices = client.indices();
CreateIndexResponse response = indices.create(request, RequestOptions.DEFAULT);
System.out.println(response);
}
@Test
public void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("index_test1");
IndicesClient indices = client.indices();
boolean exists = indices.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
@Test
public void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("index_test1");
IndicesClient indices = client.indices();
AcknowledgedResponse response = indices.delete(request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
}
}
文档测试代码
@SpringBootTest
public class ESdocumentTest {
@Autowired
// @Qualifier("restHighLevelClient") 变量名跟方法名一致时即可不指定
private RestHighLevelClient restHighLevelClient;
@Test
public void testAdddocument() throws IOException {
// 构造插入ES的对象
User user = new User("evader", 21);
// 创建请求
IndexRequest request = new IndexRequest("document_test");
// 设置请求参数
request.id("1");
request.timeout(Timevalue.timevalueSeconds(1));
// 将要插入的对象放入请求
request.source(JSON.toJSONString(user), XContentType.JSON);
// 发送请求并获取响应结果
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
System.out.println(response.status());
}
@Test
public void testExistsDocu() throws IOException {
GetRequest request = new GetRequest("document_test", "1");
// 设置不返回_source
request.fetchSourceContext(new FetchSourceContext(false));
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
@Test
public void testGetDocu() throws IOException {
GetRequest request = new GetRequest("document_test", "1");
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
System.out.println(response);
System.out.println(response.getVersion());
System.out.println(response.getSourceAsString());
}
@Test
public void testUpdateDocu() throws IOException {
UpdateRequest request = new UpdateRequest("document_test", "1");
request.timeout("1s");
User user = new User("evader1997", 24);
request.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
@Test
public void testDeleteDocu() throws IOException {
DeleteRequest request = new DeleteRequest("document_test", "1");
request.timeout("1s");
DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
}



