ElaticSearch 7.x in action

es查询合集

Term 精准匹配查询

  • es写法
{
  "query": {
    "term": {
      "jerseyNo": "23"
    }
  }
}
  • java写法
QueryBuilders.termQuery("jerseyNo.keyword","23")

Terms 批量查询

  • es写法
{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "goods_id": [
              1,
              2,
              3
            ]
          }
        }
      ]
    }
  }
}
  • java写法
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
queryBuilder.must(QueryBuilders.termsQuery(fieldName + ".keyword", (List<String>) value));

Wildcard 模糊查询

  • es写法
{
  "query": {
    "wildcard": {
      "teamNameEn": "Ro*s"
    }
  }
}
  • java写法
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
queryBuilder.must(QueryBuilders.wildcardQuery(fieldName + ".keyword", ("*" + value + "*")));

Exsit 在特定的字段中查找非空值的文档

  • es写法
{
  "query": {
    "term": {
      "exists": "schoolType"
    }
  }
}
  • java写法
QueryBuilders.existsQuery("schoolType")

Prefix 查找包含带有指定前缀term的文档

  • es写法
{
  "query": {
    "prefix": {
      "teamNameEn": "Rock"
    }
  }
}
  • java写法
QueryBuilders.prefixQuery("teamNameEn","Rock")

range范围查询

  • es写法
{
  "query": {
    "range": {
      "playYear": {
        "gte": 2,
        "lte": 10
      }
    }
  }
}
  • java写法
queryBuilder.must(QueryBuilders.rangeQuery("playYear" + "TimeStamp").gte(startDate).lte(endDate));

Sort 排序查询

  • es写法
{
  "query": {
    "match": {
      "teamNameEn": "Rockets"
    }
  },
  "sort": [
    {
      "playYear": {
        "order": "desc"
      }
    }
  ]
}
  • java写法
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchQuery("teamNameEn","Rockets"));
sourceBuilder.sort("palyYear", SortOrder.DESC);

Terms Aggregation 根据字段项分组聚合

  • es写法
{
  "query": {
    "term": {
      "teamNameEn": {
        "value": "Rockets"
      }
    }
  },
  "aggs": {
    "aggsAge": {
      "terms": {
        "field": "age",
        "size": 20
      }
    }
  },
  "size": 0
}
  • java写法
SearchRequest searchRequest = new SearchRequest(index);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//根据teamNameEn.keyword分组,别名为age
TermsAggregationBuilder state = AggregationBuilders.terms("age").field("age.keyword").size(200);
searchSourceBuilder.aggregation(termsAggregationBuilder);
searchSourceBuilder.size(0);
System.out.println("********************************************");
System.out.println(searchSourceBuilder);
searchRequest.source(searchSourceBuilder);

es实战以及封装方法

es接收结果对象:EsQueryResultVO

@Builder
@Data
public class EsQueryResultVO<T>{
    private List<T> records ;
    private long total;
    private long size;
    /**
     * 查询es的游标
     */
    private Object[] lastNum;

}

es封装工具:EsUtil

@Slf4j
@Service
public class ESUtil {

    @Resource
    private RestHighLevelClient client;


    /**
     * 添加数据
     *
     * @param content 数据内容
     * @param index   索引
     * @param id      id
     */
    public String addData(XContentBuilder content, String index, String id) {
        try {
            IndexRequest request = new IndexRequest(index).id(id).source(content);
            IndexResponse response = client.index(request, RequestOptions.DEFAULT);
            id = response.getId();
            log.info("索引:{},数据添加,返回码:{},id:{}", index, response.status().getStatus(), id);
        } catch (Exception e) {
            log.error("添加数据失败,index:{},id:{}", index, id);
        }
        return id;
    }

    /**
     * 修改数据
     *
     * @param content 修改内容
     * @param index   索引
     * @param id      id
     */
    public String updateData(XContentBuilder content, String index, String id) {
        String resultId = null;
        try {
            UpdateRequest request = new UpdateRequest(index, id).doc(content);
            request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
            UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
            resultId = response.getId();
            log.info("数据更新,返回码:{},id:{}", response.status().getStatus(), resultId);
        } catch (Exception e) {
            log.error("数据更新失败,index:{},id:{}", index, id);
        }
        return resultId;
    }

    public boolean existsData(String index, String id) {
        try {
            GetRequest request = new GetRequest(index, id);
            boolean isExists = client.exists(request, RequestOptions.DEFAULT);
            return isExists;
        } catch (Exception e) {
            log.error("数据判断是否存在失败,index:{},id:{}", index, id);
        }
        return false;
    }

    /**
     * 批量插入数据
     *
     * @param index 索引
     * @param list  批量增加的数据
     */
    public String insertBatch(String index, List<EsDTO> list) throws IOException {
        String state = null;
        BulkRequest request = new BulkRequest();
        request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        list.forEach(item -> request.add(new IndexRequest(index)
                .id(item.getId()).source(JSON.toJSONString(item.getData()), XContentType.JSON)));
        try {
            BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
            if (bulk.hasFailures()) {
                throw new TransactionalHandleException(bulk.buildFailureMessage());
            }
            int status = bulk.status().getStatus();
            state = Integer.toString(status);
            log.info("索引:{},批量插入{}条数据成功!", index, list.size());
        } catch (Exception e) {
            log.error("索引:{},批量插入数据失败,异常描述:{}", index, e.getMessage());
            throw e;
        }
        return state;
    }

    /**
     * 根据条件删除数据
     *
     * @param index   索引
     * @param builder 删除条件
     */
    public void deleteByQuery(String index, QueryBuilder builder) {
        DeleteByQueryRequest request = new DeleteByQueryRequest(index);
        request.setQuery(builder);
        //设置此次删除的最大条数
        request.setBatchSize(1000);
        try {
            client.deleteByQuery(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("根据条件删除数据失败,index:{}", index);
        }
    }

    /**
     * 根据id删除数据
     *
     * @param index 索引
     * @param id    id
     */
    public String deleteById(String index, String id) {
        String state = null;
        DeleteRequest request = new DeleteRequest(index, id);
        try {
            DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
            int status = response.status().getStatus();
            state = Integer.toString(status);
            log.info("索引:{},根据id{}删除数据:{}", index, id, JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("根据id删除数据失败,index:{},id:{}", index, id);
        }
        return state;
    }


    /**
     * 根据条件查询数据
     *
     * @param index         索引
     * @param startPage     开始页
     * @param pageSize      每页条数
     * @param sourceBuilder 查询返回条件
     * @param queryBuilder  查询条件
     */
    public List<Map<String, Object>> searchDataPage(String index, int startPage, int pageSize,
                                                    SearchSourceBuilder sourceBuilder, QueryBuilder queryBuilder) {
        SearchRequest request = new SearchRequest(index);
        //设置超时时间
        sourceBuilder.timeout(new TimeValue(120, TimeUnit.SECONDS));
        //设置是否按匹配度排序
        sourceBuilder.explain(true);
        //加载查询条件
        sourceBuilder.query(queryBuilder);
        //设置分页
        sourceBuilder.from((startPage - 1) * pageSize).size(pageSize);
        log.info("查询返回条件:" + sourceBuilder.toString());
        request.source(sourceBuilder);
        try {
            SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
            TotalHits totalHits = searchResponse.getHits().getTotalHits();
            log.info("共查出{}条记录", totalHits);
            RestStatus status = searchResponse.status();
            if (status.getStatus() == 200) {
                List<Map<String, Object>> sourceList = new ArrayList<>();
                for (SearchHit searchHit : searchResponse.getHits().getHits()) {
                    Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
                    sourceList.add(sourceAsMap);
                }
                return sourceList;
            }
        } catch (Exception e) {
            log.error("条件查询索引{}时出错", index);
        }
        return null;
    }


    /**
     * 分页查询
     *
     * @param index         索引
     * @param query         分页条件
     * @param sourceBuilder 查询返回条件
     * @param queryBuilder  查询条件
     * @return
     */
    public IPage searchDataPage(String index, Query query,
                                      SearchSourceBuilder sourceBuilder, QueryBuilder queryBuilder) {
        SearchRequest request = new SearchRequest(index);
        //设置超时时间
        sourceBuilder.timeout(new TimeValue(120, TimeUnit.SECONDS));
        //设置是否按匹配度排序
        sourceBuilder.explain(true);
        //加载查询条件
        sourceBuilder.query(queryBuilder);
        //设置分页
        sourceBuilder.from((query.getCurrent() - 1) * query.getSize()).size(query.getSize());
        if (StringUtils.isNotEmpty(query.getAscs())) {
            sourceBuilder.sort(query.getAscs(), SortOrder.ASC);
        }
        if (StringUtils.isNotEmpty(query.getDescs())) {
            sourceBuilder.sort(query.getDescs(), SortOrder.DESC);
        }
        log.info("查询返回条件:" + sourceBuilder.toString());
        request.source(sourceBuilder);
        try {
            SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
            TotalHits totalHits = searchResponse.getHits().getTotalHits();
            log.info("共查出{}条记录", totalHits);
            RestStatus status = searchResponse.status();
            if (status.getStatus() == 200) {
                List sourceList = new ArrayList<>();
                IPage page = Condition.getPage(query);
                for (SearchHit searchHit : searchResponse.getHits().getHits()) {
                    Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
                    sourceList.add(sourceAsMap);
                }
                page.setRecords(sourceList);
                page.setTotal(totalHits.value);
                return page;
            }
        } catch (Exception e) {
            log.error("条件查询索引{}时出错", index);
        }
        return null;
    }

    /**
     * 获取单条
     *
     * @param index 索引
     * @param id    esId
     * @return
     */
    public Map<String, Object> searchOne(String index, String id) {
        try {
            GetRequest request = new GetRequest(index, id);
            GetResponse response = client.get(request, RequestOptions.DEFAULT);
            return response.getSourceAsMap();
        } catch (Exception e) {
            log.error("获取单条失败,index:{},id:{},e={}", index, id, e);
        }
        return null;
    }

    /**
     * 根据ids查询
     *
     * @param index               索引
     * @param searchSourceBuilder searchSourceBuilder
     * @return list
     */
    public List<Map<String, Object>> searchList(String index, SearchSourceBuilder searchSourceBuilder) {
        try {
            SearchRequest searchRequest = new SearchRequest(index);
//            searchRequest.searchType(type);
            //设置超时时间
            searchSourceBuilder.timeout(new TimeValue(120, TimeUnit.SECONDS));
            //设置是否按匹配度排序
            searchSourceBuilder.explain(true);
            searchRequest.source(searchSourceBuilder);
            log.info("index={},查询返回条件:" + searchSourceBuilder.toString());
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            RestStatus status = searchResponse.status();
            if (status.getStatus() == 200) {
                List sourceList = new ArrayList<>();
                for (SearchHit searchHit : searchResponse.getHits().getHits()) {
                    Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
                    sourceList.add(sourceAsMap);
                }
                return sourceList;
            }
        } catch (Exception e) {
            log.error("根据ids查询失败,index:{},searchSourceBuilder:{}", index, searchSourceBuilder);
        }
        return null;
    }

    /**
     * 通过searchAfter方式查询数据
     *
     * @param index               索引
     * @param query               分页参数
     * @param queryBuilder        查询条件
     * @param searchSourceBuilder 承载查询条件的参数
     * @return 查询结果
     */
    public EsQueryResultVO searchAfterList(String index, Query query, QueryBuilder queryBuilder, SearchSourceBuilder searchSourceBuilder) {
        List<Object> result = new ArrayList<>();
        try {
            if (StringUtils.isEmpty(index)) {
                return EsQueryResultVO.builder().build();
            }
            SearchRequest searchRequest = new SearchRequest(index);
            searchSourceBuilder.trackTotalHits(true);
            //设置超时时间
            searchSourceBuilder.timeout(new TimeValue(100, TimeUnit.SECONDS));
            //设置是否按匹配度排序
            searchSourceBuilder.explain(true);
            searchSourceBuilder.size(query.getSize());
            if (StringUtils.isNotEmpty(query.getAscs())) {
                searchSourceBuilder.sort(query.getAscs(), SortOrder.ASC);
            }
            if (StringUtils.isNotEmpty(query.getDescs())) {
                searchSourceBuilder.sort(query.getDescs(), SortOrder.DESC);
            }
            //设置查询条件
            searchSourceBuilder.query(queryBuilder);
            searchRequest.source(searchSourceBuilder);
            log.info("index={},查询返回条件={}", index, searchSourceBuilder);
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHit[] hits = searchResponse.getHits().getHits();
            if (Objects.nonNull(hits) && hits.length > 0) {
                for (SearchHit hit : hits) {
                    result.add(hit.getSourceAsMap());
                }
                SearchHit[] hits1 = searchResponse.getHits().getHits();
                Object[] lastNum = hits1[hits1.length - 1].getSortValues();
                return EsQueryResultVO.builder()
                        .records(result)
                        .size(query.getSize())
                        .total(searchResponse.getHits().getTotalHits().value)
                        .lastNum(lastNum)
                        .build();
            }

        } catch (Exception e) {
            log.error("根据ids查询失败,index:{},searchSourceBuilder:{}", index, searchSourceBuilder);
        }
        return EsQueryResultVO.builder().build();
    }

    /**
     * 单个更新
     *
     * @param index  索引
     * @param id     id
     * @param params 参数
     */
    public void updateEs(String index, String id, Map<String, Object> params) {
        log.info("updateEs开始修改ES数据时间:{};", com.goldnet.core.tool.utils.DateUtil.format(DateUtil.now(), "yyyy-MM-dd HH:mm:ss.SSS"));
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(getRequest(index, id, JSON.toJSONString(params)));
        //插入
        try {
            bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
            BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
            if (bulkResponse.hasFailures()) {
                for (BulkItemResponse item : bulkResponse.getItems()) {
                    log.info(">>>>>>updateEs>>>update error, id=" + id + ", msg=" + item.getFailureMessage());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info(">>>>>>updateEs>>>update error, id=" + id + ", error=" + e.getMessage());
        }
        log.info("updateEs结束修改ES数据时间:{};", com.goldnet.core.tool.utils.DateUtil.format(DateUtil.now(), "yyyy-MM-dd HH:mm:ss.SSS"));
    }


    /**
     * 批量更新
     *
     * @param index      索引
     * @param ids        ids
     * @param paramsList list
     */
    public void batchUpdateEs(String index, List<String> ids, List<Map<String, Object>> paramsList) {
        log.info("batchUpdateEs开始批量修改ES数据时间:{};", DateUtil.format(DateUtil.now(), "yyyy-MM-dd HH:mm:ss.SSS"));
        BulkRequest bulkRequest = new BulkRequest();
        for (int i = 0; i < ids.size(); i++) {
            UpdateRequest updateRequest = getRequest(index, ids.get(i), JSON.toJSONString(paramsList.get(i)));
            bulkRequest.add(updateRequest);
        }
        try {
            bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
            BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
            if (bulkResponse.hasFailures()) {
                for (BulkItemResponse item : bulkResponse.getItems()) {
                    log.info(">>>>>>batchUpdateEs>>>update error, id=" + item.getId() + ", msg=" + item.getFailureMessage());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info(">>>>>>batchUpdateEs>>>update error, error=" + e.getMessage());
        }
        log.info("batchUpdateEs结束批量修改ES数据时间:{};", DateUtil.format(DateUtil.now(), "yyyy-MM-dd HH:mm:ss.SSS"));
    }


    /**
     * 批量更新设置参数
     *
     * @param index 索引
     * @param id    id
     * @param json  json 数据
     * @return UpdateRequest
     */
    public UpdateRequest getRequest(String index, String id, String json) {
        UpdateRequest updateRequest = new UpdateRequest(index, id);
        //冲突重试设置
        updateRequest.retryOnConflict(3);
        //如果尚未存在,则表明必须将部分文档用作upsert文档
        updateRequest.docAsUpsert(true);
        //禁用noop检测
        updateRequest.detectNoop(false);
        //更新后是否获取_source
        updateRequest.fetchSource(true);
        //upsert
        updateRequest.upsert(json, XContentType.JSON);
        //指定doc
        updateRequest.doc(json, XContentType.JSON);
        return updateRequest;
    }


    /**
     * 分页查询 取重
     *
     * @param index         索引
     * @param query         分页条件
     * @param sourceBuilder 查询返回条件
     * @param queryBuilder  查询条件
     * @param disField      取重字段
     * @return
     */
    public IPage searchDataDistinctPage(String index, Query query, SearchSourceBuilder sourceBuilder, QueryBuilder queryBuilder, String disField) {
        SearchRequest request = new SearchRequest(index);
        //设置超时时间
        sourceBuilder.timeout(new TimeValue(120, TimeUnit.SECONDS));
        //设置是否按匹配度排序
        sourceBuilder.explain(true);
        sourceBuilder.trackTotalHits(true);
        //加载查询条件
        sourceBuilder.query(queryBuilder);
        //设置分页
        sourceBuilder.from((query.getCurrent() - 1) * query.getSize()).size(query.getSize());
        if (StringUtils.isNotEmpty(query.getAscs())) {
            sourceBuilder.sort(query.getAscs(), SortOrder.ASC);
        }
        if (StringUtils.isNotEmpty(query.getDescs())) {
            sourceBuilder.sort(query.getDescs(), SortOrder.DESC);
        }
        CollapseBuilder collapseBuilder = new CollapseBuilder(disField + ".keyword");
        sourceBuilder.collapse(collapseBuilder);
        AggregationBuilder aggregation = AggregationBuilders.cardinality(disField).field(disField + ".keyword");
        sourceBuilder.aggregation(aggregation);
        log.info("查询返回条件:" + sourceBuilder.toString());
        request.source(sourceBuilder);
        try {
            SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
            log.info("response is {}", searchResponse);
            Aggregations aggregations = searchResponse.getAggregations();
            Cardinality cardinality = aggregations.get(disField);
            //TotalHits totalHits = searchResponse.getHits().getTotalHits();
            log.info("共查出{}条记录", cardinality.getValue());
            RestStatus status = searchResponse.status();
            if (status.getStatus() == 200) {
                List sourceList = new ArrayList<>();
                IPage page = Condition.getPage(query);
                for (SearchHit searchHit : searchResponse.getHits().getHits()) {
                    Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
                    sourceList.add(sourceAsMap);
                }
                page.setRecords(sourceList);
                long size = searchDataDistinctCountPage(index,queryBuilder,disField, (int) cardinality.getValue());
                page.setTotal(size);
                return page;
            }
        } catch (Exception e) {
            log.error("条件查询索引{}时出错", index);
        }
        return null;
    }


    /**
     * 分页查询 取重 获取总数
     *
     * @param index         索引
     * @param queryBuilder  查询条件
     * @param disField      取重字段
     * @param size          原始条数
     * @return int
     */
    public long searchDataDistinctCountPage(String index, QueryBuilder queryBuilder, String disField, Integer size) {
        // 创建查询请求
        SearchRequest searchRequest = new SearchRequest(index);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 设置查询条件
        searchSourceBuilder.query(queryBuilder);
        // 设置分组聚合
        TermsAggregationBuilder termsAggregation = AggregationBuilders.terms("group_by_field")
                .field(disField + ".keyword")
                .size(size);
        searchSourceBuilder.aggregation(termsAggregation);
        // 设置返回结果数量为1,仅获取聚合结果
        searchSourceBuilder.size(0);
        // 将SearchSourceBuilder添加到SearchRequest中
        log.info("查询返回条件:" + searchSourceBuilder.toString());
        searchRequest.source(searchSourceBuilder);
        try {
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            RestStatus status = searchResponse.status();
            if (status.getStatus() == 200) {
                Terms groupByFieldTerms = searchResponse.getAggregations().get("group_by_field");
                long bucketCount = groupByFieldTerms.getBuckets().size();
                log.info("汇总去重复统计总数Bucket count: " + bucketCount);
                return bucketCount;
            }
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Long.parseLong(size.toString());
    }


    /**
     * 查询总条数
     *
     * @param index
     * @param query
     * @param sourceBuilder
     * @param queryBuilder
     * @return
     */
    public int count(String index, Query query, SearchSourceBuilder sourceBuilder, QueryBuilder queryBuilder) {
        SearchResponse response = getSearchResponse(index, query, sourceBuilder, queryBuilder);
        SearchHits hits = response.getHits();
        return Integer.parseInt(String.valueOf(hits.getTotalHits().value));
    }

    /**
     * 执行
     *
     * @param index
     * @param query
     * @param sourceBuilder
     * @param queryBuilder
     * @return
     */
    private SearchResponse getSearchResponse(String index, Query query, SearchSourceBuilder sourceBuilder, QueryBuilder queryBuilder) {
        SearchRequest searchRequest = new SearchRequest(index);
        //设置超时时间
        sourceBuilder.timeout(new TimeValue(120, TimeUnit.SECONDS));
        //设置是否按匹配度排序
        sourceBuilder.explain(true);
        sourceBuilder.trackTotalHits(true);
        //设置分页
        sourceBuilder.from((query.getCurrent() - 1) * query.getSize()).size(query.getSize());
        if (StringUtils.isNotEmpty(query.getAscs())) {
            sourceBuilder.sort(query.getAscs(), SortOrder.ASC);
        }
        if (StringUtils.isNotEmpty(query.getDescs())) {
            sourceBuilder.sort(query.getDescs(), SortOrder.DESC);
        }
        //返回所有数据
        sourceBuilder.query(queryBuilder);
        log.info("查询返回条件:" + sourceBuilder.toString());
        searchRequest.source(sourceBuilder);
        try {
            return client.search(searchRequest, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.warn("条件查询索引{}时出错", index);
            log.warn("条件查询索引时出错:", e);
        }
        return new SearchResponse();
    }


    /**
     * 总数据
     *
     * @param index
     * @param query
     * @param sourceBuilder
     * @param queryBuilder
     * @return
     */
    public List<Map<String, Object>> getEsSumList(String index, Query query, SearchSourceBuilder sourceBuilder, QueryBuilder queryBuilder) {
        SearchResponse searchResponse = getSearchResponse(index, query, sourceBuilder, queryBuilder);
        RestStatus status = searchResponse.status();
        if (status.getStatus() == 200) {
            List sourceList = new ArrayList<>();
            for (SearchHit searchHit : searchResponse.getHits().getHits()) {
                Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
                sourceList.add(sourceAsMap);
            }
            return sourceList;
        }
        return new ArrayList<>();

    }


    /**
     * Scroll分页查询
     *
     * @param index         索引
     * @param query         分页条件
     * @param sourceBuilder 查询返回条件
     * @param queryBuilder  查询条件
     * @return
     */
    public IPage searchDataScrollListPage(String index, Query query, SearchSourceBuilder sourceBuilder, QueryBuilder queryBuilder) {

        // 创建滚动分页查询请求
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.scroll(TimeValue.timeValueMinutes(1L));

        // 加载查询条件
        sourceBuilder.query(queryBuilder);
        // 设置分页
        sourceBuilder.size(query.getSize());
        // 设置为 true,以获取准确的总命中数
        sourceBuilder.trackTotalHits(true);
        if (StringUtils.isNotEmpty(query.getAscs())) {
            sourceBuilder.sort(query.getAscs(), SortOrder.ASC);
        }
        if (StringUtils.isNotEmpty(query.getDescs())) {
            sourceBuilder.sort(query.getDescs(), SortOrder.DESC);
        }
        log.info("查询返回条件:" + sourceBuilder.toString());
        searchRequest.source(sourceBuilder);

        String scrollId = null;
        // 3、发送请求到ES
        SearchResponse scrollResponse = null;
        // 设置游标id存活时间
        Scroll scroll = new Scroll(TimeValue.timeValueMinutes(2));
        // 记录所有游标id
        List<String> scrollIds = new ArrayList<>();
        for (int i = 0; i < query.getCurrent(); i++) {
            try {
                // 首次检索
                if (i == 0) {
                    //记录游标id
                    searchRequest.scroll(scroll);
                    // 首次查询需要指定索引名称和查询条件
                    scrollResponse = client.search(searchRequest, RequestOptions.DEFAULT);
                    // 下一次搜索要用到该游标id
                    // 记录所有游标id
                }
                // 非首次检索
                else {
                    // 不需要在使用其他条件,也不需要指定索引名称,只需要使用执行游标id存活时间和上次游标id即可,毕竟信息都在上次游标id里面呢
                    SearchScrollRequest searchScrollRequest = new SearchScrollRequest(scrollId);
                    searchScrollRequest.scroll(scroll);
                    scrollResponse = client.scroll(searchScrollRequest, RequestOptions.DEFAULT);
                    // 下一次搜索要用到该游标id
                    // 记录所有游标id
                }
                scrollId = scrollResponse.getScrollId();
                scrollIds.add(scrollId);
            } catch (Exception e) {
                log.error("条件查询索引{}时出错", index);
            }
        }
        //清除游标id
        ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
        clearScrollRequest.scrollIds(scrollIds);
        try {
            client.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("清除滚动查询游标id失败{}时出错:", index);
            log.error("清除滚动查询游标出错:", e);
        }
        // 4、处理响应结果
        List sourceList = new ArrayList<>();
        IPage page = Condition.getPage(query);
        if (!Func.isEmpty(scrollResponse)) {
            SearchHits hits = scrollResponse.getHits();
            TotalHits totalHits = scrollResponse.getHits().getTotalHits();
            log.info("共查出{}条记录", totalHits);
            for (SearchHit searchHit : hits) {
                Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
                sourceList.add(sourceAsMap);
            }
            page.setRecords(sourceList);
            page.setTotal(totalHits.value);
        }
        return page;
    }
}

组装BoolQueryBuilder,Map必须包含三个字段name、value、nameType

public BoolQueryBuilder queryBuilder(List<Map<String, Object>> params, GoldNetUser goldNetUser) {
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
    queryBuilder.must(QueryBuilders.termQuery("isDeleted", 0));
    if(Func.isEmpty(params)){
        return queryBuilder;
    }

    for (Map<String, Object> data : params) {
        if (Func.isEmpty(data.get("name")) || Func.isEmpty(data.get("value")) || Func.isEmpty(data.get("nameType"))) {
            continue;
        }
        String type = data.get("nameType").toString();
        String fieldName = data.get("name").toString();
        Object value = data.get("value");

        if (type.equals(SearchTypeEnum.EXACTMATCH.getFieldName())) {
            if(value instanceof String){
                queryBuilder.must(QueryBuilders.termsQuery(fieldName + ".keyword", value));
            }else{
                //精确
                queryBuilder.must(QueryBuilders.termsQuery(fieldName, value));
            }
        } else if (type.equals(SearchTypeEnum.LIKEMATCH.getFieldName())) {
            //模糊
            queryBuilder.must(QueryBuilders.wildcardQuery(fieldName + ".keyword", ("*" + value + "*")));
        } else if (type.equals(SearchTypeEnum.DATE.getFieldName())) {
            //日期
            String[] dates = value.toString().split(",");
            long startDate = Func.isEmpty(dates[0]) ? 0 : Long.parseLong(dates[0]);
            long endDate = Func.isEmpty(dates[1]) ? 0 : Long.parseLong(dates[1]);
            if (startDate > 0 && endDate > 0) {
                queryBuilder.must(QueryBuilders.rangeQuery(fieldName + "TimeStamp").gte(startDate).lte(endDate));
            }
        } else if (type.equals(SearchTypeEnum.ARRAYMATCH.getFieldName())) {
            //数组
            queryBuilder.must(QueryBuilders.termsQuery(fieldName + ".keyword", (List<String>) value));
        }
    }
    //queryBuilder.must(QueryBuilders.termQuery("isDeleted", 0));
    return queryBuilder;
}

nameType的枚举类

public enum SearchTypeEnum {
    LIKEMATCH(10, "like", "模糊匹配"),
    EXACTMATCH(20, "exact", "精确匹配"),
    DATE(30, "date", "日期范围匹配"),
    GT(40, "gt", "大于"),
    LT(50, "lt", "小于"),
    ARRAYMATCH(60, "array", "数组格式"),
    SCAN(70, "scan", "发票影像件-特殊");

    private final Integer code;
    private final String fieldName;
    private final String fieldDesc;
    private static Map map = new HashMap();

    private SearchTypeEnum(Integer code, String fieldName, String fieldDesc) {
        this.code = code;
        this.fieldName = fieldName;
        this.fieldDesc = fieldDesc;
    }

    public Integer getCode() {
        return this.code;
    }

    public String getFieldName() {
        return this.fieldName;
    }

    public String getFieldDesc() {
        return this.fieldDesc;
    }

    public static SearchTypeEnum valueOf(int code) {
        return (SearchTypeEnum)map.get(code);
    }

    static {
        SearchTypeEnum[] var0 = values();
        int var1 = var0.length;

        for(int var2 = 0; var2 < var1; ++var2) {
            SearchTypeEnum searchTypeEnum = var0[var2];
            map.put(searchTypeEnum.code, searchTypeEnum);
        }

    }
}

综合应用

public R objections(CommonQueryParam param, Query query) {
        User user = AuthUtil.getUser();
        BoolQueryBuilder queryBuilder = commonService.queryBuilder(param.getParam(), user);
        queryBuilder.must(QueryBuilders.termQuery("enterpriseId.keyword", user.getEnterpriseId()));
        //queryBuilder.must(QueryBuilders.termQuery("enterpriseId.keyword", "1"));
        queryBuilder.mustNot(QueryBuilders.termQuery("arrivalStatus.keyword", "0"));
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.trackTotalHits(true);
        Ipage page = esUtil.searchDataPage(ES_REC_SHIPMENT_ORDER_INDEX, query, sourceBuilder, queryBuilder);
        return R.data(page);
    }

你可能感兴趣的:(elasticsearch,开发语言,elasticsearch)