赞
踩
目录
RestHighLevelClient 请求ES数据
使用Java请求es,如何使用RestHighLevelClient 进行请求查询,获取到数据呢?
引用文件:
Maven:
- <dependency>
- <groupId>org.elasticsearch</groupId>
- <artifactId>elasticsearch</artifactId>
- <version>6.8.2</version>
- </dependency>
-
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>elasticsearch-rest-high-level-client</artifactId>
- <version>6.8.2</version>
- </dependency>
-
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>elasticsearch-rest-client</artifactId>
- <version>6.8.2</version>
- </dependency>
-
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>x-pack-transport</artifactId>
- <version>6.8.12</version>
- </dependency>
Yml:
- elasticsearch:
- cluster:
- name: elastic
- hosts: ip:port
- address: ip:port
-
- xpack:
- security:
- user: elastic:elastic
- enabled: true
- public void getHisResultOri() {
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- SearchRequest searchRequest = new SearchRequest();
- // 查询的索引
- searchRequest.indices("check_data_index");
- // query 的查询
- QueryBuilder queryBuilder = QueryBuilders.wrapperQuery("{\"bool\":{\"must\":{\"terms\":{\"make.keyword\":[\"toyota\"]}}}}");
- searchSourceBuilder.query(queryBuilder);
- searchRequest.source(searchSourceBuilder);
- // 排序
- searchSourceBuilder.sorts().add(SortBuilders.fieldSort("price").order(SortOrder.fromString("desc")));
- // from size
- searchSourceBuilder.from(0);
- searchSourceBuilder.size(10);
-
- try {
- SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
- logger.info(JSON.toJSONString(searchResponse));
- SearchHits hits = searchResponse.getHits();
-
- SearchHit[] searchHits = hits.getHits();
- logger.info("search hit size :{},{}", hits.getTotalHits(), searchHits.length);
-
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
分析:
在查询中,设置索引,query的查询参数,排序,开始(from),个数(size),五个的内容。 排序 包括 字段和排序方式。简易的添加两个相关的类:
- import io.swagger.annotations.ApiModel;
- import io.swagger.annotations.ApiModelProperty;
-
- import java.util.List;
-
- @ApiModel(value = "es查询参数")
- public class EsQuery {
-
- @ApiModelProperty("es索引")
- private String sourceIndex;
-
- @ApiModelProperty("第几页")
- private Integer page;
-
- @ApiModelProperty("分页大小")
- private Integer size;
-
- @ApiModelProperty("查询参数")
- private String sourceExpr;
-
- @ApiModelProperty("排序")
- private List<EsOrder> orders;
-
- public String getSourceIndex() {
- return sourceIndex;
- }
-
- public void setSourceIndex(String sourceIndex) {
- this.sourceIndex = sourceIndex;
- }
-
- public Integer getPage() {
- return page;
- }
-
- public void setPage(Integer page) {
- this.page = page;
- }
-
- public Integer getSize() {
- return size;
- }
-
- public void setSize(Integer size) {
- this.size = size;
- }
-
- public String getSourceExpr() {
- return sourceExpr;
- }
-
- public void setSourceExpr(String sourceExpr) {
- this.sourceExpr = sourceExpr;
- }
-
- public List<EsOrder> getOrders() {
- return orders;
- }
-
- public void setOrders(List<EsOrder> orders) {
- this.orders = orders;
- }
-
- @Override
- public String toString() {
- return "{" +
- "sourceIndex='" + sourceIndex + '\'' +
- ", page=" + page +
- ", size=" + size +
- ", sourceExpr='" + sourceExpr + '\'' +
- ", orders=" + orders +
- '}';
- }
- }
- import io.swagger.annotations.ApiModel;
- import io.swagger.annotations.ApiModelProperty;
-
- @ApiModel(value = "es排序")
- public class EsOrder {
-
- @ApiModelProperty("排序字段")
- private String fieldName;
-
- @ApiModelProperty("排序方式:ASC,DESC")
- private String orderBy;
-
- public EsOrder(String fieldName, String orderBy) {
- this.fieldName = fieldName;
- this.orderBy = orderBy;
- }
-
- public String getFieldName() {
- return fieldName;
- }
-
- public void setFieldName(String fieldName) {
- this.fieldName = fieldName;
- }
-
- public String getOrderBy() {
- return orderBy;
- }
-
- public void setOrderBy(String orderBy) {
- this.orderBy = orderBy;
- }
- }
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import es.search.highLevel.entity.EsOrder;
- import es.search.highLevel.entity.EsQuery;
- import org.apache.commons.collections4.CollectionUtils;
- import org.elasticsearch.action.search.SearchRequest;
- import org.elasticsearch.action.search.SearchResponse;
- import org.elasticsearch.client.RequestOptions;
- import org.elasticsearch.client.RestHighLevelClient;
- import org.elasticsearch.index.query.QueryBuilder;
- import org.elasticsearch.index.query.QueryBuilders;
- import org.elasticsearch.search.SearchHit;
- import org.elasticsearch.search.SearchHits;
- import org.elasticsearch.search.builder.SearchSourceBuilder;
- import org.elasticsearch.search.sort.FieldSortBuilder;
- import org.elasticsearch.search.sort.SortOrder;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
-
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
-
- public class SearchEsHits {
-
- @Autowired
- private RestHighLevelClient highLevelClient;
-
-
- private static Logger logger = LoggerFactory.getLogger(SearchEsHits.class);
-
-
- public void getHitsResult(EsQuery esQuery) {
-
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- SearchRequest searchRequest = new SearchRequest();
- // 查询的索引
- searchRequest.indices(esQuery.getSourceIndex());
- // query 的查询
- String sourceExpr = esQuery.getSourceExpr();
- if (sourceExpr.contains("query")) {// 带有query的查询
- sourceExpr = JSONObject.parseObject(sourceExpr).getJSONObject("query").toJSONString();
- }
- QueryBuilder queryBuilder = QueryBuilders.wrapperQuery(sourceExpr);
- searchSourceBuilder.query(queryBuilder);
- searchRequest.source(searchSourceBuilder);
- // 排序
- List<EsOrder> orders = esQuery.getOrders();
- if (CollectionUtils.isNotEmpty(orders)) {
- for (EsOrder e : orders) {
- searchSourceBuilder.sort(new FieldSortBuilder(e.getFieldName()).order(SortOrder.fromString(e.getOrderBy())));
- }
- }
- // from size
- int start = esQuery.getPage() * esQuery.getSize();
- searchSourceBuilder.from(start);
- searchSourceBuilder.size(esQuery.getSize());
-
-
- try {
- SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
- logger.info(JSON.toJSONString(searchResponse));
- SearchHits hits = searchResponse.getHits();
- List<JSONObject> hitsList = new ArrayList<>();
- SearchHit[] searchHits = hits.getHits();
- logger.info("search hit size :{},{}", hits.getTotalHits(), searchHits.length);
- for (SearchHit hit : searchHits) {
- String result = hit.getSourceAsString();
- JSONObject jsonObject = JSONObject.parseObject(result);
- hitsList.add(jsonObject);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
PS: 这边没有对返回值,总数进一步处理,可以根据自己的需求再处理。
使用RestHighLevelClient查询es数据,可以使用QueryBuilders.wrapperQuery(sourceExpr)处理query的数据就会方便很多,使用boolQuery查询的话,会比较崩溃。查询聚合的方式就不得不单独处理了。
关联文章: 《使用commons-httpclinet 请求ES数据》
下一篇: 《使用RestHighLevelClient 请求ES统计数据》
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。