当前位置:   article > 正文

ElasticSearch查询索引_elasticsearch 查询索引

elasticsearch 查询索引
/**
 * 查询
 * @param index         索引
 * @param type          数据类型
 * @param searchParam   搜索条件
 * @return
 */
public <T> List<T> search(String index, Class<T> type, SearchParam searchParam) {
    String keyword = searchParam.getKeyword();
    System.out.println("keyword = " + keyword);
    //构建搜索条件
    SearchRequest request = new SearchRequest(index);

    SearchSourceBuilder builder = new SearchSourceBuilder();
    // builder.query(QueryBuilders.termQuery("brand",keyword));
    // builder.query(QueryBuilders.matchPhraseQuery ("name",keyword));
    builder.query(QueryBuilders.multiMatchQuery(keyword,"tester","name","brand"));

    request.source(builder);
    System.out.println("request = " + request);
    List<T> entityList = new ArrayList<>();
    try {
        SearchResponse response = client.search(request, ElasticConfig.COMMON_OPTIONS);
        System.out.println("response = " + response.toString());
        SearchHits hits = response.getHits();
        SearchHit[] searchHits = hits.getHits();
        Arrays.stream(searchHits).forEach(hit -> {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            T t = null;
            try {
                t = BeanUtil.fillBeanWithMap(sourceAsMap, type.newInstance(), false);
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
            entityList.add(t);
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    return entityList;
}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/663295
推荐阅读
相关标签
  

闽ICP备14008679号