SpringBoot+HighRestClient(7.17.6版本)
1.添加依赖
在pom.xml中添加HighRestClient依赖。
<properties>
<java.version>1.8</java.version>
<rest.client.version>7.17.6</rest.client.version>
</properties>
...
<dependencies>
<!--es客户端-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${rest.client.version}</version>
<!--排除掉原有的版本-->
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${rest.client.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${rest.client.version}</version>
</dependency>
<!--糊涂工具库的依赖,帮我实现很多工具功能-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
</dependencies>
2.在application.yml配置Elasticsearch
spring:
#elasticsearch的配置
elasticsearch:
rest:
# es 连接地址,多个用逗号隔开
uris: 192.168.6.23:9200
# 连接超时时间,单位毫秒,默认1s
connection-timeout: 1000
# 读取超时时间,单位毫秒,默认30s
read-timeout: 1000
3.定义接口和接口实现类
MovieEsService.java
public interface MovieEsService {
//给ES当中保存电影索引。
public boolean saveMovie(Movie movie) throws Exception;
public boolean updateMovie(Movie movie) throws Exception;
//根据电影ID查询电影资料
public Movie queryMovieById(String id);
public boolean removeMovie(String id) throws Exception;
}
MovieEsServiceImpl.java
@Service
public class MovieEsServiceImpl implements MovieEsService {
@Resource
private RestHighLevelClient restHighLevelClient;
@Override
public boolean saveMovie(Movie movie) throws Exception {
try {
IndexRequest indexRequest = new IndexRequest("movie");
indexRequest.id(movie.getId() + "");
//咱们使用hutool工具类把一个java对象转成json字符串。
indexRequest.source(JSONUtil.toJsonStr(movie), XContentType.JSON);
indexRequest.timeout("1s");
IndexResponse indexResponse = restHighLevelClient.index(indexRequest,
RequestOptions.DEFAULT);
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
@Override
public boolean updateMovie(Movie movie) throws Exception {
IndexRequest indexRequest = null;
IndexResponse indexResponse = null;
try {
indexRequest = new IndexRequest("movie", "_doc", movie.getId() + "");
String jsonStr = JSONUtil.toJsonStr(movie);
indexRequest.source(jsonStr, XContentType.JSON);
indexResponse = null;
indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
public Movie queryMovieById(String id) {
GetRequest request = new GetRequest("movie", id);
try {
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
Map<String, Object> result = response.getSource();
Movie m = JSONUtil.toBean(JSONUtil.toJsonStr(result), Movie.class);
//System.out.println(m);
return m;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
@Override
public boolean removeMovie(String id) throws Exception {
DeleteRequest request = new DeleteRequest("movie", id);
try {
DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
//System.out.println(response.getResult());
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
}
4.单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class MovieEsServiceTest {
@Resource
private MovieEsService movieEsService;
@Test
public void testSaveMovie() throws Exception {
Movie m = new Movie();
m.setId(3);
m.setName("英雄");
m.setDirector("张艺谋");
m.setIntroduce("荆轲刺秦王");
m.setDuration(200);
m.setType("动作");
m.setPlayDate("2002-10-10");
boolean flag = movieEsService.saveMovie(m);
Assert.isTrue(flag, "");
}
@Test
public void testQueryMovieById() {
Movie m = movieEsService.queryMovieById("1");
System.out.println(m);
}
@Test
public void testRemoveMovie() throws Exception {
boolean flag = movieEsService.removeMovie("1");
Assert.isTrue(flag, "");
}
@Test
public void testUpdateMovie() throws Exception {
Movie m = movieEsService.queryMovieById("2");
m.setName("功夫2");
boolean flag = movieEsService.updateMovie(m);
Assert.isTrue(flag, "");
}
}