1.term查询

  1. {
  2. "query": {
  3. "term": {
  4. "title": "crime"
  5. }
  6. }
  7. }

1.1.指定权重

  1. {
  2. "query": {
  3. "term": {
  4. "title": {
  5. "value":"crime",
  6. "boost":10.0
  7. }
  8. }
  9. }
  10. }

1.2.多term查询查询tags字段中包含novel或book

  1. {
  2. "query": {
  3. "terms": {
  4. "tags": ["novel","book"]
  5. }
  6. }
  7. }

2.常用词查询

2.1.cutoff_frequency查询低于这个概率的词将

  1. {
  2. "query": {
  3. "common": {
  4. "title":{
  5. "query":"crime and punishment",
  6. "cutoff_frequency":0.001
  7. }
  8. }
  9. }
  10. }

2.2.match查询( 不支持lucene查询语法,分词后再查询 )

查询title包含crime或and或punishment的文档

  1. {
  2. "query": {
  3. "match": {
  4. "title": "crime and punishment"
  5. }
  6. }
  7. }

2.3.operator操作符

要求and或者or匹配文本的分词

  1. {
  2. "query": {
  3. "match": {
  4. "title": {
  5. "query":"crime and punishment",
  6. "operator":"and"
  7. }
  8. }
  9. }
  10. }

2.4.短语查询

  1. {
  2. "query": {
  3. "match_phrase": {
  4. "title": {
  5. "query":"crime punishment",
  6. "slop":1
  7. }
  8. }
  9. }
  10. }

2.5.前缀查询

对查询关键词的最后一个词条做前缀匹配

  1. {
  2. "query": {
  3. "match_phrase_prefix": {
  4. "title": {
  5. "query":"crime punish",
  6. "slop":1,
  7. "max_expansions":20
  8. }
  9. }
  10. }
  11. }

2.6.multi_match( 针对多个字段查询 )

  1. {
  2. "query": {
  3. "multi_match": {
  4. "query":"crime heller",
  5. "fields":["title","author"]
  6. }
  7. }
  8. }

3.query_string查询( 支持lucene的查询语法 )

3.1复合语法查询

title字段包含crime,且权重为10,也要包含punishment,但是otitle不包含cat,同事author字段包含Fyodor和dostoevsky。

  1. {
  2. "query": {
  3. "query_string": {
  4. "query":"title:crime^10 +title:punishment -otitle:cat +author:(+Fyodor +dostoevsky)",
  5. "default_field":"title"
  6. }
  7. }
  8. }

3.2.针对多字段查询

use_dis_max使用最大分查询,max指对于给定的关键词,只有最高分才会包括在最后的文档的评分中,而不是所有包含该词条的所有字段分数之和。

  1. {
  2. "query": {
  3. "query_string": {
  4. "query":"crime heller",
  5. "fields":["title","author"],
  6. "use_dis_max":true
  7. }
  8. }
  9. }

常见写法:

  1. {“query”:{“query_string”:{“name:obama”}}}
  2. name字段为obama
  3. {“query”:{“query_string”:{“nam\\*:obama”}}}
  4. 存在一个nam开头的字段,值为obama
  5. {“query”:{“query_string”:{“__missing__:name”}}}
  6. name字段值为null的文档
  7. {“query”:{“query_string”:{“__exists__:name”}}}
  8. name字段值不为null的文档
  9. {“query”:{“query_string”:{“name:(obama OR xidada)”}}}

name字段为Obama或者xidada的文档

3.3.simple_query_string查询

解析出错时不抛异常,丢弃查询无效的部分

  1. {
  2. "query": {
  3. "simple_query_string": {
  4. "query":"title:crime^10 +title:punishment -otitle:cat +author:(+Fyodor +dostoevsky)",
  5. "default_operator":"or"
  6. }
  7. }
  8. }

3.4.标识符查询

  1. {
  2. "query": {
  3. "ids": {
  4. "type":"book",
  5. "values":["1","2","3"]
  6. }
  7. }
  8. }

3.4.前缀查询

前缀匹配给定的关键词

  1. {
  2. "query": {
  3. "prefix": {
  4. "title":"cri"
  5. }
  6. }
  7. }

指定权重

  1. {
  2. "query": {
  3. "prefix": {
  4. "title":{
  5. "value":"cri",
  6. "boost":3.0
  7. }
  8. }
  9. }
  10. }

3.5.fuzzy模糊查询

使用编辑距离的模糊查询,计算量较大,但是对用户拼写错的场景比较有用

  1. {
  2. "query": {
  3. "fuzzy": {
  4. "title":"crme"
  5. }
  6. }
  7. }

指定最小相似度偏差

  1. {
  2. "query": {
  3. "fuzzy": {
  4. "title":{
  5. "value":"crme",
  6. "min_similarity":1
  7. }
  8. }
  9. }
  10. }

3.6.通配符查询

支持*和?等通配符

  1. {
  2. "query": {
  3. "wildcard": {
  4. "title": "cr?me"
  5. }
  6. }
  7. }

?:任意字符
*:0个或任意多个字符
性能差,必须扫描整个倒排索引,才ok

3.7.范围查询

只能针对单个字段,可以是数值型的,也可以是基于字符串的。

  1. {
  2. "query": {
  3. "range": {
  4. "year": {
  5. "gte" :1890,
  6. "lte":1900
  7. }
  8. }
  9. }
  10. }

3.8.正则表达式查询

查询性能取决于正则表达式

  1. {
  2. "query": {
  3. "regexp": {
  4. "title": {
  5. "value" :"cr.m[ae]",
  6. "boost":10.0 //配置评分乘以10
  7. }
  8. }
  9. }
  10. }

K[A-Z].+
[0-9]:指定范围内的数字
[a-z]:指定范围内的字母
.:一个字符
+:前面的正则表达式可以出现一次或多次
wildcard和regexp,与prefix原理一致,都会扫描整个索引,性能很差

4.布尔查询( 组合查询 )

  1. {
  2. "query": {
  3. "bool": {
  4. "must": {
  5. "term": {
  6. "title": "crime"
  7. }
  8. },
  9. "should": {
  10. "range": {
  11. "year": {
  12. "from": 1900,
  13. "to": 2000
  14. }
  15. }
  16. },
  17. "must_not": {
  18. "term": {
  19. "otitle": "nothing"
  20. }
  21. }
  22. }
  23. }
  24. }

mus:必须包含的条件,must not:不包含 ,should:包含的话会更匹配
搜索多个条件:

  1. GET test*/_search
  2. {
  3. "size":3,
  4. "query": {
  5. "bool":{
  6. "must": [
  7. {"match":{"message": "学生"}},
  8. {"match":{"message": "所有"}}
  9. ],
  10. "should": [
  11. {"match": {"port": "53198"}},
  12. {"match": {"@timestamp":"2018-09-17T17:49:25.991Z"}}
  13. ],
  14. "must_not": [
  15. {"match": {"port": "64273"}},
  16. {"match": {"port":"1234"}}
  17. ]
  18. }
  19. }
  20. }

结果:

  1. {
  2. "took": 25,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 35,
  6. "successful": 35,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 14,
  12. "max_score": 17.026089,
  13. "hits": [
  14. {
  15. "_index": "test-name",
  16. "_type": "doc",
  17. "_id": "Ff0g6GUBPXqEl7zCsbQb",
  18. "_score": 17.026089,
  19. "_source": {
  20. "@timestamp": "2018-09-17T15:23:06.878Z",
  21. "appname": "test-name",
  22. "level": "INFO",
  23. "port": 55714,
  24. "thread_name": "main",
  25. "level_value": 20000,
  26. "appName": "test-name",
  27. "@version": 1,
  28. "host": "192.168.1.100",
  29. "logger_name": "com.example.service.StudentService",
  30. "@metdata": {
  31. "ip_address": "192.168.1.100"
  32. },
  33. "message": "查询所有学生,pageNo1,pageSize1"
  34. }
  35. },
  36. {
  37. "_index": "test-name",
  38. "_type": "doc",
  39. "_id": "WFOm6GUBlATfpgHyvD55",
  40. "_score": 16.024178,
  41. "_source": {
  42. "@timestamp": "2018-09-17T17:49:25.991Z",
  43. "appname": "test-name",
  44. "level": "INFO",
  45. "port": 53198,
  46. "thread_name": "main",
  47. "level_value": 20000,
  48. "appName": "test-name",
  49. "@version": 1,
  50. "host": "192.168.1.100",
  51. "logger_name": "com.example.service.StudentService",
  52. "@metdata": {
  53. "ip_address": "192.168.1.100"
  54. },
  55. "message": "查询所有学生,pageNo1,pageSize1"
  56. }
  57. },
  58. {
  59. "_index": "test-name",
  60. "_type": "doc",
  61. "_id": "nAMg42UBRHcv2wBhnFDg",
  62. "_score": 14.024178,
  63. "_source": {
  64. "@timestamp": "2018-09-16T16:04:54.948Z",
  65. "appname": "test-name",
  66. "level": "INFO",
  67. "port": 58709,
  68. "thread_name": "main",
  69. "level_value": 20000,
  70. "appName": "test-name",
  71. "@version": 1,
  72. "host": "172.20.10.6",
  73. "logger_name": "com.example.service.StudentService",
  74. "@metdata": {
  75. "ip_address": "172.20.10.6"
  76. },
  77. "message": "查询所有学生,pageNo1,pageSize1"
  78. }
  79. }
  80. ]
  81. }
  82. }

还可以这么实现:

  1. GET test*/_search
  2. {
  3. "size":3,
  4. "query": {
  5. "query_string":{"query": "message:学生 +message:所有 -port:55714"}
  6. }
  7. }