elasticsearch5.x:查询建议介绍、Suggester 介绍以及Java-api实现

 

elasticsearch5.x:查询建议介绍、Suggester 介绍


参考:http://www.cnblogs.com/leeSmall/p/9206646.html

参考(重点):https://elasticsearch.cn/article/142

参考(官网):https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html


一、查询建议介绍

1. 查询建议是什么?

查询建议,为用户提供良好的使用体验。主要包括: 拼写检查; 自动建议查询词(自动补全)

拼写检查如图: image

自动建议查询词(自动补全): image

2. ES中查询建议的API

查询建议也是使用_search端点地址。在DSL中suggest节点来定义需要的建议查询

示例1:定义单个建议查询词
POST twitter/_search
{"query" : {"match": {"message": "tring out Elasticsearch"}},"suggest" : { <!-- 定义建议查询 -->"my-suggestion" : { <!-- 一个建议查询名 -->"text" : "tring out Elasticsearch", <!-- 查询文本 -->"term" : { <!-- 使用词项建议器 -->"field" : "message" <!-- 指定在哪个字段上获取建议词 -->}}}
}PUT  index
{"mappings":{"completion":{"properties":{"title": {"type": "text","analyzer": "ik_smart"},"title_suggest": {"type": "completion","analyzer": "ik_smart","search_analyzer": "ik_smart"}}}}}

  

示例2:定义多个建议查询词
POST _search
{"suggest": {"my-suggest-1" : {"text" : "tring out Elasticsearch","term" : {"field" : "message"}},"my-suggest-2" : {"text" : "kmichy","term" : {"field" : "user"}}}
}

 

示例3:多个建议查询可以使用全局的查询文本
POST _search
{"suggest": {"text" : "tring out Elasticsearch","my-suggest-1" : {"term" : {"field" : "message"}},"my-suggest-2" : {"term" : {"field" : "user"}}}
}

 

二、Suggester 介绍

1. Term suggester

term 词项建议器,对给入的文本进行分词,为每个词进行模糊查询提供词项建议。对于在索引中存在词默认不提供建议词,不存在的词则根据模糊查询结果进行排序后取一定数量的建议词。

常用的建议选项: image

示例1:

POST twitter/_search
{"query" : {"match": {"message": "tring out Elasticsearch"}},"suggest" : { <!-- 定义建议查询 -->"my-suggestion" : { <!-- 一个建议查询名 -->"text" : "tring out Elasticsearch", <!-- 查询文本 -->"term" : { <!-- 使用词项建议器 -->"field" : "message" <!-- 指定在哪个字段上获取建议词 -->}}}
}

  

2. phrase suggester

phrase 短语建议,在term的基础上,会考量多个term之间的关系,比如是否同时出现在索引的原文里,相邻程度,以及词频等

示例
POST twitter/_search
{"query" : {"match": {"message": "tring out Elasticsearch"}},"suggest" : {"my-suggestion" : {"text" : "tring out Elasticsearch","phrase" : {"field" : "message"}}}
}

 

结果:
{"took": 30,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 2,"max_score": 1.113083,"hits": [{"_index": "twitter","_type": "tweet","_id": "4","_score": 1.113083,"_source": {"user": "kimchy","postDate": "2018-07-23T07:29:57.653Z","message": "trying out Elasticsearch"}},{"_index": "twitter","_type": "tweet","_id": "7","_score": 0.98382175,"_source": {"user": "yuchen20","postDate": "2018-07-23T08:12:05.604Z","message": "trying out Elasticsearch"}}]},"suggest": { <!-- 建议-->"my-suggestion": [{"text": "tring out Elasticsearch","offset": 0,"length": 23,"options": [{{"text": "trying out elasticsearch","score": 0.5118434}]}]}
}

  

3. Completion suggester 自动补全

针对自动补全场景而设计的建议器。此场景下用户每输入一个字符的时候,就需要即时发送一次查询请求到后端查找匹配项,在用户输入速度较高的情况下对后端响应速度要求比较苛刻。因此实现上它和前面两个Suggester采用了不同的数据结构,索引并非通过倒排来完成,而是将analyze过的数据编码成FST和索引一起存放。对于一个open状态的索引,FST会被ES整个装载到内存里的,进行前缀查找速度极快。但是FST只能用于前缀查找,这也是Completion Suggester的局限所在。

官网链接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html

示例1:

为了使用自动补全,索引中用来提供补全建议的字段需特殊设计,字段类型为 completion。 先设置mapping:

PUT  index/
{"mappings":{"completion":{"properties":{"title": {"type": "text","analyzer": "ik_smart"},"title_suggest": {"type": "completion","analyzer": "ik_smart","search_analyzer": "ik_smart"}}}}}

 

重点是title_suggest,这个字段就是之后我们搜索补全的字段,需要设置type为completion,analyzer按情况设置分析器

索引数据:

POST /index/completion/_bulk
{ "index" : { } }
{ "title": "背景天安门广场大学", "title_suggest": "背景天安门广场大学"}
{ "index" : { } }
{ "title": "北京天安门","title_suggest": "北京天安门"}
{ "index" : { } }
{ "title": "北京鸟巢","title_suggest": "北京鸟巢"}
{ "index" : { } }
{ "title": "奥林匹克公园","title_suggest": "奥林匹克公园"}
{ "index" : { } }
{ "title": "奥林匹克森林公园","title_suggest": "奥林匹克森林公园"}
{ "index" : { } }
{ "title": "北京奥林匹克公园","title_suggest": "北京奥林匹克公园"}
{ "index" : { } }
{ "title": "北京奥林匹克公园","title_suggest": {"input": "我爱中国","weight": 100}}

 

索引的时候可以对suggest字段,增加weight增加排序权重

搜索补全:

POST /index/completion/_search
{"size": 0,"suggest":{"blog-suggest":{"prefix":"北京","completion":{"field":"title_suggest"}}}
}

 

结果:

{"took": 3,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 0,"max_score": 0,"hits": []},"suggest": {"blog-suggest": [{"text": "北京","offset": 0,"length": 2,"options": [{"text": "北京天安门","_index": "index","_type": "completion","_id": "AWSRo_hn9K_aupETR6FR","_score": 1,"_source": {"title": "北京天安门","title_suggest": "北京天安门"}},{"text": "北京奥林匹克公园","_index": "index","_type": "completion","_id": "AWSRo_hn9K_aupETR6FV","_score": 1,"_source": {"title": "北京奥林匹克公园","title_suggest": "北京奥林匹克公园"}},{"text": "北京鸟巢","_index": "index","_type": "completion","_id": "AWSRo_hn9K_aupETR6FS","_score": 1,"_source": {"title": "北京鸟巢","title_suggest": "北京鸟巢"}}]}]}
}

 

示例2:

创建映射

PUT music
{"mappings": {"docc" : {"properties" : {"suggest" : {"type" : "completion"},"title" : {"type": "keyword"}}}}
}

  

Input 指定输入词 Weight 指定排序值(可选)
PUT music/docc/1?refresh
{"suggest" : {"input": [ "Nevermind", "Nirvana" ],"weight" : 34}
}

  

指定不同的排序值:

PUT music/_doc/1?refresh
{"suggest" : [{"input": "Nevermind","weight" : 10},{"input": "Nirvana","weight" : 3}]}

  

放入一条重复数据

PUT music/docc/2?refresh
{"suggest" : {"input": [ "Nevermind", "Nirvana" ],"weight" : 20}
}

 

查询建议根据前缀查询:

POST music/_search?pretty
{"suggest": {"song-suggest" : {"prefix" : "nir", "completion" : { "field" : "suggest" }}}
}

  

对建议查询结果去重: "skip_duplicates": true ,该特性在6.x支持,5.x不支持

POST music/_search?pretty
{"suggest": {"song-suggest" : {"prefix" : "nir", "completion" : { "field" : "suggest","skip_duplicates": true }}    }}

 

查询建议文档存储短语

PUT music/docc/3?refresh
{"suggest" : {"input": [ "lucene solr", "lucene so cool","lucene elasticsearch" ],"weight" : 20}
}PUT music/docc/4?refresh
{"suggest" : {"input": ["lucene solr cool","lucene elasticsearch" ],"weight" : 10}
}

 

查询

POST music/_search?pretty
{"suggest": {"song-suggest" : {"prefix" : "lucene s", "completion" : { "field" : "suggest" }}}}

 三 、java -api

## elasticsearch5.x:查询建议java-api介绍、Suggester 介绍
参考:http://www.mamicode.com/info-detail-2347270.htmlpackage com.youlan.es.util;import java.util.concurrent.ExecutionException;import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.suggest.*;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.elasticsearch.search.suggest.phrase.PhraseSuggestion;
import org.elasticsearch.search.suggest.term.TermSuggestion;public class SuggestDemo {private static Logger logger = LogManager.getRootLogger();//拼写检查(英文)public static void termSuggest(TransportClient client) {// 1、创建search请求//SearchRequest searchRequest = new SearchRequest();SearchRequest searchRequest = new SearchRequest("twitter");// 2、用SearchSourceBuilder来构造查询请求体 ,请仔细查看它的方法,构造各种查询的方法都在这。SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();sourceBuilder.size(0);//做查询建议//词项建议SuggestionBuilder termSuggestionBuilder =SuggestBuilders.termSuggestion("message").text("tring out Elticsearch");//搜索框输入内容:tring out ElticsearchSuggestBuilder suggestBuilder = new SuggestBuilder();suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder);sourceBuilder.suggest(suggestBuilder);searchRequest.source(sourceBuilder);try{//3、发送请求SearchResponse searchResponse = client.search(searchRequest).get();//4、处理响应//搜索结果状态信息if(RestStatus.OK.equals(searchResponse.status())) {// 获取建议结果Suggest suggest = searchResponse.getSuggest();TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user");for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {logger.info("text: " + entry.getText().string());for (TermSuggestion.Entry.Option option : entry) {String suggestText = option.getText().string();//建议内容logger.info("   suggest option : " + suggestText);}}}} catch (InterruptedException | ExecutionException e) {logger.error(e);}/*"suggest": {"my-suggestion": [{"text": "tring","offset": 0,"length": 5,"options": [{"text": "trying","score": 0.8,"freq": 2}]},{"text": "out","offset": 6,"length": 3,"options": []},{"text": "elasticsearch","offset": 10,"length": 13,"options": []}]}*/}public static void phraseSuggest(TransportClient client){//1、创建search请求SearchRequest searchRequest = new SearchRequest("twitter");//2、构造查询qing'qi请求体SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();sourceBuilder.size(0);SuggestionBuilder phraseSuggestBuilder = SuggestBuilders.phraseSuggestion( "message").text("tring out");SuggestBuilder suggestBuilder = new SuggestBuilder();suggestBuilder.addSuggestion("my-suggestion",phraseSuggestBuilder);sourceBuilder.suggest(suggestBuilder);searchRequest.source(sourceBuilder);try {//3、发送请求SearchResponse searchResponse = client.search(searchRequest).get();//4、处理响应//搜索状态信息if (RestStatus.OK.equals(searchResponse.status())){//获得建议Suggest suggest = searchResponse.getSuggest();PhraseSuggestion phraseSuggestion =suggest.getSuggestion("my-suggestion");for (PhraseSuggestion.Entry entry:phraseSuggestion){logger.info("text:"+entry.getText().string());for (PhraseSuggestion.Entry.Option option:entry){String suggestText = option.getText().string();logger.info("   suggest option :"+suggestText);}}}} catch (InterruptedException e) {logger.error("请求出错:"+e);} catch (ExecutionException e) {logger.error(e);}}//自动补全public static void completionSuggester(TransportClient client) {// 1、创建search请求//SearchRequest searchRequest = new SearchRequest();SearchRequest searchRequest = new SearchRequest("music");// 2、用SearchSourceBuilder来构造查询请求体 ,请仔细查看它的方法,构造各种查询的方法都在这。SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();sourceBuilder.size(0);//做查询建议//自动补全/*POST music/_search?pretty{"suggest": {"song-suggest" : {"prefix" : "lucene s","completion" : {"field" : "suggest" ,"skip_duplicates": true}}}}*/SuggestionBuilder termSuggestionBuilder =SuggestBuilders.completionSuggestion("suggest").prefix("lucene s");// .skipDuplicates(true) 6.x去重;SuggestBuilder suggestBuilder = new SuggestBuilder();suggestBuilder.addSuggestion("song-suggest", termSuggestionBuilder);sourceBuilder.suggest(suggestBuilder);searchRequest.source(sourceBuilder);try {//3、发送请求SearchResponse searchResponse = client.search(searchRequest).get();//4、处理响应//搜索结果状态信息if(RestStatus.OK.equals(searchResponse.status())) {// 获取建议结果Suggest suggest = searchResponse.getSuggest();CompletionSuggestion termSuggestion = suggest.getSuggestion("song-suggest");for (CompletionSuggestion.Entry entry : termSuggestion.getEntries()) {logger.info("text: " + entry.getText().string());for (CompletionSuggestion.Entry.Option option : entry) {String suggestText = option.getText().string();logger.info("   suggest option : " + suggestText);}}}} catch (InterruptedException | ExecutionException e) {logger.error(e);}
//        结果:
//        {
//            "took": 7,
//                "timed_out": false,
//                "_shards": {
//            "total": 5,
//                    "successful": 5,
//                    "skipped": 0,
//                    "failed": 0
//        },
//            "hits": {
//            "total": 0,
//                    "max_score": 0,
//                    "hits": []
//        },
//            "suggest": {
//            "song-suggest": [
//            {
//                "text": "lucene s",
//                    "offset": 0,
//                    "length": 8,
//                    "options": [
//                {
//                    "text": "lucene so cool",
//                        "_index": "music",
//                        "_type": "docc",
//                        "_id": "3",
//                        "_score": 20,
//                        "_source": {
//                    "suggest": {
//                        "input": [
//                        "lucene solr",
//                                "lucene so cool",
//                                "lucene elasticsearch"
//                ],
//                        "weight": 20
//                    }
//                }
//                },
//                {
//                    "text": "lucene solr cool",
//                        "_index": "music",
//                        "_type": "docc",
//                        "_id": "4",
//                        "_score": 10,
//                        "_source": {
//                    "suggest": {
//                        "input": [
//                        "lucene solr cool",
//                                "lucene elasticsearch"
//                ],
//                        "weight": 10
//                    }
//                }
//                }
//        ]
//            }
//    ]
//        }
//        }}public static void main(String[] args) {EsClient esClient= new EsClient();try (TransportClient client =esClient.getConnection() ;) {logger.info("---------------- 拼写检查:termSuggest----------------------");termSuggest(client);logger.info("------------------ 短语建议:phraseSuggest--------------------");phraseSuggest(client);logger.info("------------------ 自动补全:completionSuggester--------------------");completionSuggester(client);} catch (Exception e) {logger.error(e);}}
}

  

转载于:https://www.cnblogs.com/hengzhi/p/9374415.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/251120.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

DOM-2 document对象、获取元素、节点、遍历树

一、document获取元素 1. 方法 document.getElementById(‘box’) // 在IE8及以下是不分大小写的&#xff0c;而且name值也能匹配上document.getElementsByClassName(’’) // IE8及以下是用不了的document.getElementsByTagName() 都兼容document.getElementsByName() 用的非…

javascript --- js中的事件

事件实现松耦合: // JS和HTML之间的交互是通过事件实现的. // 事件,就是文档或浏览器窗口中发生一些特定的交互瞬间. // 可以使用侦听器来预定事件,以便事件发生时执行相应的代码. // 这种在传统软件工程中被称为观察员模式的模型,支持页面的行为与页面的外观之间的松耦合事件…

centos系统设置局域网静态IP

---恢复内容开始--- centos系统设置局域网静态IP 很多时候&#xff0c;我们并不希望漏油器重启之后&#xff0c;自己的服务器动态的获取IP&#xff0c;这样很不利&#xff0c;因为你可能装了mysql&#xff0c;redis&#xff0c;等软件&#xff0c;然后需要远程去访问这台服务器…

DOM-3 【utils/待讲评】节点属性、方法、封装方法、DOM结构

讲评 节点属性 nodeType 元素节点 1 大写 属性节点 2 文本节点 3 #text 注释节点 8 #comment document 9 DocumentFragment 11 nodeName是只读属性元素节点的nodeName是大写的其余的是#小写的元素节点没有nodeValue属性&#xff0c;null&#xff0c;是可写的其余有&#xff08…

javascript --- DOM0级、DOM2级、跨浏览器 的事件处理程序

DOM0级事件处理程序: // 使用DOM0级方法指定的事件处理程序被认为是元素的方法 // 这个时候的事件处理程序是在元素的作用域中运行: <div id "myBtn" >DOM0</div> <script>var btn document.getElementById("myBtn");btn.onclick fun…

DOM-4 【utils/待讲评】节点创建删除、元素属性设置获取、节点属性

讲评 节点创建 Document.prototype ← document.createElement(div)document.createTextNode(xxx) // 创建文本节点document.createComment(xxx) // 创建注释节点 增加/剪切子节点 Node.prototype ← node.appendChild(node)总是在父元素的最后增加&#xff08;类似push&am…

javascript --- 事件对象和事件类型

// 无论程序使用"DOM0级"规范还是"DOM2级"规范,都会在局部产生一个event对象, // 将其打印出来研究: <div id"divBtn"><button id"rawBtn" >Click Me!</button></div> <script>const divBtn document…

关于APP更新,两包共存的解决方案

大多数是由于包名不一致导致的&#xff0c;一定要注意包名一致、不然手机会认为是两个app&#xff0c;导致两个包共存 查看包名的方法 1、hbuilder打包时会有提示 2、手机打开设置->应用->正在运行->对应app即可查看 转载于:https://www.cnblogs.com/zz-0128/p/827969…

DOM-5【兼容】滚动距离与高度、兼容模式、可视尺寸

查看滚动条的距离 方法版本window.pageXOffset / pageYOffsetIE9/IE8及以下不支持(常规)document.body.scrollLeft/scrollTopIE9/IE8及以下(部分支持&#xff0c;具体看模式)document.documentElement.scrollLeft/scrollTopIE9/IE8及以下(部分支持&#xff0c;具体看模式)wind…

DOM-6 【兼容】读写样式属性、操作伪元素、运动元素初探

规范 <style type"text/css"></style><script type"text/javascript"></script>读写样式属性 .style是访问不到css样式表的&#xff0c;只能访问到行内/内联的属性&#xff0c;当未设置行内属性时&#xff0c;结果为空字符串 设…

RGBD论文阅读笔记

Single-Image Depth Perception in the Wild 简要记载一下对于利用RGB图像depth图像得到segmentation的论文。 论文名称&#xff1a;Cascaded Feature Network for Semantic Segmentation of RGB-D Images 论文出处 ICCV 2017 Lin 模型结构 数据集 NYUDv2 SUN-RGBD 性能分析 总…

vue组件调用(全局调用和局部调用)

当用vue-cli创建一个项目后, 创建项目的方法: https://www.cnblogs.com/fps2tao/p/9376847.html 编写了组件怎么,在其他组件中调用了? 组件listBox: 路径 src/components/listBox.vue <template><div class"listBox">listBox222</div> </templ…

DOM-7 【兼容】事件处理函数、冒泡捕获、阻止冒泡默认事件

事件处理函数 绑定事件 绑定事件的处理函数&#xff08;事件本身是存在的&#xff0c;绑定的是相应的反馈&#xff09;事件 事件的反馈 前端交互 &#xff08;前端核心&#xff09; 绑定事件的方式 1. 句柄绑定 默认&#xff08;只能&#xff09;是事件冒泡这种事件流为…

DOM-8 【兼容】冒泡捕获流、事件与事件源对象、事件委托

事件流 1. 含义 描述从页面中接收事件的顺序 2. 分类 IE提出的 事件冒泡流 Event BubblingNetscape提出的 事件捕获流 Event Capturing 3. 阶段 事件捕获阶段 处于目标阶段 事件冒泡阶段 事件捕获先于事件冒泡执行 dom dom0&#xff1a;定义句柄方式&#xff0c;兼…

IIS6.0,Apache低版本,PHP CGI 解析漏洞

IIS6.0解析漏洞 在IIS6.0下存在这样的文件“名字.asp;名字.jpg” 代表了jpg文件可以以asp脚本类型的文件执行。 根据这个解析漏洞我们可以上传这种名字类型的图片&#xff0c;比如 1.asp;xxx.jpg 他会忽略;后边的内容&#xff0c;所以文件相当于1.asp asp一句话木马 <%eval …

UVA 3485 Bridge

题目大意 你的任务是修建一座大桥。桥上等距地摆放着若干个塔&#xff0c;塔高为H&#xff0c;宽度忽略不计。相邻两座塔之间的距离不能超过D。塔之间的绳索形成全等的对称抛物线。桥长度为B&#xff0c;绳索总长为L&#xff0c;如下图所示求建最少的塔时绳索的最下端离地的高度…

ES5-拓展 this指向的总结

每个函数在执行时都有自己的this指向 1. 默认绑定规则&#xff1a; 全局中&#xff0c;this指向window this window函数的独立调用&#xff0c;this默认指向window &#xff08;不要把独立调用理解成window调用&#xff09; // 函数在全局中调用&#xff0c;也就是被window调…

国内初创企业选择云计算服务器价格对比

假设我们是一家初创企业&#xff0c;现在要选择第一台服务器。业务还未确定&#xff0c;只知道是互联网业务 要求&#xff1a; 至少2核&#xff0c;否则多线程的软件都跑不了至少4G内存&#xff0c;这样可以运行数据库或者elasticsearch这种比较吃内存的东西。或者说&#xff0…

ES5-拓展 箭头函数的this、this的优先级

让内部函数的this指向和父函数的this&#xff0c;可以显示改变this指向&#xff0c;也可以使用箭头函数 箭头代替了function关键字 箭头函数内部没有this指向&#xff0c;箭头函数的this是稳定引用父作用域的。因为它的存在就是要稳定this指向。 适用于普通函数的所有绑定规则…

java 实现压缩单个文件

先来实现一个简单的单文件压缩&#xff0c;主要是为了解一下压缩需要使用到的流。。 效果&#xff1a; 说明&#xff1a;压缩实现使用ZipOutputStream 代码&#xff1a; package com.gx.compress;import java.io.BufferedInputStream; import java.io.BufferedOutputStream; im…