首页 文章 精选 留言 我的

精选列表

搜索[提示词调教],共10000篇文章
优秀的个人博客,低调大师

Elasticsearch关键词高亮检索案例分享

1.准备工作 参考文档《 集成Elasticsearch Restful API案例》导入和配置es客户端 2.创建索引表和初始化数据 通过组件 DocumentCRUD来创建索引表和初始化数据, DocumentCRUD组件实现本文不做重点介绍: /** * 创建索引表,并导入高亮检索功能需要的测试数据 */ public void initIndiceAndData(){ DocumentCRUD documentCRUD = new DocumentCRUD(); documentCRUD.testCreateIndice(); documentCRUD.testBulkAddDocuments(); } 3.高亮检索功能实现 3.1 定义高亮检索dsl 在文件 esmapper/demo.xml中增加testHighlightSearch配置: <!-- 一个简单的检索dsl,中有四个变量 name 全文检索字段 startTime endTime 通过map传递变量参数值 变量语法参考文档:https://my.oschina.net/bboss/blog/1556866 --> <property name="testHighlightSearch"> <![CDATA[{ "query": { "bool": { "filter": [ { ## 时间范围检索,返回对应时间范围内的记录,接受long型的值 "range": { "agentStarttime": { "gte": #[startTime],##统计开始时间 "lt": #[endTime] ##统计截止时间 } } } ], "must": [ #* { "query_string": { "query": #[condition], "analyze_wildcard": true } } *# { ## 全文检索参考文档 https://www.elastic.co/guide/en/elasticsearch/reference/6.2/full-text-queries.html "match_phrase" : { "name" : { "query" : #[condition] } } } ] } }, ## 最多返回1000条记录 "size":1000, ## 高亮检索定义,参考文档:https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-request-highlighting.html "highlight": { "pre_tags": [ "<mark>" ], "post_tags": [ "</mark>" ], "fields": { "*": {} }, "fragment_size": 2147483647 } }]]> </property> 3.2 编写高亮检索代码 创建检索类-org.bboss.elasticsearchtest.HighlightSearch 在其中定义以下方法 public void highlightSearch() throws ParseException { //创建加载配置文件的客户端工具,用来检索文档,单实例多线程安全 ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil(mappath); //设定查询条件,通过map传递变量参数值,key对于dsl中的变量名称 //dsl中有三个变量 // condition // startTime // endTime Map<String,Object> params = new HashMap<String,Object>(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间范围,时间参数接受long值 params.put("startTime",dateFormat.parse("2017-09-02 00:00:00")); params.put("endTime",new Date()); params.put("condition","喜欢唱歌");//全文检索条件,匹配上的记录的字段值对应的匹配内容都会被高亮显示 //执行查询,demo为索引表,_search为检索操作action ESDatas<Demo> esDatas = //ESDatas包含当前检索的记录集合,最多1000条记录,由dsl中的size属性指定 clientUtil.searchList("demo/_search",//demo为索引表,_search为检索操作action "testHighlightSearch",//esmapper/demo.xml中定义的dsl语句 params,//变量参数 Demo.class);//返回的文档封装对象类型 //获取总记录数 long totalSize = esDatas.getTotalSize(); System.out.println(totalSize); //获取结果对象列表,最多返回1000条记录 List<Demo> demos = esDatas.getDatas(); for(int i = 0; demos != null && i < demos.size(); i ++){//遍历检索结果列表 Demo demo = demos.get(i); //记录中匹配上检索条件的所有字段的高亮内容 Map<String,List<Object>> highLights = demo.getHighlight(); Iterator<Map.Entry<String, List<Object>>> entries = highLights.entrySet().iterator(); while(entries.hasNext()){ Map.Entry<String, List<Object>> entry = entries.next(); String fieldName = entry.getKey(); System.out.print(fieldName+":"); List<Object> fieldHighLightSegments = entry.getValue(); for (Object highLightSegment:fieldHighLightSegments){ /** * 在dsl中通过<mark></mark>来标识需要高亮显示的内容,然后传到web ui前端的时候,通过为mark元素添加css样式来设置高亮的颜色背景样式 * 例如: * <style type="text/css"> * .mark,mark{background-color:#f39c12;padding:.2em} * </style> */ System.out.println(highLightSegment); } } } } 4.运行检索功能 定义junit测试用例: import org.junit.Test; import java.text.ParseException; public class HighlightSearchTest { @Test public void testHighlightSearch() throws ParseException { HighlightSearch highlightSearch = new HighlightSearch(); highlightSearch.initIndiceAndData(); highlightSearch.highlightSearch(); } @Test public void testHighlightSearchOther() throws ParseException { HighlightSearch highlightSearch = new HighlightSearch(); highlightSearch.initIndiceAndData(); highlightSearch.highlightSearchOther(); } } 在idea或者eclipse中运行测试用例即可,输出检索到的高亮内容信息如下: name:刘德华<mark>喜</mark><mark>欢</mark><mark>唱</mark><mark>歌</mark>454 name:刘德华<mark>喜</mark><mark>欢</mark><mark>唱</mark><mark>歌</mark>488 name:刘德华<mark>喜</mark><mark>欢</mark><mark>唱</mark><mark>歌</mark>508 name:刘德华<mark>喜</mark><mark>欢</mark><mark>唱</mark><mark>歌</mark>518 5.完整的demo实例工程 https://github.com/bbossgroups/eshelloword-booter https://gitee.com/bbossgroups/eshelloword-booter 6.参考文档 https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-request-highlighting.html https://my.oschina.net/bboss/blog/1801273 5 开发交流 elasticsearch技术交流群:166471282 elasticsearch微信公众号:bbossgroups

优秀的个人博客,低调大师

更好的IDE代码提示:laravel-ide-helper

https://github.com/barryvdh/laravel-ide-helper 安装 composer require --dev barryvdh/laravel-ide-helper 设置服务提供者 config/app.php: Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class, 5.5及以上版本不需要设置 设置只在开发环境中加载 app/Providers/AppServiceProvider.php: public function register() { if ($this->app->environment() !== 'production') { $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class); } // ... } 设置自动更新 conposer.json: "scripts":{ "post-update-cmd": [ "Illuminate\\Foundation\\ComposerScripts::postUpdate", "php artisan ide-helper:generate", "php artisan ide-helper:meta" ] }, 发布配置文件 php artisan vendor:publish --provider="Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider" --tag=config 设置链式调用支持 config/ide-helper.php: 'include_fluent' => true,

资源下载

更多资源
Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册