一 SpringBoot + Elasticsearch 項(xiàng)目環(huán)境搭建
1.1 修改pom文件添加依賴
目前使用spring-boot-starter-parent版本為2.2.8.RELEASE
對(duì)應(yīng)spring-data-elasticsearch版本為2.2.8.RELEASE,版本對(duì)應(yīng)可以自行百度,如果不行直接用elasticsearch-rest-high-level-client工具類吧文章來源:http://www.zghlxwxcb.cn/news/detail-733729.html
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.5.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.5.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.5.0</version>
</dependency>
1.2 新建配置文件
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;
/**
* ES配置類
*
* @author lc
* @version 1.0
* @date 2022/3/25 10:53
*/
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.1.100", 9200, "http")));
return client;
}
}
二 RestHighLevelClient的使用
RestHighLevelClient是Elasticsearch 的操作方法,我們先進(jìn)行引用吧。文章來源地址http://www.zghlxwxcb.cn/news/detail-733729.html
@Autowired
private RestHighLevelClient client;
1、創(chuàng)建索引
@Test
void testCreateIndex() throws IOException {
//1 創(chuàng)建索引請(qǐng)求
CreateIndexRequest request = new CreateIndexRequest("zlc_index");
//2 客戶端執(zhí)行請(qǐng)求
CreateIndexResponse createIndexResponse =
client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
2、索引是否存在
@Test
void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("zlc_index");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
3、刪除索引
@Test
void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("zlc_index");
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
4、添加文檔
@Test
void testAddDocument() throws IOException {
//創(chuàng)建對(duì)象
UserES user = new UserES();
user.setUserName("suwerw");
user.setUserPhone("178245774");
//創(chuàng)建請(qǐng)求
IndexRequest request = new IndexRequest("zlc_index");
//規(guī)則 put /zlc_index/_doc/1
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));
request.timeout("1s");
//將數(shù)據(jù)放入請(qǐng)求
request.source(JSON.toJSONString(user), XContentType.JSON);
//客戶端發(fā)送請(qǐng)求,獲取響應(yīng)結(jié)果
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
System.out.println(indexResponse.status());
}
5、判斷文檔是否存在
@Test
void testIsExists() throws IOException {
GetRequest getRequest = new GetRequest("zlc_index", "1");
//不獲取返回的 _source 的上下文,提高效率
getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
6、獲取文檔
@Test
void testGetDocument() throws IOException {
GetRequest getRequest = new GetRequest("zlc_index", "1");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(getResponse);
System.out.println(getResponse.getSourceAsString());
}
7、更新文檔信息
@Test
void testUpdateDocument() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("zlc_index", "1");
updateRequest.timeout("1s");
UserES user = new UserES();
user.setUserName("Zhou_LC");
user.setUserPhone("233669");
updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(updateResponse);
System.out.println(updateResponse.status());
}
8、刪除文檔
@Test
void testDeleteDocument() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("zlc_index", "1");
deleteRequest.timeout("1s");
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(deleteResponse);
System.out.println(deleteResponse.status());
}
到了這里,關(guān)于使用springboot對(duì)Elasticsearch 進(jìn)行索引的增、刪、改、查的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!