1、ES API的两种方式
Elasticsearch 的API 分为 REST Client API(http请求形式)以及 transportClientAPI两种。相比来说transportClient API效率更高,transportClient是通过Elasticsearch内部RPC的形式进行请求的,连接可以是一个长连接,相当于是把客户端的请求当成Elasticsearch 集群的一个节点, REST Client API 也支持httpd的keepAlive形式的长连接,只是非内部RPC形式。Elasticsearch 7 后就会移除transportClient,主要原因是transportClient 难以向下兼容版本。
1.1 9300[TCP]
利用9300端口的是spring-data-elasticsearch:transport-api.jar,但是这种方式因为对应的SpringBoot版本不一致,造成对应的transport-api.jar也不同,不能适配es的版本,而且ElasticSearch7.x中已经不推荐使用了,ElasticSearch 8之后更是废弃。
1.2 9200[HTTP]
基于9200端口的方式也有多种,详情如下:
JsetClient |
非官方,更新缓慢 |
RestTemplate |
模拟发送Http请求,ES很多的操作需要我们自己来封装,效率低 |
HttpClient |
与RestTemplate的情况一样 |
ElasticSearch-Rest-Client |
官方的RestClient,封装了ES的操作,API层次分明,易于上手 |
JavaAPIClient |
7.15版本后推荐 |
下面重点来看ElasticSearch-Rest-Client的构建。
2、ElasticSearch-Rest-Client的应用
2.1、添加依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.4.12</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>2.4.12</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.4.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> </dependencies>
2.2、添加ES配置
2.2.1、ES客户端配置
客户端配置如下:
import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Description: ElasticSearch的配置类 * @author: snails */ @Configuration public class ElasticSearchConfiguration { @Bean public RestHighLevelClient restHighLevelClient() { // 构建ES客户端 RestClientBuilder builder = RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")); RestHighLevelClient client = new RestHighLevelClient(builder); return client; } }
测试从Spring容器中获取client对象:
import org.elasticsearch.client.RestHighLevelClient; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; /** * @Description: * @author: snails */ @SpringBootTest public class ESApplicationTests { @Autowired private RestHighLevelClient client; @Test public void contextLoads() { System.out.println("====> " + client); } }
输出结果不为空,表示连接ES服务成功。
2.2.2、设置RequestOptions
在 ElasticSearchConfiguration 中添加如下内容:
private static final RequestOptions COMMON_OPTIONS; static { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); COMMON_OPTIONS = builder.build(); }
2.3、API使用
2.3.1、保存文档
@Test public void addDocument() throws IOException { // 创建索引对象 IndexRequest indexRequest = new IndexRequest("java_api"); // 设置id indexRequest.id("1"); // 创建文档对象 Student stu = new Student(); stu.setStuno("20130911"); stu.setAge(15); stu.setName("张三"); // 用Jackson中的对象转json数据 ObjectMapper objectMapper = new ObjectMapper(); String json = objectMapper.writeValueAsString(stu); indexRequest.source(json, XContentType.JSON); // 执行操作 IndexResponse index = client.index(indexRequest, ElasticSearchConfiguration.COMMON_OPTIONS); // 提取返回信息 System.out.println(index); } @Data class Student{ private String stuno; private String name; private Integer age; }
执行结果如下:
kibana查询结果如下:
2.3.2、检索文档
2.1、检索出items索引的所有文件
@Test public void testSearchIndexAllDoc() throws IOException { // 1.创建一个 SearchRequest 对象 SearchRequest searchRequest = new SearchRequest(); // 设置检索数据对应的索引库 searchRequest.indices("items"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); searchRequest.source(sourceBuilder); // 2.执行检索操作 SearchResponse response = client.search(searchRequest, ElasticSearchConfiguration.COMMON_OPTIONS); // 3.获取检索后的响应对象,解析出需要的数据 System.out.println("ElasticSearch检索的信息:"+response); }
执行结果如下:
2.2、根据address全文检索
@Test void searchIndexByAddress() throws IOException { // 1.创建一个 SearchRequest 对象 SearchRequest searchRequest = new SearchRequest(); // 设置检索数据对应的索引库 searchRequest.indices("items"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 查询出items下 address 中包含 586 的记录 sourceBuilder.query(QueryBuilders.matchQuery("address","586")); searchRequest.source(sourceBuilder); // 2.执行检索操作 SearchResponse response = client.search(searchRequest, ElasticSearchConfiguration.COMMON_OPTIONS); // 3.获取检索后的响应对象解析出需要的数据 System.out.println("ElasticSearch检索的信息:"+response); }
执行结果如下:
2.3、嵌套的聚合操作:检索出 items 下的年龄分布和每个年龄段的平均薪资
/** * 聚合:嵌套聚合 * @throws IOException */ @Test void searchIndexAggregation() throws IOException { // 1.创建一个 SearchRequest 对象 SearchRequest searchRequest = new SearchRequest(); // 设置检索数据对应的索引库 searchRequest.indices("items"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 查询出items下 所有的文档 sourceBuilder.query(QueryBuilders.matchAllQuery()); // 聚合 aggregation // 聚合item下年龄的分布和每个年龄段的平均薪资 AggregationBuilder aggregationBuiler = AggregationBuilders.terms("ageAgg") .field("age") .size(2); // 嵌套聚合 aggregationBuiler.subAggregation(AggregationBuilders.avg("balanceAvg").field("balance")); sourceBuilder.aggregation(aggregationBuiler); // 聚合时,不显示满足条件的文档内容 sourceBuilder.size(0); searchRequest.source(sourceBuilder); System.out.println(sourceBuilder); // 2.执行检索操作 SearchResponse response = client.search(searchRequest, ElasticSearchConfiguration.COMMON_OPTIONS); // 3.获取检索后的响应对象解析出需要的数据 System.out.println(response); }
执行结果如下:
2.4、并行的聚合操作:查询出items下年龄段的分布和总的平均薪资
/** * 聚合 * @throws IOException */ @Test void searchIndexAggregation02() throws IOException { // 1.创建一个 SearchRequest 对象 SearchRequest searchRequest = new SearchRequest(); // 设置检索数据对应的索引库 searchRequest.indices("items"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 查询出items下 所有的文档 sourceBuilder.query(QueryBuilders.matchAllQuery()); // 聚合 aggregation // 聚合items下年龄的分布和平均薪资 AggregationBuilder aggregationBuiler = AggregationBuilders.terms("ageAgg") .field("age") .size(2); sourceBuilder.aggregation(aggregationBuiler); // 聚合平均年龄 AvgAggregationBuilder balanceAggBuilder = AggregationBuilders.avg("balanceAgg").field("age"); sourceBuilder.aggregation(balanceAggBuilder); // 聚合时,不显示满足条件的文档内容 sourceBuilder.size(0); searchRequest.source(sourceBuilder); System.out.println(sourceBuilder); // 2.执行检索操作 SearchResponse response = client.search(searchRequest, ElasticSearchConfiguration.COMMON_OPTIONS); // 3.获取检索后的响应对象解析出需要的数据 System.out.println(response); }
执行结果如下:
2.5、处理检索后的结果
@Test void searchIndexResponse() throws IOException { // 1.创建一个 SearchRequest 对象 SearchRequest searchRequest = new SearchRequest(); // 设置检索数据对应的索引库 searchRequest.indices("items"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 查询出bank下 address 中包含 586 的记录 sourceBuilder.query(QueryBuilders.matchQuery("address","586")); searchRequest.source(sourceBuilder); // 2.执行检索操作 SearchResponse response = client.search(searchRequest, ElasticSearchConfiguration.COMMON_OPTIONS); // 3.解析数据 RestStatus status = response.status(); TimeValue took = response.getTook(); SearchHits hits = response.getHits(); TotalHits totalHits = hits.getTotalHits(); TotalHits.Relation relation = totalHits.relation; long value = totalHits.value; // 相关性的最高分 float maxScore = hits.getMaxScore(); SearchHit[] hits1 = hits.getHits(); for (SearchHit documentFields : hits1) { String json = documentFields.getSourceAsString(); // JSON字符串转换为 Object对象 ObjectMapper mapper = new ObjectMapper(); Item item = mapper.readValue(json, Item.class); System.out.println("item = " + item); } } @ToString @Data static class Item { private int account_number; private int balance; private String firstname; private String lastname; private int age; private String gender; private String address; private String employer; private String email; private String city; private String state; }
执行结果如下:
标签:检索,十一,searchRequest,编程,sourceBuilder,private,client,new,ES From: https://www.cnblogs.com/RunningSnails/p/17973004