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

Java结合ElasticSearch根据查询关键字,高亮显示全文数据。

由于es高亮显示机制的问题。当全文内容过多,且搜索中标又少时,就会出现高亮结果无法覆盖全文。因此需要根据需求手动替换。
1.根据es的ik分词器获取搜索词的分词结果。
es部分:

//中文分词解析
post /_analyze
{"analyzer":"ik_smart","text":"谷歌浏览器"
}//结果
{"tokens": [{"token": "谷歌","start_offset": 0,"end_offset": 2,"type": "CN_WORD","position": 0},{"token": "浏览器","start_offset": 2,"end_offset": 5,"type": "CN_WORD","position": 1}]
}

注意:ik_smart 是最粗颗粒度,不会有重复分词。ik_max_word 是最细颗粒度,会有重复分词。高亮显示只需要最粗即可。
ik_smart:
在这里插入图片描述
ik_max_word:
在这里插入图片描述

将es的语句转为Java语句:

//主要使用的包
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestHighLevelClient;@Resourceprivate RestHighLevelClient restHighLevelClient;/*** 获取到es的分词结果** @param searchContent 查询关键字* @return 分词结果*/private List<String> getAnalyze(String searchContent) {List<String> tokens = new ArrayList<>();if (StringUtils.isNotEmpty(searchContent)) {String endpoint = "/_analyze";String body = "{\n" +"  \"analyzer\": \"ik_smart\",\n" +"  \"text\": \"" + searchContent + "\"\n" +"}";try {Request request = new Request("POST", endpoint);request.setJsonEntity(body);Response response = restHighLevelClient.getLowLevelClient().performRequest(request);InputStream content = response.getEntity().getContent();JsonNode jsonNode = objectMapper.readTree(content);if (jsonNode.has("tokens")) {for (JsonNode token : jsonNode.get("tokens")) {tokens.add(token.get("token").asText());}}} catch (IOException | UnsupportedOperationException e) {log.error("ES查询分词异常", e);}}return tokens;}

2.根据获取到的多个分词数据。替换全文内容。

    /*** 根据多个需要替换的字符,高效替换全文数据* @param replaceStrList 替换字符* @param content 全文* @return 高亮显示的全文*/private String replaceHighlight(List<String> replaceStrList, String content) {StringBuffer result = new StringBuffer();try {Map<String, String> replacements = new HashMap<>();for (String replaceStr : replaceStrList) {replacements.put(replaceStr, "<font class='eslight'>" + replaceStr + "</font>");}Pattern pattern = Pattern.compile(String.join("|", replacements.keySet()));Matcher matcher = pattern.matcher(content);while (matcher.find()) {matcher.appendReplacement(result, replacements.get(matcher.group(0)));}matcher.appendTail(result);} catch (Exception e) {log.error("替换高亮显示异常", e);}return result.toString();}

此时就能将全文关键词以分词的效果高亮显示了。

http://www.lryc.cn/news/485215.html

相关文章:

  • Design Compiler:Topographical Workshop Lab2
  • 【C语言】连接陷阱探秘(1):声明与定义
  • ChatGPT学术专用版,一键润色纠错+中英互译+批量翻译PDF
  • python isinstance(True, int)
  • 1.5寸**进口 128128带灰阶oled屏 spi串口 老王电子diy 设备 OLED 2024/11/15 arduino
  • 【EasyExcel】复杂导出操作-自定义颜色样式等(版本3.1.x)
  • 机器学习 ---线性回归
  • 深度学习每周学习总结J5(DenseNet-121 +SE 算法实战与解析 - 猴痘识别)
  • VBA学习笔记:点击单元格显示指定的列
  • windows C#-LINQ概述
  • vue项目npm run serve出现【- Network: unavailable】(从排查到放弃)
  • R语言贝叶斯分析:INLA 、MCMC混合模型、生存分析肿瘤临床试验、间歇泉喷发时间数据应用|附数据代码...
  • C++ 关于类与对象(中篇)一篇详解!(运算符重载)
  • Scala的set
  • Linux---常用shell脚本
  • windows二进制安全零基础(二)
  • git常用命令+搭vscode使用
  • 如何在C#中处理必盈接口返回的股票数据?
  • 01 最舒适的python开发环境
  • 【PyTorch】libtorch_cpu.so: undefined symbol: iJIT_NotifyEvent
  • 快速利用c语言实现线性表(lineList)
  • 量子计算与人工智能的交汇:科技未来的新引擎
  • 51单片机使用NRF24L01进行2.4G无线通信
  • HelloMeme 上手即用教程
  • 自定义call方法和apply方法
  • typescript中为js文件提供类型声明
  • ETH挖矿显卡超频信息汇总
  • 调用 Xinference OpenAI接口时报错 Model not found in the model list, uid
  • 一文说清:C静态库与动态库的区别
  • Mysql 5.7.6以上版本怎样关闭GTID(由GTID改为基于file,position方式)