1. 单字段 模糊匹配查询与精准查询
  1. postman请求
  2.  
  3. POST 127.0.0.1:9200/book/_search
  4.  
  5. 请求json
  6.  
  7. {
  8. "query":{
  9. "match":{
  10. "name":"晓明9"
  11. }
  12. }
  13. }
  14.  
  15. 注:match 模糊查询的标识 :查询内容自动拆分成分词来查询
      match 改为 match_phrase :精准查询 具体可以查看 http://www.cnblogs.com/liuxiaoming123/p/8119217.html
      
  16.  
  17. 响应结果:
  18.  
  19. {
  20. "took": 51,
  21. "timed_out": false,
  22. "_shards": {
  23. "total": 5,
  24. "successful": 5,
  25. "skipped": 0,
  26. "failed": 0
  27. },
  28. "hits": {
  29. "total": 4,
  30. "max_score": 0.5753642,
  31. "hits": [
  32. {
  33. "_index": "book",
  34. "_type": "novel",
  35. "_id": "1",
  36. "_score": 0.5753642,
  37. "_source": {
  38. "name": "晓明1",
  39. "country": "china1",
  40. "age": 26,
  41. "date": "1992-08-08"
  42. }
  43. },
  44. {
  45. "_index": "book",
  46. "_type": "novel",
  47. "_id": "5",
  48. "_score": 0.28363907,
  49. "_source": {
  50. "name": "晓明",
  51. "country": "china",
  52. "age": 26,
  53. "date": "1992-08-08"
  54. }
  55. },
  56. {
  57. "_index": "book",
  58. "_type": "novel",
  59. "_id": "8",
  60. "_score": 0.28363907,
  61. "_source": {
  62. "name": "晓明",
  63. "country": "china",
  64. "age": 26,
  65. "date": "1992-08-08"
  66. }
  67. },
  68. {
  69. "_index": "book",
  70. "_type": "novel",
  71. "_id": "9",
  72. "_score": 0.23911436,
  73. "_source": {
  74. "name": "晓明9",
  75. "country": "china9",
  76. "age": 26,
  77. "date": "1992-08-08"
  78. }
  79. }
  80. ]
  81. }
  82. }

  1. 多字段 模糊匹配查询与精准查询
  1. postman请求URL
  2.  
  3. POST 127.0.0.1:9200/book/_search
  4.  
  5. 请求json字符串:
  6.  
  7. {
  8. "query":{
  9. "multi_match":{
  10. "query":"晓明china",
  11. "fields":["name","country"]
  12. }
  13. }
  14. }
  15.  
  16. 注:multi_match为指定多字段匹配
  17.  
  18. 响应结果:
  19.  
  20. {
        "took": 42,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 4,
            "max_score": 0.5753642,
            "hits": [
                {
                    "_index": "book",
                    "_type": "novel",
                    "_id": "1",
                    "_score": 0.5753642,
                    "_source": {
                        "name": "晓明1",
                        "country": "china1",
                        "age": 26,
                        "date": "1992-08-08"
                    }
                },
                {
                    "_index": "book",
                    "_type": "novel",
                    "_id": "5",
                    "_score": 0.47000363,
                    "_source": {
                        "name": "晓明",
                        "country": "china",
                        "age": 26,
                        "date": "1992-08-08"
                    }
                },
                {
                    "_index": "book",
                    "_type": "novel",
                    "_id": "8",
                    "_score": 0.47000363,
                    "_source": {
                        "name": "晓明",
                        "country": "china",
                        "age": 26,
                        "date": "1992-08-08"
                    }
                },
                {
                    "_index": "book",
                    "_type": "novel",
                    "_id": "9",
                    "_score": 0.23911436,
                    "_source": {
                        "name": "晓明9",
                        "country": "china9",
                        "age": 26,
                        "date": "1992-08-08"
                    }
                }
            ]
        }
    }

  1. 语法查询
    未指定字段:
  1. postman请求:
  2.  
  3. POST 127.0.0.1:9200/book/_search
  4.  
  5. 请求json字符串:
  6.  
  7. {
  8. "query":{
  9. "query_string":{
  10. "query":"(ElasticSearch AND 入门) OR SpringBoot"
  11. }
  12. }
  13. }
  14.  
  15. 返回结果:
  16.  
  17. {
  18. "took": 21,
  19. "timed_out": false,
  20. "_shards": {
  21. "total": 5,
  22. "successful": 5,
  23. "skipped": 0,
  24. "failed": 0
  25. },
  26. "hits": {
  27. "total": 2,
  28. "max_score": 2.634553,
  29. "hits": [
  30. {
  31. "_index": "book",
  32. "_type": "novel",
  33. "_id": "9",
  34. "_score": 2.634553,
  35. "_source": {
  36. "name": "ElasticSearch 入门",
  37. "country": "china9",
  38. "age": 26,
  39. "date": "1992-08-08"
  40. }
  41. },
  42. {
  43. "_index": "book",
  44. "_type": "novel",
  45. "_id": "1",
  46. "_score": 0.2876821,
  47. "_source": {
  48. "name": "SpringBoot",
  49. "country": "china1",
  50. "age": 26,
  51. "date": "1992-08-08"
  52. }
  53. }
  54. ]
  55. }
  56. }

语法查询
 指定字段:

  1. postman请求:
  2.  
  3. POST 127.0.0.1:9200/book/_search
  4.  
  5. 请求json字符串:
  6.  
  7. {
  8. "query":{
  9. "query_string":{
  10. "query":"SpringBoot OR 中国",
  11. "fields":["name","country"]
  12. }
  13. }
  14. }
  15.  
  16. 响应结果:
  17.  
  18. {
  19. "took": 11,
  20. "timed_out": false,
  21. "_shards": {
  22. "total": 5,
  23. "successful": 5,
  24. "skipped": 0,
  25. "failed": 0
  26. },
  27. "hits": {
  28. "total": 1,
  29. "max_score": 0.8630463,
  30. "hits": [
  31. {
  32. "_index": "book",
  33. "_type": "novel",
  34. "_id": "1",
  35. "_score": 0.8630463,
  36. "_source": {
  37. "name": "SpringBoot",
  38. "country": "中国",
  39. "age": 26,
  40. "date": "1992-08-08"
  41. }
  42. }
  43. ]
  44. }
  45. }

(结构化数据的查询)

指定字段查询:(term)

  1. postman请求:
  2.  
  3. POST 127.0.0.1:9200/book/_search
  4.  
  5. 请求json字符串:
  6.  
  7. {
  8. "query" :
  9. {
  10. "term" : {"name" : "springboot"}
  11. }
  12. }
  13.  
  14. 响应结果:
  15.  
  16. {
  17. "took": 4,
  18. "timed_out": false,
  19. "_shards": {
  20. "total": 5,
  21. "successful": 5,
  22. "skipped": 0,
  23. "failed": 0
  24. },
  25. "hits": {
  26. "total": 1,
  27. "max_score": 0.2876821,
  28. "hits": [
  29. {
  30. "_index": "book",
  31. "_type": "novel",
  32. "_id": "1",
  33. "_score": 0.2876821,
  34. "_source": {
  35. "name": "SpringBoot",
  36. "country": "中国",
  37. "age": 26,
  38. "date": "1992-08-08"
  39. }
  40. }
  41. ]
  42. }
  43. }

注:若查询英文时 应全字母小写 精确查询

  若查询中文时 应按单个字来查询

范围查询:

注:json请求字符串中部分字段的含义

  range:范围关键字

  gte 大于等于

  lte  小于等于

  gt 大于

  lt 小于

  now 当前时间

  1. postman请求:
  2.  
  3. 127.0.0.1:9200/book/_search
  4.  
  5. 请求json字符串:
  6.  
  7. {
  8. "query" :
  9. {
  10. "range" : {
  11. "date" : {
  12. "gte":"2017-01-01",
  13. "lte":"now"
  14. }
  15. }
  16. }
  17. }
  18.  
  19. 响应结果:
  20.  
  21. {
  22. "took": 7,
  23. "timed_out": false,
  24. "_shards": {
  25. "total": 5,
  26. "successful": 5,
  27. "skipped": 0,
  28. "failed": 0
  29. },
  30. "hits": {
  31. "total": 1,
  32. "max_score": 1,
  33. "hits": [
  34. {
  35. "_index": "book",
  36. "_type": "novel",
  37. "_id": "9",
  38. "_score": 1,
  39. "_source": {
  40. "name": "ElasticSearch 入门",
  41. "country": "china9",
  42. "age": 28,
  43. "date": "2017-08-08"
  44. }
  45. }
  46. ]
  47. }
  48. }

Filter Context(对数据进行过滤)

  1. postman请求:
  2.  
  3. POST 127.0.0.1:9200/book/_search
  4.  
  5. 请求json字符串:
  6.  
  7. {
  8. "query" : {
  9. "bool" : {
  10. "filter" : {
  11. "term":{
  12. "age":20
  13. }
  14. }
  15. }
  16. }
  17. }
  18.  
  19. 响应结果:
  20.  
  21. {
  22. "took": 24,
  23. "timed_out": false,
  24. "_shards": {
  25. "total": 5,
  26. "successful": 5,
  27. "skipped": 0,
  28. "failed": 0
  29. },
  30. "hits": {
  31. "total": 1,
  32. "max_score": 0,
  33. "hits": [
  34. {
  35. "_index": "book",
  36. "_type": "novel",
  37. "_id": "1",
  38. "_score": 0,
  39. "_source": {
  40. "name": "SpringBoot",
  41. "country": "中国",
  42. "age": 20,
  43. "date": "1992-08-08"
  44. }
  45. }
  46. ]
  47. }
  48. }

注: boost 固定响应结果分数的值

  1. postman请求:
  2.  
  3. POST 127.0.0.1:9200/_search
  4.  
  5. 请求json字符串:
  6.  
  7. {
  8. "query" : {
  9. "constant_score" : {
  10. "filter" : {
  11. "match":{
  12. "name":"晓明"
  13. }
  14. },
  15. "boost":2
  16. }
  17. }
  18. }
  19.  
  20. 响应结果:
  21.  
  22. {
  23. "took": 11,
  24. "timed_out": false,
  25. "_shards": {
  26. "total": 8,
  27. "successful": 8,
  28. "skipped": 0,
  29. "failed": 0
  30. },
  31. "hits": {
  32. "total": 2,
  33. "max_score": 2,
  34. "hits": [
  35. {
  36. "_index": "book",
  37. "_type": "novel",
  38. "_id": "5",
  39. "_score": 2,
  40. "_source": {
  41. "name": "晓明",
  42. "country": "china",
  43. "age": 26,
  44. "date": "1992-08-08"
  45. }
  46. },
  47. {
  48. "_index": "book",
  49. "_type": "novel",
  50. "_id": "8",
  51. "_score": 2,
  52. "_source": {
  53. "name": "晓明8",
  54. "country": "china",
  55. "age": 26,
  56. "date": "1992-08-08"
  57. }
  58. }
  59. ]
  60. }
  61. }

should关键词:或的关系

若should改为must 关键词则表示 和的关系

  1. postman请求:
  2.  
  3. POST 127.0.0.1:9200/_search
  4.  
  5. 请求json字符串:
  6.     1.shuld:
  7. {
  8. "query" : {
  9. "bool" : {
  10. "should" : [
  11. {
  12. "match":{"name":"springboot"}
  13. },
  14. {
  15. "match":{"country":"中国"}
  16. }
  17. ]
  18. }
  19. }
  20. }
  1.  
  1.  2.must:
  2. {
  3. "query" : {
  4. "bool" : {
  5. "must" : [
  6. {
  7. "match":{"name":"springboot"}
  8. },
  9. {
  10. "match":{"country":"中国"}
  11. }
  12. ]
  13. }
  14. }
  15. }
  1.  
  1. 3.must filter:
  2. {
        "query" : {
                "bool" : {
                    "must" : [
                                {
                                    "match":{"name":"springboot"}
                                },
                                {
                                    "match":{"country":"中国"}
                                }
                                ],
                       "filter":[
                                {
                                    "term":{
                                        "age":20
                                    }
                                }    
                                ]
                            }
                  }
    }
  1.  
  1. 4.must_not:
    {
        "query" : {
                "bool" : {
                    "must_not" : {
                                    "term":{"age":20}
                                 }
                          }
                   }
    }
  1.  

ElasticSearch入门3: 高级查询的更多相关文章

  1. Elasticsearch入门,看这一篇就够了

    目录 前言 可视化工具 kibana kibana 的安装 kibana 配置 kibana 的启动 Elasticsearch 入门操作 操作 index 创建 index 索引别名有什么用 删除索 ...

  2. ElasticSearch高级查询

    ElasticSearch高级查询 https://www.imooc.com/video/15759/0 ElasticSearch查询 1,子条件查询:特定字段查询所指特定值 1.1query c ...

  3. elasticsearch 高级查询

    高级查询 子条件查询 (特定字段查询所指特定值) 复合条件查询 (以一定的逻辑组合子条件查询) 一.子条件查询 子条件查询分为 query context.filter context 1.query ...

  4. ElasticSearch入门 第九篇:实现正则表达式查询的思路

    这是ElasticSearch 2.4 版本系列的第九篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  5. ElasticSearch入门 第五篇:使用C#查询文档

    这是ElasticSearch 2.4 版本系列的第五篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  6. 031 Spring Data Elasticsearch学习笔记---重点掌握第5节高级查询和第6节聚合部分

    Elasticsearch提供的Java客户端有一些不太方便的地方: 很多地方需要拼接Json字符串,在java中拼接字符串有多恐怖你应该懂的 需要自己把对象序列化为json存储 查询到结果也需要自己 ...

  7. Elasticsearch入门教程(六):Elasticsearch查询(二)

    原文:Elasticsearch入门教程(六):Elasticsearch查询(二) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:h ...

  8. Elasticsearch入门教程(五):Elasticsearch查询(一)

    原文:Elasticsearch入门教程(五):Elasticsearch查询(一) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:h ...

  9. java整合Elasticsearch,实现crud以及高级查询的分页,范围,排序功能,泰文分词器的使用,分组,最大,最小,平均值,以及自动补全功能

    //为index创建mapping,index相当于mysql的数据库,数据库里的表也要给各个字段创建类型,所以index也要给字段事先设置好类型: 使用postMan或者其他工具创建:(此处我使用p ...

随机推荐

  1. javascript总结集合

    前言:下面的总结都是自己测试过的,绝大多数都是没问题的,但是不敢保证一定全面,还需要诸君自己独立思考: 1)基础:在nodejs里全局对象是global,而网页里js的全局对象是window:对于所有 ...

  2. keras model.compile(loss='目标函数 ', optimizer='adam', metrics=['accuracy'])

    深度学习笔记 目标函数的总结与整理   目标函数,或称损失函数,是网络中的性能函数,也是编译一个模型必须的两个参数之一.由于损失函数种类众多,下面以keras官网手册的为例. 在官方keras.io里 ...

  3. 5W2H+35问

    之前看到的一个非常好且全面的做事方法论.

  4. 成功解决在Python文件上右键菜单无“Edit with IDLE”选项

    我电脑是Win7旗舰版,之前电脑上安装的是Python2.6版本的,前两天为了体验一下Microsoft Excel与Python之间互操作, 下载并安装了DataNitro,在安装的时候脑残的安装了 ...

  5. 入手IntelliJ IDEA 常用配置

    Idea常用设置 下载地址:https://www.jetbrains.com/idea/ 激活服务器:http://idea.iteblog.com/key.php 代码补全取消区分大小写 Inte ...

  6. POJ3111 K Best 2017-05-11 18:12 31人阅读 评论(0) 收藏

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 10261   Accepted: 2644 Case Time ...

  7. Android-Java静态代码块&局部代码块

    静态代码块: 静态代码块什么时候执行,是由当前类被加载进内存的时候,什么时候当前类被加载进内存? 答:例如 描述好了Student对象,当 new Student(); 的时候,第一步:是把Stude ...

  8. mysql数据导入导出方法总结

    MySQL数据备份还原方式总结: 一.将数据导入到指定的数据库 第一种导入方式: (linux下和Windows 下语法是一样的,只是路径的书写方式不同而已) 1.创建一个空数据库 2.进入MySQL ...

  9. 应该知道的Linux技巧【转】

    这篇文章来源于Quroa的一个问答<What are some time-saving tips that every Linux user should know?>—— Linux用户 ...

  10. 查看指定spid的脚本当前运行情况和状态

    USE MasterGO declare @spid int; select @spid = 419--null:all ; ;WITH DATA(spid,blockRelationship,blo ...