一、Elasticsearch簡介
Elasticsearch是一個基於Lucene的分散式搜索引擎,能實現近實時的全文搜索並具有以下特點:
1、分散式搜索引擎,數據可以分片存儲在集群中,提供水平擴展能力;
2、支持多種數據格式,包括結構化和非結構化數據;
3、提供豐富的RESTful API,使用簡單方便;
4、提供機器學習和數據分析的功能。
因此,Elasticsearch在企業搜索、日誌分析、安全日誌分析、異常檢測、物聯網等領域得到了廣泛的應用。
二、Java連接Elasticsearch
Elasticsearch提供Java API,可以很方便地在Java應用中使用Elasticsearch。
1、Maven依賴
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.12.1</version>
</dependency>
這裡使用了最新版本的elasticsearch7.12.1。
2、連接Elasticsearch
public static RestHighLevelClient createClient() {
// 確認es集群節點地址
String hostname = "localhost";
RestClientBuilder builder = RestClient.builder(new HttpHost(hostname, 9200, "http"));
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
這裡連接了本地的Elasticsearch集群,默認地址為localhost:9200。
3、CRUD操作
Java API提供了與RESTful API對應的介面,可以進行CRUD操作。
a)創建索引
// 創建索引
public static void createIndex(RestHighLevelClient client, String index) throws IOException {
// 創建索引請求
CreateIndexRequest request = new CreateIndexRequest(index);
// 客戶端執行請求
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println("創建索引 + " + index + " " + response.isAcknowledged());
}
這裡創建了一個名為index的索引。
b)刪除索引
// 刪除索引
public static void deleteIndex(RestHighLevelClient client, String index) throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest(index);
// 客戶端執行請求
AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println("刪除索引 + " + index + " " + response.isAcknowledged());
}
這裡刪除了名為index的索引。
c)添加文檔
// 添加文檔
public static void addDoc(RestHighLevelClient client, String index, String id, String source) throws IOException {
IndexRequest request = new IndexRequest(index);
request.id(id);
request.source(source, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println("添加文檔 + " + id + " " + response.getResult());
}
這裡向名為index的索引中添加了一條id為1的文檔。
d)獲取文檔
// 獲取文檔
public static void getDoc(RestHighLevelClient client, String index, String id) throws IOException {
// 獲取文檔請求
GetRequest getRequest = new GetRequest(index, id);
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println("獲取文檔 + " + id + " " + getResponse.getSourceAsString());
}
這裡獲取了名為index的索引中id為1的文檔。
e)更新文檔
// 更新文檔
public static void updateDoc(RestHighLevelClient client, String index, String id, String source) throws IOException {
UpdateRequest request = new UpdateRequest(index, id);
request.doc(source, XContentType.JSON);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println("更新文檔 + " + id + " " + response.getResult());
}
這裡修改了名為index的索引中id為1的文檔。
f)刪除文檔
// 刪除文檔
public static void deleteDoc(RestHighLevelClient client, String index, String id) throws IOException {
DeleteRequest request = new DeleteRequest(index, id);
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println("刪除文檔 + " + id + " " + response.getResult());
}
這裡刪除了名為index的索引中id為1的文檔。
三、與Spring Boot集成
當使用Spring Boot開發應用時,使用Elasticsearch也可以很方便地集成。
1、Maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
這裡使用了Spring Boot提供的spring-boot-starter-data-elasticsearch。
2、配置文件
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
spring.data.elasticsearch.cluster-name=my-application
這裡配置了連接localhost:9300的Elasticsearch集群,集群名稱為my-application。
3、實體類
@Document(indexName = "index", type = "doc")
public class Doc {
@Id
private String id;
private String name;
private Integer age;
// getter and setter
}
這裡定義了一個名為index的索引,對應的實體為Doc。
4、ElasticsearchRepository
public interface DocRepository extends ElasticsearchRepository<Doc, String> {
}
這裡定義了一個繼承自ElasticsearchRepository的DocRepository介面,該介面提供了多種CRUD操作。
5、CRUD操作
使用Spring Boot集成Elasticsearch後,使用ElasticsearchRepository介面提供的方法可以對Elasticsearch進行CRUD操作。
a)添加文檔
@Autowired
private DocRepository docRepository;
Doc doc = new Doc();
doc.setId("1");
doc.setName("Tom");
doc.setAge(20);
docRepository.save(doc);
這裡向名為index的索引中添加了一條id為1的文檔。
b)獲取文檔
Doc doc = docRepository.findById("1").orElse(null);
if(doc != null){
System.out.println(doc.getName() + " " + doc.getAge());
}
這裡獲取了名為index的索引中id為1的文檔。
c)更新文檔
Doc doc = docRepository.findById("1").orElse(null);
if(doc != null){
doc.setAge(21);
docRepository.save(doc);
}
這裡修改了名為index的索引中id為1的文檔的age欄位。
d)刪除文檔
docRepository.deleteById("1");
這裡刪除了名為index的索引中id為1的文檔。
四、總結
Java連接Elasticsearch是一項在實際應用中很重要的功能,通過本文的介紹,我們可以清晰地了解到Java與Elasticsearch的連接方式,以及在Java應用中實現CRUD操作的方法。
原創文章,作者:SZSKG,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/369744.html