Elasticsearch Java客戶端官網(wǎng)地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-maven.html
1挚币、導入依賴
這里的版本號要與Elasticsearch版本一致摄职,后面測試需要用到fastjson砾嫉,所以也添加了fastjson依賴疾层。
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
依賴包下載完成以后崔挖,發(fā)現(xiàn)elasticsearch包的版本是7.9.3的
此時需要在pom.xml文件中的properties標簽下串塑,添加elasticsearch的版本
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.10.1</elasticsearch.version>
</properties>
重新下載依賴包以后碱工,elasticsearch的版本已經(jīng)改成7.10.1了
2娃承、新建ElasticSearch配置類
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;
/**
* @author ZhaoBW
* @version 1.0
* @date 2021/1/22 16:21
*/
@Configuration
public class ElasticSearchConfig {
@Bean
public RestHighLevelClient esClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("192.168.43.129", 9200, "http")));
return client;
}
}
3、測試新增
test包下新建測試類ESTest
import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
/**
* @author ZhaoBW
* @version 1.0
* @date 2021/1/22 16:32
*/
@SpringBootTest
public class ESTest {
@Autowired
private RestHighLevelClient client;
/**
* 測試存儲數(shù)據(jù)到Elasticsearch中
*/
@Test
void saveData() throws IOException {
//構造index請求
IndexRequest request = new IndexRequest("es_test");
//設置id怕篷,如果不傳历筝,則會自動生成一個id
request.id("2");
//第一種傳數(shù)據(jù)方法
// request.source("name","張三","age",18,"gender","男");
//第二種,將對象轉為json字符串作為傳輸內容
User user = new User();
user.setName("Jessie");
user.setAge(18);
user.setGender("男");
String userString = JSON.toJSONString(user);
request.source(userString, XContentType.JSON);
IndexResponse index = client.index(request, RequestOptions.DEFAULT);
System.out.println(index);
}
@Data
class User{
String name;
String gender;
int age;
}
}
打印結果
IndexResponse[index=es_test,type=_doc,id=2,version=1,result=created,seqNo=1,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}]
使用Kibana查看Elasticsearch中es_test索引下的數(shù)據(jù)
4廊谓、查詢數(shù)據(jù)
@Test
void getData() throws IOException {
//第一個參數(shù)是index漫谷,第二個參數(shù)是id
GetRequest getRequest = new GetRequest("es_test","2");
GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(response);
}
運行結果:
{"_index":"es_test","_type":"_doc","_id":"2","_version":1,"_seq_no":1,"_primary_term":1,"found":true,"_source":{"age":18,"gender":"男","name":"Jessie"}}
更多增刪改查操作,請參考官方文檔:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-supported-apis.html