Elasticsearch Java客户端官网地址: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>
依赖包下载完成以后,发现elasticsearch包的版本是7.9.3的
此时需要在pom.xml文件中的properties标签下,添加elasticsearch的版本
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.10.1</elasticsearch.version>
</properties>
重新下载依赖包以后,elasticsearch的版本已经改成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;
/**
* 测试存储数据到Elasticsearch中
*/
@Test
void saveData() throws IOException {
//构造index请求
IndexRequest request = new IndexRequest("es_test");
//设置id,如果不传,则会自动生成一个id
request.id("2");
//第一种传数据方法
// 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索引下的数据
4、查询数据
@Test
void getData() throws IOException {
//第一个参数是index,第二个参数是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