一、查询建议介绍

1. 查询建议是什么?

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

拼写检查如图:

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

2. ES中查询建议的API

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

示例1:定义单个建议查询词

  1. POST twitter/_search
  2. {
  3. "query" : {
  4. "match": {
  5. "message": "tring out Elasticsearch"
  6. }
  7. },
  8. "suggest" : { <!-- 定义建议查询 -->
  9. "my-suggestion" : { <!-- 一个建议查询名 -->
  10. "text" : "tring out Elasticsearch", <!-- 查询文本 -->
  11. "term" : { <!-- 使用词项建议器 -->
  12. "field" : "message" <!-- 指定在哪个字段上获取建议词 -->
  13. }
  14. }
  15. }
  16. }

示例2:定义多个建议查询词

  1. POST _search
  2. {
  3. "suggest": {
  4. "my-suggest-1" : {
  5. "text" : "tring out Elasticsearch",
  6. "term" : {
  7. "field" : "message"
  8. }
  9. },
  10. "my-suggest-2" : {
  11. "text" : "kmichy",
  12. "term" : {
  13. "field" : "user"
  14. }
  15. }
  16. }
  17. }

示例3:多个建议查询可以使用全局的查询文本

  1. POST _search
  2. {
  3. "suggest": {
  4. "text" : "tring out Elasticsearch",
  5. "my-suggest-1" : {
  6. "term" : {
  7. "field" : "message"
  8. }
  9. },
  10. "my-suggest-2" : {
  11. "term" : {
  12. "field" : "user"
  13. }
  14. }
  15. }
  16. }

二、Suggester 介绍

1. Term suggester

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

常用的建议选项:

示例1:

  1. POST twitter/_search
  2. {
  3. "query" : {
  4. "match": {
  5. "message": "tring out Elasticsearch"
  6. }
  7. },
  8. "suggest" : { <!-- 定义建议查询 -->
  9. "my-suggestion" : { <!-- 一个建议查询名 -->
  10. "text" : "tring out Elasticsearch", <!-- 查询文本 -->
  11. "term" : { <!-- 使用词项建议器 -->
  12. "field" : "message" <!-- 指定在哪个字段上获取建议词 -->
  13. }
  14. }
  15. }
  16. }

2. phrase suggester

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

示例1:

  1. POST /ftq/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6.  
  7. "suggest" : {
  8. "myss":{
  9. "text": "java sprin boot",
  10. "phrase": {
  11. "field": "title"
  12. }
  13. }
  14. }
  15. }

结果1:

  1. {
  2. "took": 177,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 2,
  12. "max_score": 1,
  13. "hits": [
  14. {
  15. "_index": "ftq",
  16. "_type": "_doc",
  17. "_id": "2",
  18. "_score": 1,
  19. "_source": {
  20. "title": "java spring boot",
  21. "content": "lucene is writerd by java"
  22. }
  23. },
  24. {
  25. "_index": "ftq",
  26. "_type": "_doc",
  27. "_id": "1",
  28. "_score": 1,
  29. "_source": {
  30. "title": "lucene solr and elasticsearch",
  31. "content": "lucene solr and elasticsearch for search"
  32. }
  33. }
  34. ]
  35. },
  36. "suggest": {
  37. "myss": [
  38. {
  39. "text": "java sprin boot",
  40. "offset": 0,
  41. "length": 15,
  42. "options": [
  43. {
  44. "text": "java spring boot",
  45. "score": 0.20745796
  46. }
  47. ]
  48. }
  49. ]
  50. }
  51. }

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

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

  1. PUT music
  2. {
  3. "mappings": {
  4. "_doc" : {
  5. "properties" : {
  6. "suggest" : { <!-- 用于自动补全的字段 -->
  7. "type" : "completion"
  8. },
  9. "title" : {
  10. "type": "keyword"
  11. }
  12. }
  13. }
  14. }
  15. }

Input 指定输入词 Weight 指定排序值(可选)

  1. PUT music/_doc/1?refresh
  2. {
  3. "suggest" : {
  4. "input": [ "Nevermind", "Nirvana" ],
  5. "weight" : 34
  6. }
  7. }

指定不同的排序值:

  1. PUT music/_doc/1?refresh
  2. {
  3. "suggest" : [
  4. {
  5. "input": "Nevermind",
  6. "weight" : 10
  7. },
  8. {
  9. "input": "Nirvana",
  10. "weight" : 3
  11. }
  12. ]}

放入一条重复数据

  1. PUT music/_doc/2?refresh
  2. {
  3. "suggest" : {
  4. "input": [ "Nevermind", "Nirvana" ],
  5. "weight" : 20
  6. }
  7. }

示例1:查询建议根据前缀查询:

  1. POST music/_search?pretty
  2. {
  3. "suggest": {
  4. "song-suggest" : {
  5. "prefix" : "nir",
  6. "completion" : {
  7. "field" : "suggest"
  8. }
  9. }
  10. }
  11. }

结果1:

  1. {
  2. "took": 25,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 0,
  12. "max_score": 0,
  13. "hits": []
  14. },
  15. "suggest": {
  16. "song-suggest": [
  17. {
  18. "text": "nir",
  19. "offset": 0,
  20. "length": 3,
  21. "options": [
  22. {
  23. "text": "Nirvana",
  24. "_index": "music",
  25. "_type": "_doc",
  26. "_id": "2",
  27. "_score": 20,
  28. "_source": {
  29. "suggest": {
  30. "input": [
  31. "Nevermind",
  32. "Nirvana"
  33. ],
  34. "weight": 20
  35. }
  36. }
  37. },
  38. {
  39. "text": "Nirvana",
  40. "_index": "music",
  41. "_type": "_doc",
  42. "_id": "1",
  43. "_score": 1,
  44. "_source": {
  45. "suggest": [
  46. "Nevermind",
  47. "Nirvana"
  48. ]
  49. }
  50. }
  51. ]
  52. }
  53. ]
  54. }
  55. }

示例2:对建议查询结果去重

  1. POST music/_search?pretty
  2. {
  3. "suggest": {
  4. "song-suggest" : {
  5. "prefix" : "nir",
  6. "completion" : {
  7. "field" : "suggest",
  8. "skip_duplicates": true
  9. }
  10. } }}

结果2:

  1. {
  2. "took": 4,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 0,
  12. "max_score": 0,
  13. "hits": []
  14. },
  15. "suggest": {
  16. "song-suggest": [
  17. {
  18. "text": "nir",
  19. "offset": 0,
  20. "length": 3,
  21. "options": [
  22. {
  23. "text": "Nirvana",
  24. "_index": "music",
  25. "_type": "_doc",
  26. "_id": "2",
  27. "_score": 20,
  28. "_source": {
  29. "suggest": {
  30. "input": [
  31. "Nevermind",
  32. "Nirvana"
  33. ],
  34. "weight": 20
  35. }
  36. }
  37. }
  38. ]
  39. }
  40. ]
  41. }
  42. }

示例3:查询建议文档存储短语

  1. PUT music/_doc/3?refresh
  2. {
  3. "suggest" : {
  4. "input": [ "lucene solr", "lucene so cool","lucene elasticsearch" ],
  5. "weight" : 20
  6. }
  7. }
  8.  
  9. PUT music/_doc/4?refresh
  10. {
  11. "suggest" : {
  12. "input": ["lucene solr cool","lucene elasticsearch" ],
  13. "weight" : 10
  14. }
  15. }

查询3:

  1. POST music/_search?pretty
  2. {
  3. "suggest": {
  4. "song-suggest" : {
  5. "prefix" : "lucene s",
  6. "completion" : {
  7. "field" : "suggest" ,
  8. "skip_duplicates": true
  9. }
  10. }
  11. }
  12. }

结果3:

  1. {
  2. "took": 3,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 0,
  12. "max_score": 0,
  13. "hits": []
  14. },
  15. "suggest": {
  16. "song-suggest": [
  17. {
  18. "text": "lucene s",
  19. "offset": 0,
  20. "length": 8,
  21. "options": [
  22. {
  23. "text": "lucene so cool",
  24. "_index": "music",
  25. "_type": "_doc",
  26. "_id": "3",
  27. "_score": 20,
  28. "_source": {
  29. "suggest": {
  30. "input": [
  31. "lucene solr",
  32. "lucene so cool",
  33. "lucene elasticsearch"
  34. ],
  35. "weight": 20
  36. }
  37. }
  38. },
  39. {
  40. "text": "lucene solr cool",
  41. "_index": "music",
  42. "_type": "_doc",
  43. "_id": "4",
  44. "_score": 10,
  45. "_source": {
  46. "suggest": {
  47. "input": [
  48. "lucene solr cool",
  49. "lucene elasticsearch"
  50. ],
  51. "weight": 10
  52. }
  53. }
  54. }
  55. ]
  56. }
  57. ]
  58. }
  59. }

elasticsearch系列五:搜索详解(查询建议介绍、Suggester 介绍)的更多相关文章

  1. Elastic Stack 笔记(六)Elasticsearch5.6 搜索详解

    博客地址:http://www.moonxy.com 一.前言 Elasticsearch 主要包含索引过程和搜索过程. 索引过程:一条文档被索引到 Elasticsearch 之后,默认情况下 ES ...

  2. nginx高性能WEB服务器系列之四配置文件详解

    nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...

  3. Hexo系列(二) 配置文件详解

    Hexo 是一款优秀的博客框架,在使用 Hexo 搭建一个属于自己的博客网站后,我们还需要对其进行配置,使得 Hexo 更能满足自己的需求 这里所说的配置文件,是位于站点根目录下的 _config.y ...

  4. Ubuntu14.04下Ambari安装搭建部署大数据集群(图文分五大步详解)(博主强烈推荐)

    不多说,直接上干货! 写在前面的话 (1) 最近一段时间,因担任我团队实验室的大数据环境集群真实物理机器工作,至此,本人秉持负责.认真和细心的态度,先分别在虚拟机上模拟搭建ambari(基于CentO ...

  5. Ubuntu14.04下Cloudera安装搭建部署大数据集群(图文分五大步详解)(博主强烈推荐)(在线或离线)

    第一步: Cloudera Manager安装之Cloudera Manager安装前准备(Ubuntu14.04)(一) 第二步: Cloudera Manager安装之时间服务器和时间客户端(Ub ...

  6. Elasticsearch shield权限管理详解

    Elasticsearch shield权限管理详解 学习了:https://blog.csdn.net/napoay/article/details/52201558 现在(20180424)改名为 ...

  7. mongo 3.4分片集群系列之六:详解配置数据库

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  8. mongo 3.4分片集群系列之五:详解平衡器

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  9. css3系列之transform详解translate

    translate translate这个参数的,是transform 身上的,那么它有什么用呢? 其实他的作用很简单,就是平移,参考自己的位置来平移 translate() translateX() ...

  10. ThreeJS系列1_CinematicCameraJS插件详解

    ThreeJS系列1_CinematicCameraJS插件详解 接着上篇 ThreeJS系列1_CinematicCameraJS插件介绍 看属性的来龙去脉 看方法作用 通过调整属性查看效果 总结 ...

随机推荐

  1. CCZone

    /**************************************************************************** Copyright (c) 2010 coc ...

  2. js中实现对checkbox选中和取消

    可以使用 element.attr('checked','checked') 来进行选中.但是不能使用 element.attr('checked','false') 来取消选中. 必须通过以下方式: ...

  3. 【驱动】linux设备驱动·扫盲

    linux设备驱动 Linux系统把设备驱动分成字符设备.块设备和网络设备三种类型. 内核为设备驱动提供了注册和管理的接口,设备驱动还可以使用内核提供的其他功能以及访问内核资源. PCI局部总线 早期 ...

  4. c#:使用using关键字自动释放资源未必一定就会有明显好处

    public string ToXML() { string strXml = string.Empty; try { MemoryStream ms = new MemoryStream(); Xm ...

  5. idea svn 使用问题

    一开始死活提交不上 解决方式 : 勾上use command line client

  6. java多线程16:join()的使用

    讲解join()方法之前请确保对于即wait()/notify()/notifyAll()机制已熟练掌握.可以参考前面的笔记 join()方法的作用是等待线程销毁.join()方法反应的是一个很现实的 ...

  7. tf.square

    tf.square square( x, name=None ) 功能说明: 计算张量对应元素平方 参数列表: 参数名 必选 类型 说明 x 是 张量 是 half, float32, float64 ...

  8. 设计模式之观察者模式(关于OC中的KVO\KVC\NSNotification)

    学习了这么久的设计模式方面的知识,最大的感触就是,设计模式不能脱离语言特性.近段时间所看的两本书籍,<大话设计模式>里面的代码是C#写的,有一些设计模式实现起来也是采用了C#的语言特性(C ...

  9. burpsuite两个变量的爱情故事

    抓包的时候在攻击类型处选择[Cluster bomb] 在payload type这里设置类型为[simple list] 第一个是账号 第二个是密码 分批加载即可

  10. Python的可视化包 – Matplotlib 2D图表(点图和线图,.柱状或饼状类型的图),3D图表(曲面图,散点图和柱状图)

    Python的可视化包 – Matplotlib Matplotlib是Python中最常用的可视化工具之一,可以非常方便地创建海量类型地2D图表和一些基本的3D图表.Matplotlib最早是为了可 ...