当前位置: 首页 > article >正文

【ES常用查询】基于ElasticsearchRestTemplate及NativeSearchQuery的查询

包含当前es所有的查询,

需要什么代码直接照搬,改个参数就行!

用的好请务必给我点赞!!!感谢爱你们!!!

(周末更  筒)

为啥写这篇文章呢:

大概是因为目前公司用的api跟以前的不太一样,

以前我们是基于高标准客户端直接做的,

但是目前这边同事是基于ElasticsearchRestTemplate跟NativeSearchQuery做的。


import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/v/k/t/query")
public class EsTestController {
    /**
     * 测试es各种查询方法*
     */

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    //通配符查询
    @PostMapping("/es/wildcard")
    public void wildcard() {
        //*:匹配任意数量的字符(包括零个字符)。 *x*: 对x做前后N位做模糊查询,前后有多少值都可以查到(也可以左模糊或者右模糊)
        //?:用于匹配单个字符.    user?a: user?a   匹配user1a,但不匹配user123a
        //^:必须以某个字符开头,如^user*:必须以user开头
        //$:必须以某个字符结尾,如user$:必须以user结尾

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        //wildcardQuery:通配符查询
        WildcardQueryBuilder bsVersion = QueryBuilders.wildcardQuery("bsVersion", "*1*");
        WildcardQueryBuilder bsVersion2 = QueryBuilders.wildcardQuery("type", "*j*");
        boolQueryBuilder.must(bsVersion).should(bsVersion2);

        NativeSearchQuery dsl = new NativeSearchQueryBuilder()
    }

    //高亮
    //默认情况下:只能对查询字段高亮!!!
    //加了.requireFieldMatch(false) 可以对非查询字段高亮,具体看下文
    @PostMapping("/es/highlight")
    public void highlight() {

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        //wildcardQuery:通配符查询
        WildcardQueryBuilder bsVersion = QueryBuilders.wildcardQuery("bsVersion", "*1*");
        WildcardQueryBuilder bsVersion2 = QueryBuilders.wildcardQuery("type", "*j*");
        boolQueryBuilder.must(bsVersion).should(bsVersion2);

        NativeSearchQuery dsl = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withHighlightFields(
                        //.requireFieldMatch(false) : 加了可以对非查询字段高亮
                        new HighlightBuilder.Field("bsVersion").preTags("<em>").postTags("</em>").requireFieldMatch(false),
                        new HighlightBuilder.Field("provinceNameCn").preTags("<em>").postTags("</em>").requireFieldMatch(false),
                        new HighlightBuilder.Field("isp").preTags("<em>").postTags("</em>").requireFieldMatch(false),
                        new HighlightBuilder.Field("type").preTags("<em>").postTags("</em>").requireFieldMatch(false),
                        new HighlightBuilder.Field("agentId").preTags("<em>").postTags("</em>").requireFieldMatch(false)
                )
                .build();

        SearchHits<Map> search = elasticsearchRestTemplate.search(dsl, Map.class, IndexCoordinates.of("298_ops-web-js_1"));
        log.info("总条数:{}", search.getTotalHits());
        for (SearchHit<Map> searchHit : search.getSearchHits()) {
            // 高亮字段是个大MAP  高亮字段集合 = {bsVersion=[<em>115</em>]}  //K=字段名 V=高亮了的数据集合
            Map<String, List<String>> highlightFields = searchHit.getHighlightFields();
            System.out.println("获取高亮字段集合:bsVersion =  " + highlightFields.get("bsVersion"));
            System.out.println("获取高亮字段集合:provinceNameCn =  " + highlightFields.get("provinceNameCn"));
            System.out.println("获取高亮字段集合:isp =  " + highlightFields.get("isp"));
            System.out.println("获取高亮字段集合:type =  " + highlightFields.get("type"));
            System.out.println("获取高亮字段集合:agentId =  " + highlightFields.get("agentId"));
            List<String> bsVersionResult = highlightFields.get("bsVersion");
            for (String s : bsVersionResult) {
                System.out.println("bsVersion集合每一个值为 = " + s);
            }

//            highlightFields.forEach((k, v) -> {
//                //获取高亮字段:k = bsVersion v = [<em>115</em>]
//                System.out.println("获取高亮字段:k = " + k + " v = " + v);
//            });
        }
        log.info("总条数:{}", search.getTotalHits());
        System.out.println("search.getSearchHits().size() = " + search.getSearchHits().size());
    }

    //排序
    @PostMapping("/es/sort")
    public void sort() {
        MatchAllQueryBuilder matchAllQuery = QueryBuilders.matchAllQuery();

        //备注:这里用的是NativeSearchQueryBuilder:xxxBuilder。最后一定要.build();,不然条件用不了
        NativeSearchQuery dsl = new NativeSearchQueryBuilder()
                .withQuery(matchAllQuery)
                //备注:应该是要keyword,paasword这一类才能排序。我用text类型排序报:非法字段异常
                .withSorts(SortBuilders.fieldSort("appId").order(SortOrder.ASC)) //排序:根据bsVersion升序排序
                .build();

        SearchHits<Map> search = elasticsearchRestTemplate.search(dsl, Map.class, IndexCoordinates.of("298_ops-web-js_1"));
        log.info("总条数:{}", search.getTotalHits());
        for (SearchHit<Map> searchHit : search.getSearchHits()) {
            System.out.println("单条文档的原数据 = " + searchHit.getContent());
            System.out.println("单条文档的id = " + searchHit.getId());
            System.out.println("单条文档的评分 = " + searchHit.getScore());
            System.out.println("单条文档索引名(表名) = " + searchHit.getIndex());//298_ops-web-js_1就是你输入的索引库名
        }

    }

    //分页
    @PostMapping("/es/page")
    public void page() {
        Integer page = 1;
        Integer size = 10;
        MatchAllQueryBuilder matchAllQuery = QueryBuilders.matchAllQuery();
        NativeSearchQuery dsl = new NativeSearchQuery(matchAllQuery);
        //分页
        Pageable pageable = PageRequest.of((page - 1) * size, size);
        dsl.setPageable(pageable);

        SearchHits<Map> search = elasticsearchRestTemplate.search(dsl, Map.class, IndexCoordinates.of("298_ops-web-js_1"));
        log.info("总条数:{}", search.getTotalHits());

        // 获取当前页数
        log.info("当前页数:{}", page);

        // 获取当前页的条数
        int currentSize = search.getSearchHits().size();
        log.info("当前页条数:{}", currentSize);

        //数据列表:search.getSearchHits()  for出每一条,然后加到list中

        for (SearchHit<Map> searchHit : search.getSearchHits()) {
            System.out.println("单条文档的原数据 = " + searchHit.getContent());
            System.out.println("单条文档的id = " + searchHit.getId());
            System.out.println("单条文档的评分 = " + searchHit.getScore());
            System.out.println("单条文档索引名(表名) = " + searchHit.getIndex());//298_ops-web-js_1就是你输入的索引库名
        }

    }


    //布尔查询
    @PostMapping("/es/boolQueryBuilder")
    public void boolQueryBuilder() {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //must 必须满足且参与算分
        boolQueryBuilder.must(QueryBuilders.matchQuery("bsVersion", "114"));
        //mustNot 必须不满足   appID 300-700之间不参与查询(就是直接筛掉了)
        boolQueryBuilder.mustNot(QueryBuilders.rangeQuery("appId").gte("300").lte("700"));
        //filter 必须满足,且不参与算分
//        boolQueryBuilder.filter(QueryBuilders.termQuery("agentId", "298_ead348abbaf30f48"));
        NativeSearchQuery dsl = new NativeSearchQuery(boolQueryBuilder);
        SearchHits<Map> search = elasticsearchRestTemplate.search(dsl, Map.class, IndexCoordinates.of("298_ops-web-js_1"));
        log.info("总条数:{}", search.getTotalHits());
        for (SearchHit<Map> searchHit : search.getSearchHits()) {
            System.out.println("单条文档的原数据 = " + searchHit.getContent());
            System.out.println("单条文档的id = " + searchHit.getId());
            System.out.println("单条文档的评分 = " + searchHit.getScore());
            System.out.println("单条文档索引名(表名) = " + searchHit.getIndex());//298_ops-web-js_1就是你输入的索引库名
        }
    }


    //算分函数查询
    @PostMapping("/es/FunctionScoreQueryBuilder")
    public void FunctionScoreQueryBuilder() {
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(
                //原始查询
                QueryBuilders.matchAllQuery(),
                //算分数组
                new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{
                        //第一个元素
                        new FunctionScoreQueryBuilder.FilterFunctionBuilder(
                                //过滤器
                                QueryBuilders.matchQuery("bsVersion", "114"),
                                //权重
                                ScoreFunctionBuilders.weightFactorFunction(10)
                        ),
                        //第二个元素
                        new FunctionScoreQueryBuilder.FilterFunctionBuilder(
                                //过滤器
                                QueryBuilders.matchQuery("bsVersion", "115"),
                                //权重
                                ScoreFunctionBuilders.weightFactorFunction(10)
                        )
                }).boostMode(CombineFunction.REPLACE);

        NativeSearchQuery dsl = new NativeSearchQuery(functionScoreQueryBuilder);
        SearchHits<Map> search = elasticsearchRestTemplate.search(dsl, Map.class, IndexCoordinates.of("298_ops-web-js_1"));
        log.info("总条数:{}", search.getTotalHits());
        for (SearchHit<Map> searchHit : search.getSearchHits()) {
            System.out.println("单条文档的原数据 = " + searchHit.getContent());
            System.out.println("单条文档的id = " + searchHit.getId());
            System.out.println("单条文档的评分 = " + searchHit.getScore());
            System.out.println("单条文档索引名(表名) = " + searchHit.getIndex());//298_ops-web-js_1就是你输入的索引库名
        }
    }

    //范围查询
    @PostMapping("/es/rangeQuery")
    public void rangeQuery() {
        //范围查询 appId字段  >=0   <=200   的数据
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("appId").gte("0").lte("200");
        NativeSearchQuery dsl = new NativeSearchQuery(rangeQueryBuilder);
        SearchHits<Map> search = elasticsearchRestTemplate.search(dsl, Map.class, IndexCoordinates.of("298_ops-web-js_1"));
        log.info("总条数:{}", search.getTotalHits());
        for (SearchHit<Map> searchHit : search.getSearchHits()) {
            System.out.println("单条文档的原数据 = " + searchHit.getContent());
            System.out.println("单条文档的id = " + searchHit.getId());
            System.out.println("单条文档的评分 = " + searchHit.getScore());
            System.out.println("单条文档索引名(表名) = " + searchHit.getIndex());//298_ops-web-js_1就是你输入的索引库名
        }
    }

    //精确查询
    @PostMapping("/es/termQuery")
    public void termQuery() {
        //根据isp字段 精确查询 内网IP
        TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("isp", " 内网IP ");
        NativeSearchQuery dsl = new NativeSearchQuery(termsQueryBuilder);
        SearchHits<Map> search = elasticsearchRestTemplate.search(dsl, Map.class, IndexCoordinates.of("298_ops-web-js_1"));
        log.info("总条数:{}", search.getTotalHits());
        for (SearchHit<Map> searchHit : search.getSearchHits()) {
            System.out.println("单条文档的原数据 = " + searchHit.getContent());
            System.out.println("单条文档的id = " + searchHit.getId());
            System.out.println("单条文档的评分 = " + searchHit.getScore());
            System.out.println("单条文档索引名(表名) = " + searchHit.getIndex());//298_ops-web-js_1就是你输入的索引库名
        }
    }


    //多字段查询
    @PostMapping("/es/multiMatchQuery")
    public void multiMatchQuery() {
        //备注:字段必须是text类型,可以分词类型!!!!
        //查找keyword、数值、日期、boolean等会报错!!!
        //如果放入精确类型的字段,会报错!!!!
        MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery("114", "bsVersion", "type");//查询114根据"bsVersion","type"
        NativeSearchQuery dsl = new NativeSearchQuery(multiMatchQuery);
        SearchHits<Map> search = elasticsearchRestTemplate.search(dsl, Map.class, IndexCoordinates.of("298_ops-web-js_1"));
        log.info("总条数:{}", search.getTotalHits());
        for (SearchHit<Map> searchHit : search.getSearchHits()) {
            System.out.println("单条文档的原数据 = " + searchHit.getContent());
            System.out.println("单条文档的id = " + searchHit.getId());
            System.out.println("单条文档的评分 = " + searchHit.getScore());
            System.out.println("单条文档索引名(表名) = " + searchHit.getIndex());//298_ops-web-js_1就是你输入的索引库名
        }
    }

    //单字段查询
    @PostMapping("/es/matchQuery")
    public void matchQuery() {
        MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("bsVersion", "114");
        NativeSearchQuery dsl = new NativeSearchQuery(matchQuery);
        SearchHits<Map> search = elasticsearchRestTemplate.search(dsl, Map.class, IndexCoordinates.of("298_ops-web-js_1"));
        log.info("总条数:{}", search.getTotalHits());
        for (SearchHit<Map> searchHit : search.getSearchHits()) {
            System.out.println("单条文档的原数据 = " + searchHit.getContent());
            System.out.println("单条文档的id = " + searchHit.getId());
            System.out.println("单条文档的评分 = " + searchHit.getScore());
            System.out.println("单条文档索引名(表名) = " + searchHit.getIndex());//298_ops-web-js_1就是你输入的索引库名
        }
    }

    //查询所有
    @PostMapping("/es/matchAllQuery")
    public void matchAllQuery() {
        MatchAllQueryBuilder matchAllQuery = QueryBuilders.matchAllQuery();
        NativeSearchQuery dsl = new NativeSearchQuery(matchAllQuery);
        SearchHits<Map> search = elasticsearchRestTemplate.search(dsl, Map.class, IndexCoordinates.of("298_ops-web-js_1"));
        log.info("总条数:{}", search.getTotalHits());
        log.info("查询原始对象:{}", search.getSearchHits());
        log.info("分数值:{}", search.getMaxScore());
        log.info("有无聚合:{}", search.hasAggregations());
        log.info("返回搜索命中数量的关系,例如精确值、估计值:{}", search.getTotalHitsRelation());
        log.info("判断是否存在搜索命中结果:{}", search.hasSearchHits());
        //原始结果的每一条数据
        for (SearchHit<Map> searchHit : search.getSearchHits()) {
            System.out.println("单条文档 = " + searchHit);
            System.out.println("单条文档的原数据 = " + searchHit.getContent());
        }
    }


}

————————————————————————————————————


http://www.kler.cn/a/133293.html

相关文章:

  • 【Qt实现虚拟键盘】
  • LC12:双指针
  • 文献阅读 | Nature Communications:使用自适应图注意自动编码器从空间解析的转录组学中解读空间域
  • 一文了解Android中的AudioFlinger
  • 大模型研究报告 | 2024年中国金融大模型产业发展洞察报告|附34页PDF文件下载
  • 「QT」窗口类 之 QWidget 窗口基类
  • LabVIEW和NIUSRP硬件加快了认知无线电开发
  • 拍照小白入坑
  • PHP接收并处理请求中携带的xml格式的信息
  • 【小黑送书—第八期】>>别再吐槽大学教材了,来看看这些网友强推的数学神作!
  • 解决STM32F429烧录程序后还需复位才能植入程序的bug
  • 深入Android S(12.0) 探索 Android Framework 之 SystemServer 进程启动详解
  • 解决docker运行elastic服务端启动不成功
  • pandas教程:Resampling and Frequency Conversion 重采样和频度转换
  • Flask学习一:概述
  • IntelliJ IDE 插件开发 |(一)快速入门
  • CentOS7安装xvfb,解决服务器没有X-Server的问题
  • springboot+activiti5.22.0集成Activiti在线流程设计器
  • 数据结构-插入排序实现
  • CCRC认证是什么?
  • JVM 调优指南
  • 视频会议设备如何安装?
  • 机器学习笔记 - 使用 PyTorch 的多任务学习和 HydraNet
  • SpringBoot-----从前端更新数据到MySql数据库
  • Android问题笔记四十六:解决open failed: EACCES (Permission denied) 问题
  • Scrapy----Scrapy简介