经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Elasticsearch » 查看文章
ElasticSearch 实现分词全文检索 - 高亮查询
来源:cnblogs  作者:VipSoft  时间:2023/3/20 9:01:05  对本文有异议

目录

ElasticSearch 实现分词全文检索 - 概述
ElasticSearch 实现分词全文检索 - ES、Kibana、IK安装
ElasticSearch 实现分词全文检索 - Restful基本操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 索引操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 文档操作
ElasticSearch 实现分词全文检索 - 测试数据准备
ElasticSearch 实现分词全文检索 - term、terms查询
ElasticSearch 实现分词全文检索 - match、match_all、multimatch查询
ElasticSearch 实现分词全文检索 - id、ids、prefix、fuzzy、wildcard、range、regexp 查询
ElasticSearch 实现分词全文检索 - Scroll 深分页
ElasticSearch 实现分词全文检索 - delete-by-query
ElasticSearch 实现分词全文检索 - 复合查询
ElasticSearch 实现分词全文检索 - filter查询
ElasticSearch 实现分词全文检索 - 高亮查询
ElasticSearch 实现分词全文检索 - 聚合查询 cardinality 以下待发布
ElasticSearch 实现分词全文检索 - 经纬度查询
ElasticSearch 实现分词全文检索 - 搜素关键字自动补全(suggest)
ElasticSearch 实现分词全文检索 - SpringBoot 完整实现 Demo 附源码

数据准备

ElasticSearch 实现分词全文检索 - 测试数据准备

高亮查询

高亮查询,就是用户输入的关键字,以一定的特殊样式展示给用户,让用户知道为什么这个结果被检索出来
高亮展示的数据,本身就是文档中的一个Field,单独将Field以highlight的形式返回
ES提供了一个 highlight 属性,和 query 同级别的

  • fragment_size:指定返回多少个高亮数据,默认100
  • pre_tags:指定前缀标签 <font color="red">
  • post_tags:指定后缀标签 </font>
  • fields:指定哪几个字段以高亮形式返回
  1. # highlight 查询
  2. POST /sms-logs-index/_search
  3. {
  4. "query": {
  5. "match":{
  6. "smsContent": "江苏"
  7. }
  8. },
  9. "highlight": {
  10. "fields": {
  11. "smsContent": {} #指定哪几个字段以高亮形式返回
  12. },
  13. "pre_tags": "<font color='red'>",
  14. "post_tags": "</font>"
  15. }
  16. }

Java

  1. @Test
  2. void highlightQuery() throws Exception {
  3. String indexName = "sms-logs-index";
  4. RestHighLevelClient client = ESClient.getClient();
  5. //1. 创建SearchRequest对象
  6. SearchRequest request = new SearchRequest(indexName);
  7. //2. 指定查询条件
  8. SearchSourceBuilder builder = new SearchSourceBuilder();
  9. builder.query(QueryBuilders.matchQuery("smsContent","江苏"));
  10. HighlightBuilder highlightBuilder = new HighlightBuilder();
  11. highlightBuilder.field("smsContent",10)
  12. .preTags("<font color='red'>")
  13. .postTags("</font>");
  14. builder.highlighter(highlightBuilder);
  15. request.source(builder);
  16. //3. 执行查询
  17. SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
  18. //4. 输出返回值
  19. for (SearchHit hit : resp.getHits().getHits()) {
  20. System.out.println(hit.getHighlightFields());
  21. }
  22. }

原文链接:https://www.cnblogs.com/vipsoft/p/17171376.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号