Elasticsearch7.X 入门学习第三课笔记----search api学习(URI Search)

原文: Elasticsearch7.X 入门学习第三课笔记----search api学习(URI Search)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_36697880/article/details/100545466

实现对es中存储的数据进行查询分析,endpoint为_search,查询主要有两种形式:

     URI Search:操作简便,方便通过命令行测试,仅包含部分查询语法
     Request Body Search:es提供完备查询语法Query DSL(Domain Specific Language)


URI Search简单介绍


通过url query参数来实现搜索,常用参数如下:

q: 指定查询语句,语法为 Query String Syntax
df: q中不指定字段时默认查询的字段,如果不指定,es会查询所有字段
sort:排序
timeout:指定超时时间,默认不超时
from,size:用于分页



   
   
  1. ##指定字段查询和 ?q= 2012&df=title一样
  2. GET /movies/_search?q= title: 2012&sort= year: asc&from= 0&size= 10

   

将查询语句通过http request body 发送到es,主要包含如下参数:

  • query: 符合Query DSL语法的查询语句
  • from,size
  • timeout
  • sort

扫描二维码关注公众号,回复: 7398112 查看本文章

URI Search详解

 

Query String Syntax

    1 泛查询与指定字段查询

         q=2012/q= title:2012

      泛查询 全字段查询 不字段字段 

     


   
   
  1. ## 泛查询 和 指定字段查询
  2. ##指定字段查询和 ?q=2012&df=title一样
  3. GET /movies/_search?q= title: 2012&sort=year:asc& from= 0&size= 10
  4. #泛查询
  5. GET /movies/_search?q= 2012&sort=year:asc& from= 0&size= 10

   2 、term与phrase查询

      alfred way 等效于 alfred OR way

      "alfred way" 词语查询,phrase查询要求先后顺序  

       


   
   
  1. # 查找美丽心灵, Mind为泛查询
  2. GET /movies/_search?q=title:Beautiful Mind
  3. {
  4. "profile": "true"
  5. }
  6. #使用引号,Phrase查询 同时满足 且位置一样
  7. GET /movies/_search?q=title: "Beautiful Mind"
  8. {
  9. "profile": "true"
  10. }

  3、 Group分组指定,使用括号指定匹配的规则

    (quick OR brown) AND fox
    status:(active OR pending) title:(full text search)

     使用()表示分组 title:(full text search)  相当于 full or text or search


   
   
  1. #分组,Bool查询 只要包含 Beautiful或者 Mind就可以
  2. GET /movies/_search?q=title:(Beautiful Mind)
  3. {
  4. "profile": "true"
  5. }
  6. GET /movies/_search?q=title:(Beautiful AND Mind)
  7. {
  8. "profile": "true"
  9. }

   4、布尔操作符

         


   
   
  1. ##布尔操作符
  2. ## AND / OR / NOT 或者 && / || / ! 都必须大写
  3. #分组 + -
  4. # + 表示 must AND
  5. #- 表示 not must
  6. GET /movies/_search?q=title:(+Beautiful -Mind)
  7. #phrase查询
  8. GET /movies/_search?q=title:(Beautiful && Mind)
  9. {
  10. "profile": "true"
  11. }
  12. GET /movies/_search?q=title:(Beautiful OR Mind )
  13. {
  14. "profile": "true"
  15. }
  16. #存在Beautiful 且不包含 Mind
  17. GET /movies/_search?q=title:(Beautiful NOT Mind )
  18. {
  19. "profile": "true"
  20. }
  21. # 查找美丽心灵 或的关系,有beautiful或者有mind
  22. GET /movies/_search?q=title:(Beautiful % 2B Mind)
  23. {
  24. "profile": "true"
  25. }

 5、范围查询,支持数值和日志

     1)区间写法,闭区间用[],开区间用{}


   
   
  1.  age: [ 1 TO 10]意为 1<=age<= 10
  2. age: [ 1 TO 10}意为 1<=age< 10
  3. age: [ 1 TO ]意为 age>= 1
  4.  age: [* TO 10]意为 age<= 10

     

2)算数符号写法

     age:>=1
      age:(>=1&&<=10)或者age:(+>=1 +<=10)


6、 通配符查询代表一个字符,*代表0或多个字符       

    name:t?m
    name:tom*

      通配符匹配执行效率低,且占用较多内存,不建议使用
 
    如无特殊需求,不要将?/*放在最前面


7、正则表达式

      name:/[mb]oat/
8、模糊匹配 fuzzy query


   
   
  1. name :roam~1
  2. 匹配与 roam差一个 character的词,比如 foam roams

9、近似度查询 proximity search     


   
   
  1. "fox quick"~ 5
  2. 以term为单位进行差异比较,比如 "quick fox" "quick brown fox"都会被匹配

10 测试实例


   
   
  1. # 查找美丽心灵 或的关系,有beautiful或者有mind
  2. GET /movies/_search?q=title:(Beautiful % 2B Mind)
  3. {
  4. "profile": "true"
  5. }
  6. #范围查询 ,区间写法 % 7D表示}
  7. #闭区间[ 2002 TO 2018] 开区间{ 2002 TO 2018}
  8. #组合 {* TO 2018]
  9. #范围查询 ,区间写法
  10. GET /movies/_search?q=title:beautiful AND year:[ 2002 TO 2018% 7D
  11. {
  12. "profile": "true"
  13. }
  14. #下面如果不写 AND 表示 or的查询关系
  15. GET /movies/_search?q=title:beautiful year:[ 2002 TO 2018]
  16. {
  17. "profile": "true"
  18. }
  19. ##运算符号
  20. ## year:(> 2012 AND <= 2018) year:(> 2012) year:(-> 2012 +<= 2018)
  21. GET /movies/_search?q=title:beautiful AND year:(-> 2012 +<= 2018)
  22. {
  23. "profile": "true"
  24. }
  25. #通配符查询 *表示 0个或者多个 ?代表 1个字符
  26. GET /movies/_search?q=title:b*
  27. {
  28. "profile": "true"
  29. }
  30. #模糊匹配&近似度匹配 ~ 1 表示 beautifl 值出现一个错别字
  31. GET /movies/_search?q=title:beautifl~ 1
  32. {
  33. "profile": "true"
  34. }
  35. ## "Lord Rings"~ 2 表示 "Lord Rings" 之间允许出现两个单词
  36. #~ 3之间允许出现小于等于 3个单词
  37. GET /movies/_search?q=title: "Lord Rings"~ 3
  38. {
  39. "profile": "true"
  40. }

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/11612416.html