elasticsearch
的Java
客户端目前有两类
(1)TransportClient
为ES原生的客户端,不能执行原生的DSL
语句必须使用它的Java Api
方法;
(2)以Rest Api
的missing client
,最典型的是jest
。这种客户端可直接使用DSL
语句拼接指定字符串,直接传送给服务端,返回给json
字符串再解析。
elasticsearch官网宣布计划在7.0以后的版本中废除TransportClient,以RestClient为主!
一、Jest依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.5.1</version>
</dependency>
二、配置application.properties
spring.elasticsearch.jest.uris=http://192.168.43.111:9200
三、测试代码
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class GmallServiceApplicationTests {
@Autowired
JestClient jestClient;
@Test
public void testEs() throws IOException {
String query="{\n" +
" \"query\": {\n" +
" \"match\": {\n" +
" \"actorList.name\": \"张译\"\n" +
" }\n" +
" }\n" +
"}";
Search search = new Search.Builder(query).addIndex("movie_chn").addType("movie_type_chn").build();
SearchResult result = jestClient.execute(search);
List<SearchResult.Hit<HashMap, Void>> hits = result.getHits(HashMap.class);
for (SearchResult.Hit<HashMap, Void> hit : hits) {
HashMap source = hit.source;
System.err.println("source = " + source);
}
}
}
控制台打印(成功)
kibana控制台测试
☝上述分享来源个人总结,如果分享对您有帮忙,希望您积极转载;如果您有不同的见解,希望您积极留言,让我们一起探讨,您的鼓励将是我前进道路上一份助力,非常感谢!我会不定时更新相关技术动态,同时我也会不断完善自己,提升技术,希望与君同成长同进步!
☞本人博客:https://coding0110lin.blog.csdn.net/ 欢迎转载,一起技术交流吧!