当前位置:   article > 正文

elasticsearch最全详细使用教程:搜索详解_es搜索

es搜索

一、搜索API

 

1. 搜索API 端点地址

从索引tweet里面搜索字段user为kimchy的记录

GET /twitter/_search?q=user:kimchy

从索引tweet,user里面搜索字段user为kimchy的记录

  1. GET /twitter/tweet,user/_search?q=user:kimchy
  2. GET /kimchy,elasticsearch/_search?q=tag:wow

从所有索引里面搜索字段tag为wow的记录

  1. GET /_all/_search?q=tag:wow
  2. GET /_search?q=tag:wow

说明:搜索的端点地址可以是多索引多mapping type的。搜索的参数可作为URI请求参数给出,也可用 request body 给出

2. URI Search

URI 搜索方式通过URI参数来指定查询相关参数。让我们可以快速做一个查询。

GET /twitter/_search?q=user:kimchy

可用的参数请参考: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html

3. 查询结果说明

5. 特殊的查询参数用法

 如果我们只想知道有多少文档匹配某个查询,可以这样用参数:

GET /bank/_search?q=city:b*&size=0

 

 

 

 如果我们只想知道有没有文档匹配某个查询,可以这样用参数:

GET /bank/_search?q=city:b*&size=0&terminate_after=1

 

 

 

 比较两个查询的结果可以知道第一个查询返回所有的命中文档数,第二个查询由于只需要知道有没有文档,所以只要有文档就立即返回

 6. Request body Search

 Request body 搜索方式以JSON格式在请求体中定义查询 query。请求方式可以是 GET 、POST 。

  1. GET /twitter/_search
  2. {
  3. "query" : {
  4. "term" : { "user" : "kimchy" }
  5. }
  6. }

可用的参数:

timeout:请求超时时长,限定在指定时长内响应(即使没查完);
from: 分页的起始行,默认0;
size:分页大小;
request_cache:是否缓存请求结果,默认true。
terminate_after:限定每个分片取几个文档。如果设置,则响应将有一个布尔型字段terminated_early来指示查询执行是否实际已经terminate_early。缺省为no terminate_after;
search_type:查询的执行方式,可选值dfs_query_then_fetch or query_then_fetch ,默认: query_then_fetch ;
batched_reduce_size:一次在协调节点上应该减少的分片结果的数量。如果请求中的潜在分片数量可能很大,则应将此值用作保护机制以减少每个搜索请求的内存开销。

6.1 query 元素定义查询

query 元素用Query DSL 来定义查询。

  1. GET /_search
  2. {
  3. "query" : {
  4. "term" : { "user" : "kimchy" }
  5. }
  6. }

6.2 指定返回哪些内容

6.2.1 source filter  对_source字段进行选择

  1. GET /_search
  2. {
  3. "_source": false,
  4. "query" : {
  5. "term" : { "user" : "kimchy" }
  6. }
  7. }

通配符查询

  1. GET /_search
  2. {
  3. "_source": [ "obj1.*", "obj2.*" ],
  4. "query" : {
  5. "term" : { "user" : "kimchy" }
  6. }
  7. }
  8. GET /_search
  9. {
  10. "_source": "obj.*",
  11. "query" : {
  12. "term" : { "user" : "kimchy" }
  13. }
  14. }

包含什么不包含什么

  1. GET /_search
  2. {
  3. "_source": {
  4. "includes": [ "obj1.*", "obj2.*" ],
  5. "excludes": [ "*.description" ]
  6. },
  7. "query" : {
  8. "term" : { "user" : "kimchy" }
  9. }
  10. }

6.2.2 stored_fields 来指定返回哪些stored字段

  1. GET /_search
  2. {
  3. "stored_fields" : ["user", "postDate"],
  4. "query" : {
  5. "term" : { "user" : "kimchy" }
  6. }
  7. }

说明:* 可用来指定返回所有存储字段

6.2.3 docValue Field 返回存储了docValue的字段值

  1. GET /_search
  2. {
  3. "query" : {
  4. "match_all": {}
  5. },
  6. "docvalue_fields" : ["test1", "test2"]
  7. }

6.2.4 version 来指定返回文档的版本字段

  1. GET /_search
  2. {
  3. "version": true,
  4. "query" : {
  5. "term" : { "user" : "kimchy" }
  6. }
  7. }

6.2.5 explain 返回文档的评分解释

  1. GET /_search
  2. {
  3. "explain": true,
  4. "query" : {
  5. "term" : { "user" : "kimchy" }
  6. }
  7. }

6.2.6 Script Field 用脚本来对命中的每个文档的字段进行运算后返回

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "script_fields": {
  7. "test1": {
  8. "script": {
  9. "lang": "painless",
  10. "source": "doc['balance'].value * 2"
  11. }
  12. },
  13. "test2": {
  14. "script": {
  15. "lang": "painless",
  16. <!-- doc指文档-->
  17. "source": "doc['age'].value * params.factor",
  18. "params": {
  19. "factor": 2
  20. }
  21. }
  22. } }}

搜索结果:

  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": 1000,
  12. "max_score": 1,
  13. "hits": [
  14. {
  15. "_index": "bank",
  16. "_type": "_doc",
  17. "_id": "25",
  18. "_score": 1,
  19. "fields": {
  20. "test1": [
  21. ],
  22. "test2": [
  23. ]
  24. }
  25. },
  26. {
  27. "_index": "bank",
  28. "_type": "_doc",
  29. "_id": "44",
  30. "_score": 1,
  31. "fields": {
  32. "test1": [
  33. ],
  34. "test2": [
  35. ]
  36. }
  37. },
  38. {
  39. "_index": "bank",
  40. "_type": "_doc",
  41. "_id": "99",
  42. "_score": 1,
  43. "fields": {
  44. "test1": [
  45. ],
  46. "test2": [
  47. ]
  48. }
  49. },
  50. {
  51. "_index": "bank",
  52. "_type": "_doc",
  53. "_id": "119",
  54. "_score": 1,
  55. "fields": {
  56. "test1": [
  57. ],
  58. "test2": [
  59. ]
  60. }
  61. },
  62. {
  63. "_index": "bank",
  64. "_type": "_doc",
  65. "_id": "126",
  66. "_score": 1,
  67. "fields": {
  68. "test1": [
  69. ],
  70. "test2": [
  71. ]
  72. }
  73. },
  74. {
  75. "_index": "bank",
  76. "_type": "_doc",
  77. "_id": "145",
  78. "_score": 1,
  79. "fields": {
  80. "test1": [
  81. ],
  82. "test2": [
  83. ]
  84. }
  85. },
  86. {
  87. "_index": "bank",
  88. "_type": "_doc",
  89. "_id": "183",
  90. "_score": 1,
  91. "fields": {
  92. "test1": [
  93. ],
  94. "test2": [
  95. ]
  96. }
  97. },
  98. {
  99. "_index": "bank",
  100. "_type": "_doc",
  101. "_id": "190",
  102. "_score": 1,
  103. "fields": {
  104. "test1": [
  105. ],
  106. "test2": [
  107. ]
  108. }
  109. },
  110. {
  111. "_index": "bank",
  112. "_type": "_doc",
  113. "_id": "208",
  114. "_score": 1,
  115. "fields": {
  116. "test1": [
  117. ],
  118. "test2": [
  119. ]
  120. }
  121. },
  122. {
  123. "_index": "bank",
  124. "_type": "_doc",
  125. "_id": "222",
  126. "_score": 1,
  127. "fields": {
  128. "test1": [
  129. ],
  130. "test2": [
  131. ]
  132. }
  133. }
  134. ]
  135. }
  136. }
  1. GET /bank/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "script_fields": {
  7. "ffx": {
  8. "script": {
  9. "lang": "painless",
  10. "source": "doc['age'].value * doc['balance'].value"
  11. }
  12. },
  13. "balance*2": {
  14. "script": {
  15. "lang": "painless",
  16. "source": "params['_source'].balance*2"
  17. }
  18. }
  19. }
  20. }

说明:

params  _source 取 _source字段值

官方推荐使用doc,理由是用doc效率比取_source 高

搜索结果:

  1. {
  2. "took": 26,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1000,
  12. "max_score": 1,
  13. "hits": [
  14. {
  15. "_index": "bank",
  16. "_type": "_doc",
  17. "_id": "25",
  18. "_score": 1,
  19. "fields": {
  20. "balance*2": [
  21. ],
  22. "ffx": [
  23. ]
  24. }
  25. },
  26. {
  27. "_index": "bank",
  28. "_type": "_doc",
  29. "_id": "44",
  30. "_score": 1,
  31. "fields": {
  32. "balance*2": [
  33. ],
  34. "ffx": [
  35. ]
  36. }
  37. },
  38. {
  39. "_index": "bank",
  40. "_type": "_doc",
  41. "_id": "99",
  42. "_score": 1,
  43. "fields": {
  44. "balance*2": [
  45. ],
  46. "ffx": [
  47. ]
  48. }
  49. },
  50. {
  51. "_index": "bank",
  52. "_type": "_doc",
  53. "_id": "119",
  54. "_score": 1,
  55. "fields": {
  56. "balance*2": [
  57. ],
  58. "ffx": [
  59. ]
  60. }
  61. },
  62. {
  63. "_index": "bank",
  64. "_type": "_doc",
  65. "_id": "126",
  66. "_score": 1,
  67. "fields": {
  68. "balance*2": [
  69. ],
  70. "ffx": [
  71. ]
  72. }
  73. },
  74. {
  75. "_index": "bank",
  76. "_type": "_doc",
  77. "_id": "145",
  78. "_score": 1,
  79. "fields": {
  80. "balance*2": [
  81. ],
  82. "ffx": [
  83. ]
  84. }
  85. },
  86. {
  87. "_index": "bank",
  88. "_type": "_doc",
  89. "_id": "183",
  90. "_score": 1,
  91. "fields": {
  92. "balance*2": [
  93. ],
  94. "ffx": [
  95. ]
  96. }
  97. },
  98. {
  99. "_index": "bank",
  100. "_type": "_doc",
  101. "_id": "190",
  102. "_score": 1,
  103. "fields": {
  104. "balance*2": [
  105. ],
  106. "ffx": [
  107. ]
  108. }
  109. },
  110. {
  111. "_index": "bank",
  112. "_type": "_doc",
  113. "_id": "208",
  114. "_score": 1,
  115. "fields": {
  116. "balance*2": [
  117. ],
  118. "ffx": [
  119. ]
  120. }
  121. },
  122. {
  123. "_index": "bank",
  124. "_type": "_doc",
  125. "_id": "222",
  126. "_score": 1,
  127. "fields": {
  128. "balance*2": [
  129. ],
  130. "ffx": [
  131. ]
  132. }
  133. }
  134. ]
  135. }
  136. }

6.2.7 min_score  限制最低评分得分

  1. GET /_search
  2. {
  3. "min_score": 0.5,
  4. "query" : {
  5. "term" : { "user" : "kimchy" }
  6. }
  7. }

6.2.8 post_filter  后置过滤:在查询命中文档、完成聚合后,再对命中的文档进行过滤。

如:要在一次查询中查询品牌为gucci且颜色为红色的shirts,同时还要得到gucci品牌各颜色的shirts的分面统计。

创建索引并指定mappping:

  1. PUT /shirts
  2. {
  3. "mappings": {
  4. "_doc": {
  5. "properties": {
  6. "brand": { "type": "keyword"},
  7. "color": { "type": "keyword"},
  8. "model": { "type": "keyword"}
  9. }
  10. }
  11. }
  12. }

往索引里面放入文档即类似数据库里面的向表插入一行数据,并立即刷新

  1. PUT /shirts/_doc/1?refresh
  2. {
  3. "brand": "gucci",
  4. "color": "red",
  5. "model": "slim"
  6. }
  7. PUT /shirts/_doc/2?refresh
  8. {
  9. "brand": "gucci",
  10. "color": "green",
  11. "model": "seec"
  12. }

执行查询:

  1. GET /shirts/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "filter": {
  6. "term": { "brand": "gucci" }
  7. }
  8. }
  9. },
  10. "aggs": {
  11. "colors": {
  12. "terms": { "field": "color" }
  13. }
  14. },
  15. "post_filter": {
  16. "term": { "color": "red" }
  17. }
  18. }

查询结果

  1. {
  2. "took": 109,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1,
  12. "max_score": 0,
  13. "hits": [
  14. {
  15. "_index": "shirts",
  16. "_type": "_doc",
  17. "_id": "1",
  18. "_score": 0,
  19. "_source": {
  20. "brand": "gucci",
  21. "color": "red",
  22. "model": "slim"
  23. }
  24. }
  25. ]
  26. },
  27. "aggregations": {
  28. "colors": {
  29. "doc_count_error_upper_bound": 0,
  30. "sum_other_doc_count": 0,
  31. "buckets": [
  32. {
  33. "key": "green",
  34. "doc_count": 1
  35. },
  36. {
  37. "key": "red",
  38. "doc_count": 1
  39. }
  40. ]
  41. }
  42. }
  43. }

6.2.9 sort  排序

可以指定按一个或多个字段排序。也可通过_score指定按评分值排序,_doc 按索引顺序排序。默认是按相关性评分从高到低排序。

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "sort": [
  7. {
  8. "age": {
  9. "order": "desc"
  10. } },
  11. {
  12. "balance": {
  13. "order": "asc"
  14. } },
  15. "_score"
  16. ]
  17. }

说明:

order 值:asc、desc。如果不给定,默认是asc,_score默认是desc

查询结果:

  1. {
  2. "took": 181,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1000,
  12. "max_score": null,
  13. "hits": [
  14. {
  15. "_index": "bank",
  16. "_type": "_doc",
  17. "_id": "549",
  18. "_score": 1,
  19. "_source": {
  20. "account_number": 549,
  21. "balance": 1932,
  22. "firstname": "Jacqueline",
  23. "lastname": "Maxwell",
  24. "age": 40,
  25. "gender": "M",
  26. "address": "444 Schenck Place",
  27. "employer": "Fuelworks",
  28. "email": "jacquelinemaxwell@fuelworks.com",
  29. "city": "Oretta",
  30. "state": "OR"
  31. },
  32. "sort": [
  33. 40,
  34. 1932,
  35. ]
  36. },
  37. {
  38. "_index": "bank",
  39. "_type": "_doc",
  40. "_id": "306",
  41. "_score": 1,
  42. "_source": {
  43. "account_number": 306,
  44. "balance": 2171,
  45. "firstname": "Hensley",
  46. "lastname": "Hardin",
  47. "age": 40,
  48. "gender": "M",
  49. "address": "196 Maujer Street",
  50. "employer": "Neocent",
  51. "email": "hensleyhardin@neocent.com",
  52. "city": "Reinerton",
  53. "state": "HI"
  54. },
  55. "sort": [
  56. 40,
  57. 2171,
  58. ]
  59. },
  60. {
  61. "_index": "bank",
  62. "_type": "_doc",
  63. "_id": "960",
  64. "_score": 1,
  65. "_source": {
  66. "account_number": 960,
  67. "balance": 2905,
  68. "firstname": "Curry",
  69. "lastname": "Vargas",
  70. "age": 40,
  71. "gender": "M",
  72. "address": "242 Blake Avenue",
  73. "employer": "Pearlesex",
  74. "email": "curryvargas@pearlesex.com",
  75. "city": "Henrietta",
  76. "state": "NH"
  77. },
  78. "sort": [
  79. 40,
  80. 2905,
  81. ]
  82. },
  83. {
  84. "_index": "bank",
  85. "_type": "_doc",
  86. "_id": "584",
  87. "_score": 1,
  88. "_source": {
  89. "account_number": 584,
  90. "balance": 5346,
  91. "firstname": "Pearson",
  92. "lastname": "Bryant",
  93. "age": 40,
  94. "gender": "F",
  95. "address": "971 Heyward Street",
  96. "employer": "Anacho",
  97. "email": "pearsonbryant@anacho.com",
  98. "city": "Bluffview",
  99. "state": "MN"
  100. },
  101. "sort": [
  102. 40,
  103. 5346,
  104. ]
  105. },
  106. {
  107. "_index": "bank",
  108. "_type": "_doc",
  109. "_id": "567",
  110. "_score": 1,
  111. "_source": {
  112. "account_number": 567,
  113. "balance": 6507,
  114. "firstname": "Diana",
  115. "lastname": "Dominguez",
  116. "age": 40,
  117. "gender": "M",
  118. "address": "419 Albany Avenue",
  119. "employer": "Ohmnet",
  120. "email": "dianadominguez@ohmnet.com",
  121. "city": "Wildwood",
  122. "state": "TX"
  123. },
  124. "sort": [
  125. 40,
  126. 6507,
  127. ]
  128. },
  129. {
  130. "_index": "bank",
  131. "_type": "_doc",
  132. "_id": "938",
  133. "_score": 1,
  134. "_source": {
  135. "account_number": 938,
  136. "balance": 9597,
  137. "firstname": "Sharron",
  138. "lastname": "Santos",
  139. "age": 40,
  140. "gender": "F",
  141. "address": "215 Matthews Place",
  142. "employer": "Zenco",
  143. "email": "sharronsantos@zenco.com",
  144. "city": "Wattsville",
  145. "state": "VT"
  146. },
  147. "sort": [
  148. 40,
  149. 9597,
  150. ]
  151. },
  152. {
  153. "_index": "bank",
  154. "_type": "_doc",
  155. "_id": "810",
  156. "_score": 1,
  157. "_source": {
  158. "account_number": 810,
  159. "balance": 10563,
  160. "firstname": "Alyssa",
  161. "lastname": "Ortega",
  162. "age": 40,
  163. "gender": "M",
  164. "address": "977 Clymer Street",
  165. "employer": "Eventage",
  166. "email": "alyssaortega@eventage.com",
  167. "city": "Convent",
  168. "state": "SC"
  169. },
  170. "sort": [
  171. 40,
  172. 10563,
  173. ]
  174. },
  175. {
  176. "_index": "bank",
  177. "_type": "_doc",
  178. "_id": "302",
  179. "_score": 1,
  180. "_source": {
  181. "account_number": 302,
  182. "balance": 11298,
  183. "firstname": "Isabella",
  184. "lastname": "Hewitt",
  185. "age": 40,
  186. "gender": "M",
  187. "address": "455 Bedford Avenue",
  188. "employer": "Cincyr",
  189. "email": "isabellahewitt@cincyr.com",
  190. "city": "Blanford",
  191. "state": "IN"
  192. },
  193. "sort": [
  194. 40,
  195. 11298,
  196. ]
  197. },
  198. {
  199. "_index": "bank",
  200. "_type": "_doc",
  201. "_id": "792",
  202. "_score": 1,
  203. "_source": {
  204. "account_number": 792,
  205. "balance": 13109,
  206. "firstname": "Becky",
  207. "lastname": "Jimenez",
  208. "age": 40,
  209. "gender": "F",
  210. "address": "539 Front Street",
  211. "employer": "Isologia",
  212. "email": "beckyjimenez@isologia.com",
  213. "city": "Summertown",
  214. "state": "MI"
  215. },
  216. "sort": [
  217. 40,
  218. 13109,
  219. ]
  220. },
  221. {
  222. "_index": "bank",
  223. "_type": "_doc",
  224. "_id": "495",
  225. "_score": 1,
  226. "_source": {
  227. "account_number": 495,
  228. "balance": 13478,
  229. "firstname": "Abigail",
  230. "lastname": "Nichols",
  231. "age": 40,
  232. "gender": "F",
  233. "address": "887 President Street",
  234. "employer": "Enquility",
  235. "email": "abigailnichols@enquility.com",
  236. "city": "Bagtown",
  237. "state": "NM"
  238. },
  239. "sort": [
  240. 40,
  241. 13478,
  242. ]
  243. }
  244. ]
  245. }
  246. }

结果中每个文档会有排序字段值给出

  1. "hits": {
  2. "total": 1000,
  3. "max_score": null,
  4. "hits": [
  5. {
  6. "_index": "bank",
  7. "_type": "_doc",
  8. "_id": "549",
  9. "_score": 1,
  10. "_source": {
  11. "account_number": 549,
  12. "balance": 1932, "age": 40, "state": "OR"
  13. },
  14. "sort": [
  15. 40,
  16. 1932,
  17. 1
  18. ]
  19. }

 

多值字段排序

对于值是数组或多值的字段,也可进行排序,通过mode参数指定按多值的:

  1. PUT /my_index/_doc/1?refresh
  2. {
  3. "product": "chocolate",
  4. "price": [20, 4]
  5. }
  6. POST /_search
  7. {
  8. "query" : {
  9. "term" : { "product" : "chocolate" }
  10. },
  11. "sort" : [
  12. {"price" : {"order" : "asc", "mode" : "avg"}}
  13. ]
  14. }

 Missing values  缺失该字段的文档

missing 的值可以是 _last, _first

  1. GET /_search
  2. {
  3. "sort" : [
  4. { "price" : {"missing" : "_last"} }
  5. ],
  6. "query" : {
  7. "term" : { "product" : "chocolate" }
  8. }
  9. }

 地理空间距离排序

官方文档:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#geo-sorting

  1. GET /_search
  2. {
  3. "sort" : [
  4. {
  5. "_geo_distance" : {
  6. "pin.location" : [-70, 40],
  7. "order" : "asc",
  8. "unit" : "km",
  9. "mode" : "min",
  10. "distance_type" : "arc"
  11. }
  12. }
  13. ],
  14. "query" : {
  15. "term" : { "user" : "kimchy" }
  16. }
  17. }

参数说明:

_geo_distance 距离排序关键字
pin.location是 geo_point 类型的字段
distance_type:距离计算方式 arc球面 、plane 平面。
unit: 距离单位 km 、m 默认m

Script Based Sorting 基于脚本计算的排序

  1. GET /_search
  2. {
  3. "query" : {
  4. "term" : { "user" : "kimchy" }
  5. },
  6. "sort" : {
  7. "_script" : {
  8. "type" : "number",
  9. "script" : {
  10. "lang": "painless",
  11. "source": "doc['field_name'].value * params.factor",
  12. "params" : {
  13. "factor" : 1.1
  14. }
  15. },
  16. "order" : "asc"
  17. }
  18. }
  19. }

 6.3.0 折叠用 collapse指定根据某个字段对命中结果进行折叠

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "collapse" : {
  7. "field" : "age"
  8. },
  9. "sort": ["balance"]
  10. }

 查询结果:

  1. {
  2. "took": 56,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1000,
  12. "max_score": null,
  13. "hits": [
  14. {
  15. "_index": "bank",
  16. "_type": "_doc",
  17. "_id": "820",
  18. "_score": null,
  19. "_source": {
  20. "account_number": 820,
  21. "balance": 1011,
  22. "firstname": "Shepard",
  23. "lastname": "Ramsey",
  24. "age": 24,
  25. "gender": "F",
  26. "address": "806 Village Court",
  27. "employer": "Mantro",
  28. "email": "shepardramsey@mantro.com",
  29. "city": "Tibbie",
  30. "state": "NV"
  31. },
  32. "fields": {
  33. "age": [
  34. ]
  35. },
  36. "sort": [
  37. ]
  38. },
  39. {
  40. "_index": "bank",
  41. "_type": "_doc",
  42. "_id": "894",
  43. "_score": null,
  44. "_source": {
  45. "account_number": 894,
  46. "balance": 1031,
  47. "firstname": "Tyler",
  48. "lastname": "Fitzgerald",
  49. "age": 32,
  50. "gender": "M",
  51. "address": "787 Meserole Street",
  52. "employer": "Jetsilk",
  53. "email": "tylerfitzgerald@jetsilk.com",
  54. "city": "Woodlands",
  55. "state": "WV"
  56. },
  57. "fields": {
  58. "age": [
  59. ]
  60. },
  61. "sort": [
  62. ]
  63. },
  64. {
  65. "_index": "bank",
  66. "_type": "_doc",
  67. "_id": "953",
  68. "_score": null,
  69. "_source": {
  70. "account_number": 953,
  71. "balance": 1110,
  72. "firstname": "Baxter",
  73. "lastname": "Black",
  74. "age": 27,
  75. "gender": "M",
  76. "address": "720 Stillwell Avenue",
  77. "employer": "Uplinx",
  78. "email": "baxterblack@uplinx.com",
  79. "city": "Drummond",
  80. "state": "MN"
  81. },
  82. "fields": {
  83. "age": [
  84. ]
  85. },
  86. "sort": [
  87. ]
  88. },
  89. {
  90. "_index": "bank",
  91. "_type": "_doc",
  92. "_id": "87",
  93. "_score": null,
  94. "_source": {
  95. "account_number": 87,
  96. "balance": 1133,
  97. "firstname": "Hewitt",
  98. "lastname": "Kidd",
  99. "age": 22,
  100. "gender": "M",
  101. "address": "446 Halleck Street",
  102. "employer": "Isologics",
  103. "email": "hewittkidd@isologics.com",
  104. "city": "Coalmont",
  105. "state": "ME"
  106. },
  107. "fields": {
  108. "age": [
  109. ]
  110. },
  111. "sort": [
  112. ]
  113. },
  114. {
  115. "_index": "bank",
  116. "_type": "_doc",
  117. "_id": "749",
  118. "_score": null,
  119. "_source": {
  120. "account_number": 749,
  121. "balance": 1249,
  122. "firstname": "Rush",
  123. "lastname": "Boyle",
  124. "age": 36,
  125. "gender": "M",
  126. "address": "310 Argyle Road",
  127. "employer": "Sportan",
  128. "email": "rushboyle@sportan.com",
  129. "city": "Brady",
  130. "state": "WA"
  131. },
  132. "fields": {
  133. "age": [
  134. ]
  135. },
  136. "sort": [
  137. ]
  138. },
  139. {
  140. "_index": "bank",
  141. "_type": "_doc",
  142. "_id": "315",
  143. "_score": null,
  144. "_source": {
  145. "account_number": 315,
  146. "balance": 1314,
  147. "firstname": "Clare",
  148. "lastname": "Morrow",
  149. "age": 33,
  150. "gender": "F",
  151. "address": "728 Madeline Court",
  152. "employer": "Gaptec",
  153. "email": "claremorrow@gaptec.com",
  154. "city": "Mapletown",
  155. "state": "PA"
  156. },
  157. "fields": {
  158. "age": [
  159. ]
  160. },
  161. "sort": [
  162. ]
  163. },
  164. {
  165. "_index": "bank",
  166. "_type": "_doc",
  167. "_id": "348",
  168. "_score": null,
  169. "_source": {
  170. "account_number": 348,
  171. "balance": 1360,
  172. "firstname": "Karina",
  173. "lastname": "Russell",
  174. "age": 37,
  175. "gender": "M",
  176. "address": "797 Moffat Street",
  177. "employer": "Limozen",
  178. "email": "karinarussell@limozen.com",
  179. "city": "Riegelwood",
  180. "state": "RI"
  181. },
  182. "fields": {
  183. "age": [
  184. ]
  185. },
  186. "sort": [
  187. ]
  188. },
  189. {
  190. "_index": "bank",
  191. "_type": "_doc",
  192. "_id": "490",
  193. "_score": null,
  194. "_source": {
  195. "account_number": 490,
  196. "balance": 1447,
  197. "firstname": "Strong",
  198. "lastname": "Hendrix",
  199. "age": 26,
  200. "gender": "F",
  201. "address": "134 Beach Place",
  202. "employer": "Duoflex",
  203. "email": "stronghendrix@duoflex.com",
  204. "city": "Allentown",
  205. "state": "ND"
  206. },
  207. "fields": {
  208. "age": [
  209. ]
  210. },
  211. "sort": [
  212. ]
  213. },
  214. {
  215. "_index": "bank",
  216. "_type": "_doc",
  217. "_id": "174",
  218. "_score": null,
  219. "_source": {
  220. "account_number": 174,
  221. "balance": 1464,
  222. "firstname": "Gamble",
  223. "lastname": "Pierce",
  224. "age": 23,
  225. "gender": "F",
  226. "address": "650 Eagle Street",
  227. "employer": "Matrixity",
  228. "email": "gamblepierce@matrixity.com",
  229. "city": "Abiquiu",
  230. "state": "OR"
  231. },
  232. "fields": {
  233. "age": [
  234. ]
  235. },
  236. "sort": [
  237. ]
  238. },
  239. {
  240. "_index": "bank",
  241. "_type": "_doc",
  242. "_id": "111",
  243. "_score": null,
  244. "_source": {
  245. "account_number": 111,
  246. "balance": 1481,
  247. "firstname": "Traci",
  248. "lastname": "Allison",
  249. "age": 35,
  250. "gender": "M",
  251. "address": "922 Bryant Street",
  252. "employer": "Enjola",
  253. "email": "traciallison@enjola.com",
  254. "city": "Robinette",
  255. "state": "OR"
  256. },
  257. "fields": {
  258. "age": [
  259. ]
  260. },
  261. "sort": [
  262. ]
  263. }
  264. ]
  265. }
  266. }

 高级折叠

  1. GET /bank/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "collapse" : {
  7. "field" : "age" ,
  8. <!--指定inner_hits来解释折叠 -->
  9. "inner_hits": {
  10. "name": "details", <!-- 自命名 -->
  11. "size": 5, <!-- 指定每组取几个文档 -->
  12. "sort": [{ "balance": "asc" }] <!-- 组内排序 -->
  13. },
  14. "max_concurrent_group_searches": 4 <!-- 指定组查询的并发数 -->
  15. },
  16. "sort": ["balance"]
  17. }

 查询结果:

  1. {
  2. "took": 60,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1000,
  12. "max_score": null,
  13. "hits": [
  14. {
  15. "_index": "bank",
  16. "_type": "_doc",
  17. "_id": "820",
  18. "_score": null,
  19. "_source": {
  20. "account_number": 820,
  21. "balance": 1011,
  22. "firstname": "Shepard",
  23. "lastname": "Ramsey",
  24. "age": 24,
  25. "gender": "F",
  26. "address": "806 Village Court",
  27. "employer": "Mantro",
  28. "email": "shepardramsey@mantro.com",
  29. "city": "Tibbie",
  30. "state": "NV"
  31. },
  32. "fields": {
  33. "age": [
  34. ]
  35. },
  36. "sort": [
  37. ],
  38. "inner_hits": {
  39. "details": {
  40. "hits": {
  41. "total": 42,
  42. "max_score": null,
  43. "hits": [
  44. {
  45. "_index": "bank",
  46. "_type": "_doc",
  47. "_id": "820",
  48. "_score": null,
  49. "_source": {
  50. "account_number": 820,
  51. "balance": 1011,
  52. "firstname": "Shepard",
  53. "lastname": "Ramsey",
  54. "age": 24,
  55. "gender": "F",
  56. "address": "806 Village Court",
  57. "employer": "Mantro",
  58. "email": "shepardramsey@mantro.com",
  59. "city": "Tibbie",
  60. "state": "NV"
  61. },
  62. "sort": [
  63. ]
  64. },
  65. {
  66. "_index": "bank",
  67. "_type": "_doc",
  68. "_id": "924",
  69. "_score": null,
  70. "_source": {
  71. "account_number": 924,
  72. "balance": 3811,
  73. "firstname": "Hilary",
  74. "lastname": "Leonard",
  75. "age": 24,
  76. "gender": "M",
  77. "address": "235 Hegeman Avenue",
  78. "employer": "Metroz",
  79. "email": "hilaryleonard@metroz.com",
  80. "city": "Roosevelt",
  81. "state": "ME"
  82. },
  83. "sort": [
  84. ]
  85. },
  86. {
  87. "_index": "bank",
  88. "_type": "_doc",
  89. "_id": "819",
  90. "_score": null,
  91. "_source": {
  92. "account_number": 819,
  93. "balance": 3971,
  94. "firstname": "Karyn",
  95. "lastname": "Medina",
  96. "age": 24,
  97. "gender": "F",
  98. "address": "417 Utica Avenue",
  99. "employer": "Qnekt",
  100. "email": "karynmedina@qnekt.com",
  101. "city": "Kerby",
  102. "state": "WY"
  103. },
  104. "sort": [
  105. ]
  106. },
  107. {
  108. "_index": "bank",
  109. "_type": "_doc",
  110. "_id": "77",
  111. "_score": null,
  112. "_source": {
  113. "account_number": 77,
  114. "balance": 5724,
  115. "firstname": "Byrd",
  116. "lastname": "Conley",
  117. "age": 24,
  118. "gender": "F",
  119. "address": "698 Belmont Avenue",
  120. "employer": "Zidox",
  121. "email": "byrdconley@zidox.com",
  122. "city": "Rockbridge",
  123. "state": "SC"
  124. },
  125. "sort": [
  126. ]
  127. },
  128. {
  129. "_index": "bank",
  130. "_type": "_doc",
  131. "_id": "493",
  132. "_score": null,
  133. "_source": {
  134. "account_number": 493,
  135. "balance": 5871,
  136. "firstname": "Campbell",
  137. "lastname": "Best",
  138. "age": 24,
  139. "gender": "M",
  140. "address": "297 Friel Place",
  141. "employer": "Fanfare",
  142. "email": "campbellbest@fanfare.com",
  143. "city": "Kidder",
  144. "state": "GA"
  145. },
  146. "sort": [
  147. ]
  148. }
  149. ]
  150. }
  151. }
  152. }
  153. },
  154. {
  155. "_index": "bank",
  156. "_type": "_doc",
  157. "_id": "894",
  158. "_score": null,
  159. "_source": {
  160. "account_number": 894,
  161. "balance": 1031,
  162. "firstname": "Tyler",
  163. "lastname": "Fitzgerald",
  164. "age": 32,
  165. "gender": "M",
  166. "address": "787 Meserole Street",
  167. "employer": "Jetsilk",
  168. "email": "tylerfitzgerald@jetsilk.com",
  169. "city": "Woodlands",
  170. "state": "WV"
  171. },
  172. "fields": {
  173. "age": [
  174. ]
  175. },
  176. "sort": [
  177. ],
  178. "inner_hits": {
  179. "details": {
  180. "hits": {
  181. "total": 52,
  182. "max_score": null,
  183. "hits": [
  184. {
  185. "_index": "bank",
  186. "_type": "_doc",
  187. "_id": "894",
  188. "_score": null,
  189. "_source": {
  190. "account_number": 894,
  191. "balance": 1031,
  192. "firstname": "Tyler",
  193. "lastname": "Fitzgerald",
  194. "age": 32,
  195. "gender": "M",
  196. "address": "787 Meserole Street",
  197. "employer": "Jetsilk",
  198. "email": "tylerfitzgerald@jetsilk.com",
  199. "city": "Woodlands",
  200. "state": "WV"
  201. },
  202. "sort": [
  203. ]
  204. },
  205. {
  206. "_index": "bank",
  207. "_type": "_doc",
  208. "_id": "402",
  209. "_score": null,
  210. "_source": {
  211. "account_number": 402,
  212. "balance": 1282,
  213. "firstname": "Pacheco",
  214. "lastname": "Rosales",
  215. "age": 32,
  216. "gender": "M",
  217. "address": "538 Pershing Loop",
  218. "employer": "Circum",
  219. "email": "pachecorosales@circum.com",
  220. "city": "Elbert",
  221. "state": "ID"
  222. },
  223. "sort": [
  224. ]
  225. },
  226. {
  227. "_index": "bank",
  228. "_type": "_doc",
  229. "_id": "735",
  230. "_score": null,
  231. "_source": {
  232. "account_number": 735,
  233. "balance": 3984,
  234. "firstname": "Loraine",
  235. "lastname": "Willis",
  236. "age": 32,
  237. "gender": "F",
  238. "address": "928 Grove Street",
  239. "employer": "Gadtron",
  240. "email": "lorainewillis@gadtron.com",
  241. "city": "Lowgap",
  242. "state": "NY"
  243. },
  244. "sort": [
  245. ]
  246. },
  247. {
  248. "_index": "bank",
  249. "_type": "_doc",
  250. "_id": "745",
  251. "_score": null,
  252. "_source": {
  253. "account_number": 745,
  254. "balance": 4572,
  255. "firstname": "Jacobs",
  256. "lastname": "Sweeney",
  257. "age": 32,
  258. "gender": "M",
  259. "address": "189 Lott Place",
  260. "employer": "Comtent",
  261. "email": "jacobssweeney@comtent.com",
  262. "city": "Advance",
  263. "state": "NJ"
  264. },
  265. "sort": [
  266. ]
  267. },
  268. {
  269. "_index": "bank",
  270. "_type": "_doc",
  271. "_id": "173",
  272. "_score": null,
  273. "_source": {
  274. "account_number": 173,
  275. "balance": 5989,
  276. "firstname": "Whitley",
  277. "lastname": "Blevins",
  278. "age": 32,
  279. "gender": "M",
  280. "address": "127 Brooklyn Avenue",
  281. "employer": "Pawnagra",
  282. "email": "whitleyblevins@pawnagra.com",
  283. "city": "Rodanthe",
  284. "state": "ND"
  285. },
  286. "sort": [
  287. ]
  288. }
  289. ]
  290. }
  291. }
  292. }
  293. },
  294. {
  295. "_index": "bank",
  296. "_type": "_doc",
  297. "_id": "953",
  298. "_score": null,
  299. "_source": {
  300. "account_number": 953,
  301. "balance": 1110,
  302. "firstname": "Baxter",
  303. "lastname": "Black",
  304. "age": 27,
  305. "gender": "M",
  306. "address": "720 Stillwell Avenue",
  307. "employer": "Uplinx",
  308. "email": "baxterblack@uplinx.com",
  309. "city": "Drummond",
  310. "state": "MN"
  311. },
  312. "fields": {
  313. "age": [
  314. ]
  315. },
  316. "sort": [
  317. ],
  318. "inner_hits": {
  319. "details": {
  320. "hits": {
  321. "total": 39,
  322. "max_score": null,
  323. "hits": [
  324. {
  325. "_index": "bank",
  326. "_type": "_doc",
  327. "_id": "953",
  328. "_score": null,
  329. "_source": {
  330. "account_number": 953,
  331. "balance": 1110,
  332. "firstname": "Baxter",
  333. "lastname": "Black",
  334. "age": 27,
  335. "gender": "M",
  336. "address": "720 Stillwell Avenue",
  337. "employer": "Uplinx",
  338. "email": "baxterblack@uplinx.com",
  339. "city": "Drummond",
  340. "state": "MN"
  341. },
  342. "sort": [
  343. ]
  344. },
  345. {
  346. "_index": "bank",
  347. "_type": "_doc",
  348. "_id": "123",
  349. "_score": null,
  350. "_source": {
  351. "account_number": 123,
  352. "balance": 3079,
  353. "firstname": "Cleo",
  354. "lastname": "Beach",
  355. "age": 27,
  356. "gender": "F",
  357. "address": "653 Haring Street",
  358. "employer": "Proxsoft",
  359. "email": "cleobeach@proxsoft.com",
  360. "city": "Greensburg",
  361. "state": "ME"
  362. },
  363. "sort": [
  364. ]
  365. },
  366. {
  367. "_index": "bank",
  368. "_type": "_doc",
  369. "_id": "637",
  370. "_score": null,
  371. "_source": {
  372. "account_number": 637,
  373. "balance": 3169,
  374. "firstname": "Kathy",
  375. "lastname": "Carter",
  376. "age": 27,
  377. "gender": "F",
  378. "address": "410 Jamison Lane",
  379. "employer": "Limage",
  380. "email": "kathycarter@limage.com",
  381. "city": "Ernstville",
  382. "state": "WA"
  383. },
  384. "sort": [
  385. ]
  386. },
  387. {
  388. "_index": "bank",
  389. "_type": "_doc",
  390. "_id": "528",
  391. "_score": null,
  392. "_source": {
  393. "account_number": 528,
  394. "balance": 4071,
  395. "firstname": "Thompson",
  396. "lastname": "Hoover",
  397. "age": 27,
  398. "gender": "F",
  399. "address": "580 Garden Street",
  400. "employer": "Portalis",
  401. "email": "thompsonhoover@portalis.com",
  402. "city": "Knowlton",
  403. "state": "AL"
  404. },
  405. "sort": [
  406. ]
  407. },
  408. {
  409. "_index": "bank",
  410. "_type": "_doc",
  411. "_id": "142",
  412. "_score": null,
  413. "_source": {
  414. "account_number": 142,
  415. "balance": 4544,
  416. "firstname": "Vang",
  417. "lastname": "Hughes",
  418. "age": 27,
  419. "gender": "M",
  420. "address": "357 Landis Court",
  421. "employer": "Bolax",
  422. "email": "vanghughes@bolax.com",
  423. "city": "Emerald",
  424. "state": "WY"
  425. },
  426. "sort": [
  427. ]
  428. }
  429. ]
  430. }
  431. }
  432. }
  433. },
  434. {
  435. "_index": "bank",
  436. "_type": "_doc",
  437. "_id": "87",
  438. "_score": null,
  439. "_source": {
  440. "account_number": 87,
  441. "balance": 1133,
  442. "firstname": "Hewitt",
  443. "lastname": "Kidd",
  444. "age": 22,
  445. "gender": "M",
  446. "address": "446 Halleck Street",
  447. "employer": "Isologics",
  448. "email": "hewittkidd@isologics.com",
  449. "city": "Coalmont",
  450. "state": "ME"
  451. },
  452. "fields": {
  453. "age": [
  454. ]
  455. },
  456. "sort": [
  457. ],
  458. "inner_hits": {
  459. "details": {
  460. "hits": {
  461. "total": 51,
  462. "max_score": null,
  463. "hits": [
  464. {
  465. "_index": "bank",
  466. "_type": "_doc",
  467. "_id": "87",
  468. "_score": null,
  469. "_source": {
  470. "account_number": 87,
  471. "balance": 1133,
  472. "firstname": "Hewitt",
  473. "lastname": "Kidd",
  474. "age": 22,
  475. "gender": "M",
  476. "address": "446 Halleck Street",
  477. "employer": "Isologics",
  478. "email": "hewittkidd@isologics.com",
  479. "city": "Coalmont",
  480. "state": "ME"
  481. },
  482. "sort": [
  483. ]
  484. },
  485. {
  486. "_index": "bank",
  487. "_type": "_doc",
  488. "_id": "411",
  489. "_score": null,
  490. "_source": {
  491. "account_number": 411,
  492. "balance": 1172,
  493. "firstname": "Guzman",
  494. "lastname": "Whitfield",
  495. "age": 22,
  496. "gender": "M",
  497. "address": "181 Perry Terrace",
  498. "employer": "Springbee",
  499. "email": "guzmanwhitfield@springbee.com",
  500. "city": "Balm",
  501. "state": "IN"
  502. },
  503. "sort": [
  504. ]
  505. },
  506. {
  507. "_index": "bank",
  508. "_type": "_doc",
  509. "_id": "159",
  510. "_score": null,
  511. "_source": {
  512. "account_number": 159,
  513. "balance": 1696,
  514. "firstname": "Alvarez",
  515. "lastname": "Mack",
  516. "age": 22,
  517. "gender": "F",
  518. "address": "897 Manor Court",
  519. "employer": "Snorus",
  520. "email": "alvarezmack@snorus.com",
  521. "city": "Rosedale",
  522. "state": "CA"
  523. },
  524. "sort": [
  525. ]
  526. },
  527. {
  528. "_index": "bank",
  529. "_type": "_doc",
  530. "_id": "220",
  531. "_score": null,
  532. "_source": {
  533. "account_number": 220,
  534. "balance": 3086,
  535. "firstname": "Tania",
  536. "lastname": "Middleton",
  537. "age": 22,
  538. "gender": "F",
  539. "address": "541 Gunther Place",
  540. "employer": "Zerology",
  541. "email": "taniamiddleton@zerology.com",
  542. "city": "Linwood",
  543. "state": "IN"
  544. },
  545. "sort": [
  546. ]
  547. },
  548. {
  549. "_index": "bank",
  550. "_type": "_doc",
  551. "_id": "350",
  552. "_score": null,
  553. "_source": {
  554. "account_number": 350,
  555. "balance": 4267,
  556. "firstname": "Wyatt",
  557. "lastname": "Wise",
  558. "age": 22,
  559. "gender": "F",
  560. "address": "896 Bleecker Street",
  561. "employer": "Rockyard",
  562. "email": "wyattwise@rockyard.com",
  563. "city": "Joes",
  564. "state": "MS"
  565. },
  566. "sort": [
  567. ]
  568. }
  569. ]
  570. }
  571. }
  572. }
  573. },
  574. {
  575. "_index": "bank",
  576. "_type": "_doc",
  577. "_id": "749",
  578. "_score": null,
  579. "_source": {
  580. "account_number": 749,
  581. "balance": 1249,
  582. "firstname": "Rush",
  583. "lastname": "Boyle",
  584. "age": 36,
  585. "gender": "M",
  586. "address": "310 Argyle Road",
  587. "employer": "Sportan",
  588. "email": "rushboyle@sportan.com",
  589. "city": "Brady",
  590. "state": "WA"
  591. },
  592. "fields": {
  593. "age": [
  594. ]
  595. },
  596. "sort": [
  597. ],
  598. "inner_hits": {
  599. "details": {
  600. "hits": {
  601. "total": 52,
  602. "max_score": null,
  603. "hits": [
  604. {
  605. "_index": "bank",
  606. "_type": "_doc",
  607. "_id": "749",
  608. "_score": null,
  609. "_source": {
  610. "account_number": 749,
  611. "balance": 1249,
  612. "firstname": "Rush",
  613. "lastname": "Boyle",
  614. "age": 36,
  615. "gender": "M",
  616. "address": "310 Argyle Road",
  617. "employer": "Sportan",
  618. "email": "rushboyle@sportan.com",
  619. "city": "Brady",
  620. "state": "WA"
  621. },
  622. "sort": [
  623. ]
  624. },
  625. {
  626. "_index": "bank",
  627. "_type": "_doc",
  628. "_id": "427",
  629. "_score": null,
  630. "_source": {
  631. "account_number": 427,
  632. "balance": 1463,
  633. "firstname": "Rebekah",
  634. "lastname": "Garrison",
  635. "age": 36,
  636. "gender": "F",
  637. "address": "837 Hampton Avenue",
  638. "employer": "Niquent",
  639. "email": "rebekahgarrison@niquent.com",
  640. "city": "Zarephath",
  641. "state": "NY"
  642. },
  643. "sort": [
  644. ]
  645. },
  646. {
  647. "_index": "bank",
  648. "_type": "_doc",
  649. "_id": "782",
  650. "_score": null,
  651. "_source": {
  652. "account_number": 782,
  653. "balance": 3960,
  654. "firstname": "Maldonado",
  655. "lastname": "Craig",
  656. "age": 36,
  657. "gender": "F",
  658. "address": "345 Myrtle Avenue",
  659. "employer": "Zilencio",
  660. "email": "maldonadocraig@zilencio.com",
  661. "city": "Yukon",
  662. "state": "ID"
  663. },
  664. "sort": [
  665. ]
  666. },
  667. {
  668. "_index": "bank",
  669. "_type": "_doc",
  670. "_id": "6",
  671. "_score": null,
  672. "_source": {
  673. "account_number": 6,
  674. "balance": 5686,
  675. "firstname": "Hattie",
  676. "lastname": "Bond",
  677. "age": 36,
  678. "gender": "M",
  679. "address": "671 Bristol Street",
  680. "employer": "Netagy",
  681. "email": "hattiebond@netagy.com",
  682. "city": "Dante",
  683. "state": "TN"
  684. },
  685. "sort": [
  686. ]
  687. },
  688. {
  689. "_index": "bank",
  690. "_type": "_doc",
  691. "_id": "170",
  692. "_score": null,
  693. "_source": {
  694. "account_number": 170,
  695. "balance": 6025,
  696. "firstname": "Mann",
  697. "lastname": "Madden",
  698. "age": 36,
  699. "gender": "F",
  700. "address": "161 Radde Place",
  701. "employer": "Farmex",
  702. "email": "mannmadden@farmex.com",
  703. "city": "Thermal",
  704. "state": "LA"
  705. },
  706. "sort": [
  707. ]
  708. }
  709. ]
  710. }
  711. }
  712. }
  713. },
  714. {
  715. "_index": "bank",
  716. "_type": "_doc",
  717. "_id": "315",
  718. "_score": null,
  719. "_source": {
  720. "account_number": 315,
  721. "balance": 1314,
  722. "firstname": "Clare",
  723. "lastname": "Morrow",
  724. "age": 33,
  725. "gender": "F",
  726. "address": "728 Madeline Court",
  727. "employer": "Gaptec",
  728. "email": "claremorrow@gaptec.com",
  729. "city": "Mapletown",
  730. "state": "PA"
  731. },
  732. "fields": {
  733. "age": [
  734. ]
  735. },
  736. "sort": [
  737. ],
  738. "inner_hits": {
  739. "details": {
  740. "hits": {
  741. "total": 50,
  742. "max_score": null,
  743. "hits": [
  744. {
  745. "_index": "bank",
  746. "_type": "_doc",
  747. "_id": "315",
  748. "_score": null,
  749. "_source": {
  750. "account_number": 315,
  751. "balance": 1314,
  752. "firstname": "Clare",
  753. "lastname": "Morrow",
  754. "age": 33,
  755. "gender": "F",
  756. "address": "728 Madeline Court",
  757. "employer": "Gaptec",
  758. "email": "claremorrow@gaptec.com",
  759. "city": "Mapletown",
  760. "state": "PA"
  761. },
  762. "sort": [
  763. ]
  764. },
  765. {
  766. "_index": "bank",
  767. "_type": "_doc",
  768. "_id": "118",
  769. "_score": null,
  770. "_source": {
  771. "account_number": 118,
  772. "balance": 2223,
  773. "firstname": "Ballard",
  774. "lastname": "Vasquez",
  775. "age": 33,
  776. "gender": "F",
  777. "address": "101 Bush Street",
  778. "employer": "Intergeek",
  779. "email": "ballardvasquez@intergeek.com",
  780. "city": "Century",
  781. "state": "MN"
  782. },
  783. "sort": [
  784. ]
  785. },
  786. {
  787. "_index": "bank",
  788. "_type": "_doc",
  789. "_id": "786",
  790. "_score": null,
  791. "_source": {
  792. "account_number": 786,
  793. "balance": 3024,
  794. "firstname": "Rene",
  795. "lastname": "Vang",
  796. "age": 33,
  797. "gender": "M",
  798. "address": "506 Randolph Street",
  799. "employer": "Isopop",
  800. "email": "renevang@isopop.com",
  801. "city": "Vienna",
  802. "state": "NJ"
  803. },
  804. "sort": [
  805. ]
  806. },
  807. {
  808. "_index": "bank",
  809. "_type": "_doc",
  810. "_id": "932",
  811. "_score": null,
  812. "_source": {
  813. "account_number": 932,
  814. "balance": 3111,
  815. "firstname": "Summer",
  816. "lastname": "Porter",
  817. "age": 33,
  818. "gender": "F",
  819. "address": "949 Grand Avenue",
  820. "employer": "Multiflex",
  821. "email": "summerporter@multiflex.com",
  822. "city": "Spokane",
  823. "state": "OK"
  824. },
  825. "sort": [
  826. ]
  827. },
  828. {
  829. "_index": "bank",
  830. "_type": "_doc",
  831. "_id": "587",
  832. "_score": null,
  833. "_source": {
  834. "account_number": 587,
  835. "balance": 3468,
  836. "firstname": "Carly",
  837. "lastname": "Johns",
  838. "age": 33,
  839. "gender": "M",
  840. "address": "390 Noll Street",
  841. "employer": "Gallaxia",
  842. "email": "carlyjohns@gallaxia.com",
  843. "city": "Emison",
  844. "state": "DC"
  845. },
  846. "sort": [
  847. ]
  848. }
  849. ]
  850. }
  851. }
  852. }
  853. },
  854. {
  855. "_index": "bank",
  856. "_type": "_doc",
  857. "_id": "348",
  858. "_score": null,
  859. "_source": {
  860. "account_number": 348,
  861. "balance": 1360,
  862. "firstname": "Karina",
  863. "lastname": "Russell",
  864. "age": 37,
  865. "gender": "M",
  866. "address": "797 Moffat Street",
  867. "employer": "Limozen",
  868. "email": "karinarussell@limozen.com",
  869. "city": "Riegelwood",
  870. "state": "RI"
  871. },
  872. "fields": {
  873. "age": [
  874. ]
  875. },
  876. "sort": [
  877. ],
  878. "inner_hits": {
  879. "details": {
  880. "hits": {
  881. "total": 42,
  882. "max_score": null,
  883. "hits": [
  884. {
  885. "_index": "bank",
  886. "_type": "_doc",
  887. "_id": "348",
  888. "_score": null,
  889. "_source": {
  890. "account_number": 348,
  891. "balance": 1360,
  892. "firstname": "Karina",
  893. "lastname": "Russell",
  894. "age": 37,
  895. "gender": "M",
  896. "address": "797 Moffat Street",
  897. "employer": "Limozen",
  898. "email": "karinarussell@limozen.com",
  899. "city": "Riegelwood",
  900. "state": "RI"
  901. },
  902. "sort": [
  903. ]
  904. },
  905. {
  906. "_index": "bank",
  907. "_type": "_doc",
  908. "_id": "663",
  909. "_score": null,
  910. "_source": {
  911. "account_number": 663,
  912. "balance": 2456,
  913. "firstname": "Rollins",
  914. "lastname": "Richards",
  915. "age": 37,
  916. "gender": "M",
  917. "address": "129 Sullivan Place",
  918. "employer": "Geostele",
  919. "email": "rollinsrichards@geostele.com",
  920. "city": "Morgandale",
  921. "state": "FL"
  922. },
  923. "sort": [
  924. ]
  925. },
  926. {
  927. "_index": "bank",
  928. "_type": "_doc",
  929. "_id": "699",
  930. "_score": null,
  931. "_source": {
  932. "account_number": 699,
  933. "balance": 4156,
  934. "firstname": "Gallagher",
  935. "lastname": "Marshall",
  936. "age": 37,
  937. "gender": "F",
  938. "address": "648 Clifford Place",
  939. "employer": "Exiand",
  940. "email": "gallaghermarshall@exiand.com",
  941. "city": "Belfair",
  942. "state": "KY"
  943. },
  944. "sort": [
  945. ]
  946. },
  947. {
  948. "_index": "bank",
  949. "_type": "_doc",
  950. "_id": "161",
  951. "_score": null,
  952. "_source": {
  953. "account_number": 161,
  954. "balance": 4659,
  955. "firstname": "Doreen",
  956. "lastname": "Randall",
  957. "age": 37,
  958. "gender": "F",
  959. "address": "178 Court Street",
  960. "employer": "Calcula",
  961. "email": "doreenrandall@calcula.com",
  962. "city": "Belmont",
  963. "state": "TX"
  964. },
  965. "sort": [
  966. ]
  967. },
  968. {
  969. "_index": "bank",
  970. "_type": "_doc",
  971. "_id": "258",
  972. "_score": null,
  973. "_source": {
  974. "account_number": 258,
  975. "balance": 5712,
  976. "firstname": "Lindsey",
  977. "lastname": "Hawkins",
  978. "age": 37,
  979. "gender": "M",
  980. "address": "706 Frost Street",
  981. "employer": "Enormo",
  982. "email": "lindseyhawkins@enormo.com",
  983. "city": "Gardners",
  984. "state": "AK"
  985. },
  986. "sort": [
  987. ]
  988. }
  989. ]
  990. }
  991. }
  992. }
  993. },
  994. {
  995. "_index": "bank",
  996. "_type": "_doc",
  997. "_id": "490",
  998. "_score": null,
  999. "_source": {
  1000. "account_number": 490,
  1001. "balance": 1447,
  1002. "firstname": "Strong",
  1003. "lastname": "Hendrix",
  1004. "age": 26,
  1005. "gender": "F",
  1006. "address": "134 Beach Place",
  1007. "employer": "Duoflex",
  1008. "email": "stronghendrix@duoflex.com",
  1009. "city": "Allentown",
  1010. "state": "ND"
  1011. },
  1012. "fields": {
  1013. "age": [
  1014. ]
  1015. },
  1016. "sort": [
  1017. ],
  1018. "inner_hits": {
  1019. "details": {
  1020. "hits": {
  1021. "total": 59,
  1022. "max_score": null,
  1023. "hits": [
  1024. {
  1025. "_index": "bank",
  1026. "_type": "_doc",
  1027. "_id": "490",
  1028. "_score": null,
  1029. "_source": {
  1030. "account_number": 490,
  1031. "balance": 1447,
  1032. "firstname": "Strong",
  1033. "lastname": "Hendrix",
  1034. "age": 26,
  1035. "gender": "F",
  1036. "address": "134 Beach Place",
  1037. "employer": "Duoflex",
  1038. "email": "stronghendrix@duoflex.com",
  1039. "city": "Allentown",
  1040. "state": "ND"
  1041. },
  1042. "sort": [
  1043. ]
  1044. },
  1045. {
  1046. "_index": "bank",
  1047. "_type": "_doc",
  1048. "_id": "280",
  1049. "_score": null,
  1050. "_source": {
  1051. "account_number": 280,
  1052. "balance": 3380,
  1053. "firstname": "Vilma",
  1054. "lastname": "Shields",
  1055. "age": 26,
  1056. "gender": "F",
  1057. "address": "133 Berriman Street",
  1058. "employer": "Applidec",
  1059. "email": "vilmashields@applidec.com",
  1060. "city": "Adamstown",
  1061. "state": "ME"
  1062. },
  1063. "sort": [
  1064. ]
  1065. },
  1066. {
  1067. "_index": "bank",
  1068. "_type": "_doc",
  1069. "_id": "596",
  1070. "_score": null,
  1071. "_source": {
  1072. "account_number": 596,
  1073. "balance": 4063,
  1074. "firstname": "Letitia",
  1075. "lastname": "Walker",
  1076. "age": 26,
  1077. "gender": "F",
  1078. "address": "963 Vanderveer Place",
  1079. "employer": "Zizzle",
  1080. "email": "letitiawalker@zizzle.com",
  1081. "city": "Rossmore",
  1082. "state": "ID"
  1083. },
  1084. "sort": [
  1085. ]
  1086. },
  1087. {
  1088. "_index": "bank",
  1089. "_type": "_doc",
  1090. "_id": "780",
  1091. "_score": null,
  1092. "_source": {
  1093. "account_number": 780,
  1094. "balance": 4682,
  1095. "firstname": "Maryanne",
  1096. "lastname": "Hendricks",
  1097. "age": 26,
  1098. "gender": "F",
  1099. "address": "709 Wolcott Street",
  1100. "employer": "Sarasonic",
  1101. "email": "maryannehendricks@sarasonic.com",
  1102. "city": "Santel",
  1103. "state": "NH"
  1104. },
  1105. "sort": [
  1106. ]
  1107. },
  1108. {
  1109. "_index": "bank",
  1110. "_type": "_doc",
  1111. "_id": "405",
  1112. "_score": null,
  1113. "_source": {
  1114. "account_number": 405,
  1115. "balance": 5679,
  1116. "firstname": "Strickland",
  1117. "lastname": "Fuller",
  1118. "age": 26,
  1119. "gender": "M",
  1120. "address": "990 Concord Street",
  1121. "employer": "Digique",
  1122. "email": "stricklandfuller@digique.com",
  1123. "city": "Southmont",
  1124. "state": "NV"
  1125. },
  1126. "sort": [
  1127. ]
  1128. }
  1129. ]
  1130. }
  1131. }
  1132. }
  1133. },
  1134. {
  1135. "_index": "bank",
  1136. "_type": "_doc",
  1137. "_id": "174",
  1138. "_score": null,
  1139. "_source": {
  1140. "account_number": 174,
  1141. "balance": 1464,
  1142. "firstname": "Gamble",
  1143. "lastname": "Pierce",
  1144. "age": 23,
  1145. "gender": "F",
  1146. "address": "650 Eagle Street",
  1147. "employer": "Matrixity",
  1148. "email": "gamblepierce@matrixity.com",
  1149. "city": "Abiquiu",
  1150. "state": "OR"
  1151. },
  1152. "fields": {
  1153. "age": [
  1154. ]
  1155. },
  1156. "sort": [
  1157. ],
  1158. "inner_hits": {
  1159. "details": {
  1160. "hits": {
  1161. "total": 42,
  1162. "max_score": null,
  1163. "hits": [
  1164. {
  1165. "_index": "bank",
  1166. "_type": "_doc",
  1167. "_id": "174",
  1168. "_score": null,
  1169. "_source": {
  1170. "account_number": 174,
  1171. "balance": 1464,
  1172. "firstname": "Gamble",
  1173. "lastname": "Pierce",
  1174. "age": 23,
  1175. "gender": "F",
  1176. "address": "650 Eagle Street",
  1177. "employer": "Matrixity",
  1178. "email": "gamblepierce@matrixity.com",
  1179. "city": "Abiquiu",
  1180. "state": "OR"
  1181. },
  1182. "sort": [
  1183. ]
  1184. },
  1185. {
  1186. "_index": "bank",
  1187. "_type": "_doc",
  1188. "_id": "110",
  1189. "_score": null,
  1190. "_source": {
  1191. "account_number": 110,
  1192. "balance": 4850,
  1193. "firstname": "Daphne",
  1194. "lastname": "Byrd",
  1195. "age": 23,
  1196. "gender": "F",
  1197. "address": "239 Conover Street",
  1198. "employer": "Freakin",
  1199. "email": "daphnebyrd@freakin.com",
  1200. "city": "Taft",
  1201. "state": "MN"
  1202. },
  1203. "sort": [
  1204. ]
  1205. },
  1206. {
  1207. "_index": "bank",
  1208. "_type": "_doc",
  1209. "_id": "900",
  1210. "_score": null,
  1211. "_source": {
  1212. "account_number": 900,
  1213. "balance": 6124,
  1214. "firstname": "Gonzalez",
  1215. "lastname": "Watson",
  1216. "age": 23,
  1217. "gender": "M",
  1218. "address": "624 Sullivan Street",
  1219. "employer": "Marvane",
  1220. "email": "gonzalezwatson@marvane.com",
  1221. "city": "Wikieup",
  1222. "state": "IL"
  1223. },
  1224. "sort": [
  1225. ]
  1226. },
  1227. {
  1228. "_index": "bank",
  1229. "_type": "_doc",
  1230. "_id": "443",
  1231. "_score": null,
  1232. "_source": {
  1233. "account_number": 443,
  1234. "balance": 7588,
  1235. "firstname": "Huff",
  1236. "lastname": "Thomas",
  1237. "age": 23,
  1238. "gender": "M",
  1239. "address": "538 Erskine Loop",
  1240. "employer": "Accufarm",
  1241. "email": "huffthomas@accufarm.com",
  1242. "city": "Corinne",
  1243. "state": "AL"
  1244. },
  1245. "sort": [
  1246. ]
  1247. },
  1248. {
  1249. "_index": "bank",
  1250. "_type": "_doc",
  1251. "_id": "643",
  1252. "_score": null,
  1253. "_source": {
  1254. "account_number": 643,
  1255. "balance": 8057,
  1256. "firstname": "Hendricks",
  1257. "lastname": "Stokes",
  1258. "age": 23,
  1259. "gender": "F",
  1260. "address": "142 Barbey Street",
  1261. "employer": "Remotion",
  1262. "email": "hendricksstokes@remotion.com",
  1263. "city": "Lewis",
  1264. "state": "MA"
  1265. },
  1266. "sort": [
  1267. ]
  1268. }
  1269. ]
  1270. }
  1271. }
  1272. }
  1273. },
  1274. {
  1275. "_index": "bank",
  1276. "_type": "_doc",
  1277. "_id": "111",
  1278. "_score": null,
  1279. "_source": {
  1280. "account_number": 111,
  1281. "balance": 1481,
  1282. "firstname": "Traci",
  1283. "lastname": "Allison",
  1284. "age": 35,
  1285. "gender": "M",
  1286. "address": "922 Bryant Street",
  1287. "employer": "Enjola",
  1288. "email": "traciallison@enjola.com",
  1289. "city": "Robinette",
  1290. "state": "OR"
  1291. },
  1292. "fields": {
  1293. "age": [
  1294. ]
  1295. },
  1296. "sort": [
  1297. ],
  1298. "inner_hits": {
  1299. "details": {
  1300. "hits": {
  1301. "total": 52,
  1302. "max_score": null,
  1303. "hits": [
  1304. {
  1305. "_index": "bank",
  1306. "_type": "_doc",
  1307. "_id": "111",
  1308. "_score": null,
  1309. "_source": {
  1310. "account_number": 111,
  1311. "balance": 1481,
  1312. "firstname": "Traci",
  1313. "lastname": "Allison",
  1314. "age": 35,
  1315. "gender": "M",
  1316. "address": "922 Bryant Street",
  1317. "employer": "Enjola",
  1318. "email": "traciallison@enjola.com",
  1319. "city": "Robinette",
  1320. "state": "OR"
  1321. },
  1322. "sort": [
  1323. ]
  1324. },
  1325. {
  1326. "_index": "bank",
  1327. "_type": "_doc",
  1328. "_id": "417",
  1329. "_score": null,
  1330. "_source": {
  1331. "account_number": 417,
  1332. "balance": 1788,
  1333. "firstname": "Wheeler",
  1334. "lastname": "Ayers",
  1335. "age": 35,
  1336. "gender": "F",
  1337. "address": "677 Hope Street",
  1338. "employer": "Fortean",
  1339. "email": "wheelerayers@fortean.com",
  1340. "city": "Ironton",
  1341. "state": "PA"
  1342. },
  1343. "sort": [
  1344. ]
  1345. },
  1346. {
  1347. "_index": "bank",
  1348. "_type": "_doc",
  1349. "_id": "984",
  1350. "_score": null,
  1351. "_source": {
  1352. "account_number": 984,
  1353. "balance": 1904,
  1354. "firstname": "Viola",
  1355. "lastname": "Crawford",
  1356. "age": 35,
  1357. "gender": "F",
  1358. "address": "354 Linwood Street",
  1359. "employer": "Ginkle",
  1360. "email": "violacrawford@ginkle.com",
  1361. "city": "Witmer",
  1362. "state": "AR"
  1363. },
  1364. "sort": [
  1365. ]
  1366. },
  1367. {
  1368. "_index": "bank",
  1369. "_type": "_doc",
  1370. "_id": "527",
  1371. "_score": null,
  1372. "_source": {
  1373. "account_number": 527,
  1374. "balance": 2028,
  1375. "firstname": "Carver",
  1376. "lastname": "Peters",
  1377. "age": 35,
  1378. "gender": "M",
  1379. "address": "816 Victor Road",
  1380. "employer": "Housedown",
  1381. "email": "carverpeters@housedown.com",
  1382. "city": "Nadine",
  1383. "state": "MD"
  1384. },
  1385. "sort": [
  1386. ]
  1387. },
  1388. {
  1389. "_index": "bank",
  1390. "_type": "_doc",
  1391. "_id": "266",
  1392. "_score": null,
  1393. "_source": {
  1394. "account_number": 266,
  1395. "balance": 2777,
  1396. "firstname": "Monique",
  1397. "lastname": "Conner",
  1398. "age": 35,
  1399. "gender": "F",
  1400. "address": "489 Metrotech Courtr",
  1401. "employer": "Flotonic",
  1402. "email": "moniqueconner@flotonic.com",
  1403. "city": "Retsof",
  1404. "state": "MD"
  1405. },
  1406. "sort": [
  1407. ]
  1408. }
  1409. ]
  1410. }
  1411. }
  1412. }
  1413. }
  1414. ]
  1415. }
  1416. }

在inner_hits 中返回多个角度的组内topN

  1. GET /twitter/_search
  2. {
  3. "query": {
  4. "match": {
  5. "message": "elasticsearch"
  6. }
  7. },
  8. "collapse" : {
  9. "field" : "user",
  10. "inner_hits": [
  11. {
  12. "name": "most_liked",
  13. "size": 3,
  14. "sort": ["likes"]
  15. },
  16. {
  17. "name": "most_recent",
  18. "size": 3,
  19. "sort": [{ "date": "asc" }]
  20. }
  21. ]
  22. },
  23. "sort": ["likes"]
  24. }

 说明:

most_liked:最像

most_recent:最近一段时间的

 6.3.1 分页

 from and size

  1. GET /_search
  2. {
  3. "from" : 0, "size" : 10,
  4. "query" : {
  5. "term" : { "user" : "kimchy" }
  6. }
  7. }

注意:搜索请求耗用的堆内存和时间与 from + size 大小成正比。分页越深耗用越大,为了不因分页导致OOM或严重影响性能,ES中规定from + size 不能大于索引setting参数 index.max_result_window 的值,默认值为 10,000。

需要深度分页, 不受index.max_result_window 限制,怎么办? 

Search after  在指定文档后取文档, 可用于深度分页

 首次查询第一页

  1. GET twitter/_search
  2. {
  3. "size": 10,
  4. "query": {
  5. "match" : {
  6. "title" : "elasticsearch"
  7. }
  8. },
  9. "sort": [
  10. {"date": "asc"},
  11. {"_id": "desc"}
  12. ]
  13. }

后续页的查询

  1. GET twitter/_search
  2. {
  3. "size": 10,
  4. "query": {
  5. "match" : {
  6. "title" : "elasticsearch"
  7. }
  8. },
  9. "search_after": [1463538857, "654323"],
  10. "sort": [
  11. {"date": "asc"},
  12. {"_id": "desc"}
  13. ]
  14. }

注意:使用search_after,要求查询必须指定排序,并且这个排序组合值每个文档唯一(最好排序中包含_id字段)。 search_after的值用的就是这个排序值。 用search_after时 from 只能为0、-1。

6.3.2 高亮

准备数据:

  1. PUT /hl_test/_doc/1
  2. {
  3. "title": "lucene solr and elasticsearch",
  4. "content": "lucene solr and elasticsearch for search"
  5. }

查询高亮数据

  1. GET /hl_test/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": "lucene"
  6. }
  7. },
  8. "highlight": {
  9. "fields": {
  10. "title": {},
  11. "content": {}
  12. }
  13. }
  14. }

查询结果:

  1. {
  2. "took": 113,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1,
  12. "max_score": 0.2876821,
  13. "hits": [
  14. {
  15. "_index": "hl_test",
  16. "_type": "_doc",
  17. "_id": "1",
  18. "_score": 0.2876821,
  19. "_source": {
  20. "title": "lucene solr and elasticsearch",
  21. "content": "lucene solr and elasticsearch for search"
  22. },
  23. "highlight": {
  24. "title": [
  25. "<em>lucene</em> solr and elasticsearch"
  26. ]
  27. }
  28. }
  29. ]
  30. }
  31. }

多字段高亮

  1. GET /hl_test/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": "lucene"
  6. }
  7. },
  8. "highlight": {
  9. "require_field_match": false,
  10. "fields": {
  11. "title": {},
  12. "content": {}
  13. }
  14. }
  15. }

查询结果:

  1. {
  2. "took": 5,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1,
  12. "max_score": 0.2876821,
  13. "hits": [
  14. {
  15. "_index": "hl_test",
  16. "_type": "_doc",
  17. "_id": "1",
  18. "_score": 0.2876821,
  19. "_source": {
  20. "title": "lucene solr and elasticsearch",
  21. "content": "lucene solr and elasticsearch for search"
  22. },
  23. "highlight": {
  24. "title": [
  25. "<em>lucene</em> solr and elasticsearch"
  26. ],
  27. "content": [
  28. "<em>lucene</em> solr and elasticsearch for search"
  29. ]
  30. }
  31. }
  32. ]
  33. }
  34. }

说明:

高亮结果在返回的每个文档中以hightlight节点给出

指定高亮标签

  1. GET /hl_test/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": "lucene"
  6. }
  7. },
  8. "highlight": {
  9. "require_field_match": false,
  10. "fields": {
  11. "title": {
  12. "pre_tags":["<strong>"],
  13. "post_tags": ["</strong>"]
  14. },
  15. "content": {}
  16. }
  17. }
  18. }

查询结果:

  1. {
  2. "took": 5,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1,
  12. "max_score": 0.2876821,
  13. "hits": [
  14. {
  15. "_index": "hl_test",
  16. "_type": "_doc",
  17. "_id": "1",
  18. "_score": 0.2876821,
  19. "_source": {
  20. "title": "lucene solr and elasticsearch",
  21. "content": "lucene solr and elasticsearch for search"
  22. },
  23. "highlight": {
  24. "title": [
  25. "<strong>lucene</strong> solr and elasticsearch"
  26. ],
  27. "content": [
  28. "<em>lucene</em> solr and elasticsearch for search"
  29. ]
  30. }
  31. }
  32. ]
  33. }
  34. }

高亮的详细设置请参考官网:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html

6.3.3 Profile  为了调试、优化

对于执行缓慢的查询,我们很想知道它为什么慢,时间都耗在哪了,可以在查询上加入上 profile 来获得详细的执行步骤、耗时信息。

  1. GET /twitter/_search
  2. {
  3. "profile": true,
  4. "query" : {
  5. "match" : { "message" : "some number" }
  6. }
  7. }

信息的说明请参考:

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

7.  count api 查询数量

  1. PUT /twitter/_doc/1?refresh
  2. {
  3. "user": "kimchy"
  4. }
  5. GET /twitter/_doc/_count?q=user:kimchy
  6. GET /twitter/_doc/_count
  7. {
  8. "query" : {
  9. "term" : { "user" : "kimchy" }
  10. }
  11. }

结果说明:

  1. {
  2. "count" : 1,
  3. "_shards" : {
  4. "total" : 5,
  5. "successful" : 5,
  6. "skipped" : 0,
  7. "failed" : 0
  8. }
  9. }

8. validate api  

用来检查我们的查询是否正确,以及查看底层生成查询是怎样的

GET twitter/_validate/query?q=user:foo

8.1 校验查询

  1. GET twitter/_doc/_validate/query
  2. {
  3. "query": {
  4. "query_string": {
  5. "query": "post_date:foo",
  6. "lenient": false
  7. }
  8. }
  9. }

查询结果:

  1. {
  2. "valid": true,
  3. "_shards": {
  4. "total": 1,
  5. "successful": 1,
  6. "failed": 0
  7. }
  8. }

8.2 获得查询解释

  1. GET twitter/_doc/_validate/query?explain=true
  2. {
  3. "query": {
  4. "query_string": {
  5. "query": "post_date:foo",
  6. "lenient": false
  7. }
  8. }
  9. }

查询结果

  1. {
  2. "valid": true,
  3. "_shards": {
  4. "total": 1,
  5. "successful": 1,
  6. "failed": 0
  7. },
  8. "explanations": [
  9. {
  10. "index": "twitter",
  11. "valid": true,
  12. "explanation": """+MatchNoDocsQuery("unmapped field [post_date]") #MatchNoDocsQuery("Type list does not contain the index type")"""
  13. }
  14. ]
  15. }

8.3 用rewrite获得比explain 更详细的解释

  1. GET twitter/_doc/_validate/query?rewrite=true
  2. {
  3. "query": {
  4. "more_like_this": {
  5. "like": {
  6. "_id": "2"
  7. },
  8. "boost_terms": 1
  9. }
  10. }
  11. }

查询结果:

  1. {
  2. "valid": true,
  3. "_shards": {
  4. "total": 1,
  5. "successful": 1,
  6. "failed": 0
  7. },
  8. "explanations": [
  9. {
  10. "index": "twitter",
  11. "valid": true,
  12. "explanation": """+(MatchNoDocsQuery("empty BooleanQuery") -ConstantScore(MatchNoDocsQuery("empty BooleanQuery"))) #MatchNoDocsQuery("Type list does not contain the index type")"""
  13. }
  14. ]
  15. }

8.4 获得所有分片上的查询解释

  1. GET twitter/_doc/_validate/query?rewrite=true&all_shards=true
  2. {
  3. "query": {
  4. "match": {
  5. "user": {
  6. "query": "kimchy",
  7. "fuzziness": "auto"
  8. }
  9. }
  10. }
  11. }

查询结果:

  1. {
  2. "valid": true,
  3. "_shards": {
  4. "total": 3,
  5. "successful": 3,
  6. "failed": 0
  7. },
  8. "explanations": [
  9. {
  10. "index": "twitter",
  11. "shard": 0,
  12. "valid": true,
  13. "explanation": """MatchNoDocsQuery("unmapped field [user]")"""
  14. },
  15. {
  16. "index": "twitter",
  17. "shard": 1,
  18. "valid": true,
  19. "explanation": """MatchNoDocsQuery("unmapped field [user]")"""
  20. },
  21. {
  22. "index": "twitter",
  23. "shard": 2,
  24. "valid": true,
  25. "explanation": """MatchNoDocsQuery("unmapped field [user]")"""
  26. }
  27. ]
  28. }

官网链接:

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

9. Explain api  

获得某个查询的评分解释,及某个文档是否被这个查询命中

  1. GET /twitter/_doc/0/_explain
  2. {
  3. "query" : {
  4. "match" : { "message" : "elasticsearch" }
  5. }
  6. }

官网链接:

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

10. Search Shards API

让我们可以了解可执行查询的索引分片节点情况

GET /twitter/_search_shards

查询结果:

  1. {
  2. "nodes": {
  3. "qkmtovyLRPWjXcfDTryNwA": {
  4. "name": "qkmtovy",
  5. "ephemeral_id": "sxgsvzsORraAnN7PIlMYpg",
  6. "transport_address": "127.0.0.1:9300",
  7. "attributes": {}
  8. }
  9. },
  10. "indices": {
  11. "twitter": {}
  12. },
  13. "shards": [
  14. [
  15. {
  16. "state": "STARTED",
  17. "primary": true,
  18. "node": "qkmtovyLRPWjXcfDTryNwA",
  19. "relocating_node": null,
  20. "shard": 0,
  21. "index": "twitter",
  22. "allocation_id": {
  23. "id": "3Yf6lOjyQja_v4yP_gL8qA"
  24. }
  25. }
  26. ],
  27. [
  28. {
  29. "state": "STARTED",
  30. "primary": true,
  31. "node": "qkmtovyLRPWjXcfDTryNwA",
  32. "relocating_node": null,
  33. "shard": 1,
  34. "index": "twitter",
  35. "allocation_id": {
  36. "id": "8S88pnUkSSy8kiCcwBgb9Q"
  37. }
  38. }
  39. ],
  40. [
  41. {
  42. "state": "STARTED",
  43. "primary": true,
  44. "node": "qkmtovyLRPWjXcfDTryNwA",
  45. "relocating_node": null,
  46. "shard": 2,
  47. "index": "twitter",
  48. "allocation_id": {
  49. "id": "_uIup55LQZKaltUfuh5aFA"
  50. }
  51. }
  52. ]
  53. ]
  54. }

想知道指定routing值的查询将在哪些分片节点上执行

GET /twitter/_search_shards?routing=foo,baz

查询结果:

  1. {
  2. "nodes": {
  3. "qkmtovyLRPWjXcfDTryNwA": {
  4. "name": "qkmtovy",
  5. "ephemeral_id": "sxgsvzsORraAnN7PIlMYpg",
  6. "transport_address": "127.0.0.1:9300",
  7. "attributes": {}
  8. }
  9. },
  10. "indices": {
  11. "twitter": {}
  12. },
  13. "shards": [
  14. [
  15. {
  16. "state": "STARTED",
  17. "primary": true,
  18. "node": "qkmtovyLRPWjXcfDTryNwA",
  19. "relocating_node": null,
  20. "shard": 1,
  21. "index": "twitter",
  22. "allocation_id": {
  23. "id": "8S88pnUkSSy8kiCcwBgb9Q"
  24. }
  25. }
  26. ]
  27. ]
  28. }

11. Search Template 查询模板

注册一个模板

  1. POST _scripts/<templatename>
  2. {
  3. "script": {
  4. "lang": "mustache",
  5. "source": {
  6. "query": {
  7. "match": {
  8. "title": "{{query_string}}"
  9. }
  10. }
  11. }
  12. }
  13. }

使用模板进行查询

  1. GET _search/template
  2. {
  3. "id": "<templateName>",
  4. "params": {
  5. "query_string": "search for these words"
  6. }
  7. }

查询结果:

  1. {
  2. "took": 11,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 38,
  6. "successful": 38,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 0,
  12. "max_score": null,
  13. "hits": []
  14. }
  15. }

详细了解请参考官网:

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

二、Query DSL

 

官网介绍链接:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html

 Query DSL 介绍

 1. DSL是什么?

Domain Specific Language:领域特定语言

Elasticsearch基于JSON提供完整的查询DSL来定义查询。

一个查询可由两部分字句构成:

Leaf query clauses 叶子查询字句
Leaf query clauses 在指定的字段上查询指定的值, 如:match, term or range queries. 叶子字句可以单独使用. 
Compound query clauses 复合查询字句
以逻辑方式组合多个叶子、复合查询为一个查询

 2. Query and filter context

 一个查询字句的行为取决于它是用在query context  还是 filter context 中 。

Query context 查询上下文
用在查询上下文中的字句回答“这个文档有多匹配这个查询?”。除了决定文档是否匹配,字句匹配的文档还会计算一个字句评分,来评定文档有多匹配。查询上下文由 query 元素表示。
Filter context 过滤上下文
过滤上下文由 filter 元素或 bool 中的 must not 表示。用在过滤上下文中的字句回答“这个文档是否匹配这个查询?”,不参与相关性评分
被频繁使用的过滤器将被ES自动缓存,来提高查询性能。

 示例:

  1. GET /_search
  2. {
  3. <!--查询 -->
  4. "query": {
  5. "bool": {
  6. "must": [
  7. { "match": { "title": "Search" }},
  8. { "match": { "content": "Elasticsearch" }}
  9. ],
  10. <!--过滤 -->
  11. "filter": [
  12. { "term": { "status": "published" }},
  13. { "range": { "publish_date": { "gte": "2015-01-01" }}}
  14. ]
  15. }
  16. }
  17. }

 说明:查询和过滤都是对所有文档进行查询,最后两个结果取交集

 提示:在查询上下文中使用查询子句来表示影响匹配文档得分的条件,并在过滤上下文中使用所有其他查询子句。

 查询分类介绍

 

1. Match all query 查询所有

  1. GET /_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }

 相反,什么都不查

  1. GET /_search
  2. {
  3. "query": {
  4. "match_none": {}
  5. }
  6. }

 2. Full text querys

全文查询,用于对分词的字段进行搜索。会用查询字段的分词器对查询的文本进行分词生成查询。可用于短语查询、模糊查询、前缀查询、临近查询等查询场景

 官网链接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html

 3. match query

全文查询的标准查询,它可以对一个字段进行模糊、短语查询。 match queries 接收 text/numerics/dates, 对它们进行分词分析, 再组织成一个boolean查询。可通过operator 指定bool组合操作(or、and 默认是 or ), 以及minimum_should_match 指定至少需多少个should(or)字句需满足。还可用ananlyzer指定查询用的特殊分析器。

  1. GET /_search
  2. {
  3. "query": {
  4. "match" : {
  5. "message" : "this is a test"
  6. }
  7. }
  8. }

 说明:message是字段名

 官网链接:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html

 示例:

构造索引和数据:

  1. PUT /ftq/_doc/1
  2. {
  3. "title": "lucene solr and elasticsearch",
  4. "content": "lucene solr and elasticsearch for search"
  5. }
  6. PUT /ftq/_doc/2
  7. {
  8. "title": "java spring boot",
  9. "content": "lucene is writerd by java"
  10. }

 执行查询1

  1. GET ftq/_doc/_validate/query?rewrite=true
  2. {
  3. "query": {
  4. "match": {
  5. "title": "lucene java"
  6. }
  7. }
  8. }

 查询结果1:

  1. {
  2. "valid": true,
  3. "_shards": {
  4. "total": 1,
  5. "successful": 1,
  6. "failed": 0
  7. },
  8. "explanations": [
  9. {
  10. "index": "ftq",
  11. "valid": true,
  12. "explanation": "title:lucene title:java"
  13. }
  14. ]
  15. }

 执行查询2:

  1. GET ftq/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": "lucene java"
  6. }
  7. }
  8. }

 查询结果2:

  1. {
  2. "took": 6,
  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": 0.2876821,
  13. "hits": [
  14. {
  15. "_index": "ftq",
  16. "_type": "_doc",
  17. "_id": "2",
  18. "_score": 0.2876821,
  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": 0.2876821,
  29. "_source": {
  30. "title": "lucene solr and elasticsearch",
  31. "content": "lucene solr and elasticsearch for search"
  32. }
  33. }
  34. ]
  35. }
  36. }

 执行查询3:指定操作符

  1. GET ftq/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": {
  6. "query": "lucene java",
  7. "operator": "and"
  8. }
  9. }
  10. }
  11. }

 查询结果3:

  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": null,
  13. "hits": []
  14. }
  15. }

模糊查询,最大编辑数为2

  1. GET ftq/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": {
  6. "query": "ucen elatic",
  7. "fuzziness": 2
  8. }
  9. }
  10. }
  11. }

模糊查询结果:

  1. {
  2. "took": 280,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1,
  12. "max_score": 0.14384104,
  13. "hits": [
  14. {
  15. "_index": "ftq",
  16. "_type": "_doc",
  17. "_id": "1",
  18. "_score": 0.14384104,
  19. "_source": {
  20. "title": "lucene solr and elasticsearch",
  21. "content": "lucene solr and elasticsearch for search"
  22. }
  23. }
  24. ]
  25. }
  26. }

指定最少需满足两个词匹配

  1. GET ftq/_search
  2. {
  3. "query": {
  4. "match": {
  5. "content": {
  6. "query": "ucen elatic java",
  7. "fuzziness": 2,
  8. "minimum_should_match": 2
  9. }
  10. }
  11. }
  12. }

 查询结果:

  1. {
  2. "took": 19,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1,
  12. "max_score": 0.43152314,
  13. "hits": [
  14. {
  15. "_index": "ftq",
  16. "_type": "_doc",
  17. "_id": "2",
  18. "_score": 0.43152314,
  19. "_source": {
  20. "title": "java spring boot",
  21. "content": "lucene is writerd by java"
  22. }
  23. }
  24. ]
  25. }
  26. }

 可用max_expansions 指定模糊匹配的最大词项数,默认是50。比如:反向索引中有 100 个词项与 ucen 模糊匹配,只选用前50 个。

 4. match  phrase  query

match_phrase 查询用来对一个字段进行短语查询,可以指定 analyzer、slop移动因子。

 对字段进行短语查询1:

 

GET ftq/_search
{
  "query": {
    "match_phrase": {
      "title": "lucene solr"
    }
  }
}

 结果1:

  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": 1,
  12. "max_score": 0.5753642,
  13. "hits": [
  14. {
  15. "_index": "ftq",
  16. "_type": "_doc",
  17. "_id": "1",
  18. "_score": 0.5753642,
  19. "_source": {
  20. "title": "lucene solr and elasticsearch",
  21. "content": "lucene solr and elasticsearch for search"
  22. }
  23. }
  24. ]
  25. }
  26. }

 对字段进行短语查询2:

 

  1. GET ftq/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "title": "lucene elasticsearch"
  6. }
  7. }
  8. }

 

结果2:

  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": null,
  13. "hits": []
  14. }
  15. }

对查询指定移动因子:

 

GET ftq/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "lucene elasticsearch",
        "slop": 2
      }
    }
  }
}

 查询结果:

  1. {
  2. "took": 2174,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1,
  12. "max_score": 0.27517417,
  13. "hits": [
  14. {
  15. "_index": "ftq",
  16. "_type": "_doc",
  17. "_id": "1",
  18. "_score": 0.27517417,
  19. "_source": {
  20. "title": "lucene solr and elasticsearch",
  21. "content": "lucene solr and elasticsearch for search"
  22. }
  23. }
  24. ]
  25. }
  26. }

 5. match  phrase  prefix query

match_phrase_prefix 在 match_phrase 的基础上支持对短语的最后一个词进行前缀匹配

 

GET /_search
{
    "query": {
        "match_phrase_prefix" : {
            "message" : "quick brown f"
        }
    }
}

 指定前缀匹配选用的最大词项数量

  1. GET /_search
  2. {
  3. "query": {
  4. "match_phrase_prefix" : {
  5. "message" : {
  6. "query" : "quick brown f",
  7. "max_expansions" : 10
  8. }
  9. }
  10. }
  11. }

 6. Multi match query

如果你需要在多个字段上进行文本搜索,可用multi_match 。 multi_match在 match的基础上支持对多个字段进行文本查询。

查询1:

  1. GET ftq/_search
  2. {
  3. "query": {
  4. "multi_match" : {
  5. "query": "lucene java",
  6. "fields": [ "title", "content" ]
  7. }
  8. }
  9. }

结果1:

  1. {
  2. "took": 1973,
  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": 0.5753642,
  13. "hits": [
  14. {
  15. "_index": "ftq",
  16. "_type": "_doc",
  17. "_id": "2",
  18. "_score": 0.5753642,
  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": 0.2876821,
  29. "_source": {
  30. "title": "lucene solr and elasticsearch",
  31. "content": "lucene solr and elasticsearch for search"
  32. }
  33. }
  34. ]
  35. }
  36. }

查询2:字段通配符查询

  1. GET ftq/_search
  2. {
  3. "query": {
  4. "multi_match" : {
  5. "query": "lucene java",
  6. "fields": [ "title", "cont*" ]
  7. }
  8. }
  9. }

结果2:

  1. {
  2. "took": 5,
  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": 0.5753642,
  13. "hits": [
  14. {
  15. "_index": "ftq",
  16. "_type": "_doc",
  17. "_id": "2",
  18. "_score": 0.5753642,
  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": 0.2876821,
  29. "_source": {
  30. "title": "lucene solr and elasticsearch",
  31. "content": "lucene solr and elasticsearch for search"
  32. }
  33. }
  34. ]
  35. }
  36. }

查询3:给字段的相关性评分加权重

 

GET ftq/_search?explain=true
{
  "query": {
    "multi_match" : {
      "query":    "lucene elastic", 
      "fields": [ "title^5", "content" ] 
    }
  }
}

结果3:

  1. {
  2. "took": 6,
  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.4384104,
  13. "hits": [
  14. {
  15. "_shard": "[ftq][3]",
  16. "_node": "qkmtovyLRPWjXcfDTryNwA",
  17. "_index": "ftq",
  18. "_type": "_doc",
  19. "_id": "1",
  20. "_score": 1.4384104,
  21. "_source": {
  22. "title": "lucene solr and elasticsearch",
  23. "content": "lucene solr and elasticsearch for search"
  24. },
  25. "_explanation": {
  26. "value": 1.4384104,
  27. "description": "max of:",
  28. "details": [
  29. {
  30. "value": 1.4384104,
  31. "description": "sum of:",
  32. "details": [
  33. {
  34. "value": 1.4384104,
  35. "description": "weight(title:lucene in 0) [PerFieldSimilarity], result of:",
  36. "details": [
  37. {
  38. "value": 1.4384104,
  39. "description": "score(doc=0,freq=1.0 = termFreq=1.0\n), product of:",
  40. "details": [
  41. {
  42. "value": 5,
  43. "description": "boost",
  44. "details": []
  45. },
  46. {
  47. "value": 0.2876821,
  48. "description": "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:",
  49. "details": [
  50. {
  51. "value": 1,
  52. "description": "docFreq",
  53. "details": []
  54. },
  55. {
  56. "value": 1,
  57. "description": "docCount",
  58. "details": []
  59. }
  60. ]
  61. },
  62. {
  63. "value": 1,
  64. "description": "tfNorm, computed as (freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength)) from:",
  65. "details": [
  66. {
  67. "value": 1,
  68. "description": "termFreq=1.0",
  69. "details": []
  70. },
  71. {
  72. "value": 1.2,
  73. "description": "parameter k1",
  74. "details": []
  75. },
  76. {
  77. "value": 0.75,
  78. "description": "parameter b",
  79. "details": []
  80. },
  81. {
  82. "value": 4,
  83. "description": "avgFieldLength",
  84. "details": []
  85. },
  86. {
  87. "value": 4,
  88. "description": "fieldLength",
  89. "details": []
  90. }
  91. ]
  92. }
  93. ]
  94. }
  95. ]
  96. }
  97. ]
  98. },
  99. {
  100. "value": 0.2876821,
  101. "description": "sum of:",
  102. "details": [
  103. {
  104. "value": 0.2876821,
  105. "description": "weight(content:lucene in 0) [PerFieldSimilarity], result of:",
  106. "details": [
  107. {
  108. "value": 0.2876821,
  109. "description": "score(doc=0,freq=1.0 = termFreq=1.0\n), product of:",
  110. "details": [
  111. {
  112. "value": 0.2876821,
  113. "description": "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:",
  114. "details": [
  115. {
  116. "value": 1,
  117. "description": "docFreq",
  118. "details": []
  119. },
  120. {
  121. "value": 1,
  122. "description": "docCount",
  123. "details": []
  124. }
  125. ]
  126. },
  127. {
  128. "value": 1,
  129. "description": "tfNorm, computed as (freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength)) from:",
  130. "details": [
  131. {
  132. "value": 1,
  133. "description": "termFreq=1.0",
  134. "details": []
  135. },
  136. {
  137. "value": 1.2,
  138. "description": "parameter k1",
  139. "details": []
  140. },
  141. {
  142. "value": 0.75,
  143. "description": "parameter b",
  144. "details": []
  145. },
  146. {
  147. "value": 6,
  148. "description": "avgFieldLength",
  149. "details": []
  150. },
  151. {
  152. "value": 6,
  153. "description": "fieldLength",
  154. "details": []
  155. }
  156. ]
  157. }
  158. ]
  159. }
  160. ]
  161. }
  162. ]
  163. }
  164. ]
  165. }
  166. },
  167. {
  168. "_shard": "[ftq][2]",
  169. "_node": "qkmtovyLRPWjXcfDTryNwA",
  170. "_index": "ftq",
  171. "_type": "_doc",
  172. "_id": "2",
  173. "_score": 0.2876821,
  174. "_source": {
  175. "title": "java spring boot",
  176. "content": "lucene is writerd by java"
  177. },
  178. "_explanation": {
  179. "value": 0.2876821,
  180. "description": "max of:",
  181. "details": [
  182. {
  183. "value": 0.2876821,
  184. "description": "sum of:",
  185. "details": [
  186. {
  187. "value": 0.2876821,
  188. "description": "weight(content:lucene in 0) [PerFieldSimilarity], result of:",
  189. "details": [
  190. {
  191. "value": 0.2876821,
  192. "description": "score(doc=0,freq=1.0 = termFreq=1.0\n), product of:",
  193. "details": [
  194. {
  195. "value": 0.2876821,
  196. "description": "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:",
  197. "details": [
  198. {
  199. "value": 1,
  200. "description": "docFreq",
  201. "details": []
  202. },
  203. {
  204. "value": 1,
  205. "description": "docCount",
  206. "details": []
  207. }
  208. ]
  209. },
  210. {
  211. "value": 1,
  212. "description": "tfNorm, computed as (freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength)) from:",
  213. "details": [
  214. {
  215. "value": 1,
  216. "description": "termFreq=1.0",
  217. "details": []
  218. },
  219. {
  220. "value": 1.2,
  221. "description": "parameter k1",
  222. "details": []
  223. },
  224. {
  225. "value": 0.75,
  226. "description": "parameter b",
  227. "details": []
  228. },
  229. {
  230. "value": 5,
  231. "description": "avgFieldLength",
  232. "details": []
  233. },
  234. {
  235. "value": 5,
  236. "description": "fieldLength",
  237. "details": []
  238. }
  239. ]
  240. }
  241. ]
  242. }
  243. ]
  244. }
  245. ]
  246. }
  247. ]
  248. }
  249. }
  250. ]
  251. }
  252. }

7. Common terms query

common 常用词查询

问1、什么是停用词?索引时做停用词处理的目的是什么?

    不再使用的词,做停用词处理的目的是提高索引的效率,去掉不需要的索引操作,即停用词不需要索引
问2、如果在索引时应用停用词处理,下面的两个查询会查询什么词项?
the brown fox—— brown fox
not happy——happy

问3、索引时应用停用词处理对搜索精度是否有影响?如果不做停用词处理又会有什么影响?如何协调这两个问题?如何保证搜索的精确度又兼顾搜索性能?

索引时应用停用词处理对搜索精度有影响,不做停用词处理又会影响索引的效率,要协调这两个问题就必须要使用tf-idf 相关性计算模型

7.1 tf-idf 相关性计算模型简介

tf:term frequency   词频 :指一个词在一篇文档中出现的频率。

如“世界杯”在文档A中出现3次,那么可以定义“世界杯”在文档A中的词频为3。请问在一篇3000字的文章中出现“世界杯”3次和一篇150字的文章中出现3词,哪篇文章更是与“世界杯”有关的。也就是说,简单用出现次数作为频率不够准确。那就用占比来表示:

问:tf值越大是否就一定说明这个词更相关?

 不是,出现太多了说明不重要

 说明:tf的计算不一定非是这样的,可以定义不同的计算方式。

df:document frequency 词的文档频率 :指包含某个词的文档数(有多少文档中包含这个词)。 df越大的词越常见,哪些词会是高频词?

问1:词的df值越大说明这个词在这个文档集中是越重要还是越不重要?

 越不重要

问2:词t的tf高,在文档集中的重要性也高,是否说明文档与该词越相关?举例:整个文档集中只有3篇文档中有“世界杯”,文档A中就出现了“世界杯”好几次。 

 不能说明文档与该词越相关

问3:如何用数值体现词t在文档集中的重要性?df可以吗?

 不可以

 idf:inverse document frequency   词的逆文档频率 :用来表示词在文档集中的重要性。文档总数/ df ,df越小,词越重要,这个值会很大,那就对它取个自然对数,将值映射到一个较小的取值范围。

 

说明: +1 是为了避免除0(即词t在文档集中未出现的情况)

tf-idf 相关性性计算模型:tf-idf t = tf t,d * idf t

 说明: tf-idf 相关性性计算模型的值为词频( tf t,d)乘以词的逆文档频率(idf t

7.2 Common terms query

common 区分常用(高频)词查询让我们可以通过cutoff_frequency来指定一个分界文档频率值,将搜索文本中的词分为高频词和低频词,低频词的重要性高于高频词,先对低频词进行搜索并计算所有匹配文档相关性得分;然后再搜索和高频词匹配的文档,这会搜到很多文档,但只对和低频词重叠的文档进行相关性得分计算(这可保证搜索精确度,同时大大提高搜索性能),和低频词累加作为文档得分。实际执行的搜索是 必须包含低频词 + 或包含高频词。

思考:这样处理下,如果用户输入的都是高频词如 “to be or not to be”结果会是怎样的?你希望是怎样的?

优化:如果都是高频词,那就对这些词进行and 查询。
进一步优化:让用户可以自己定对高频词做and/or 操作,自己定对低频词进行and/or 操作;或指定最少得多少个同时匹配

示例1:

 

GET /_search
{
    "query": {
        "common": {
            "message": {
                "query": "this is bonsai cool",
                "cutoff_frequency": 0.001
            }
        }
    }
}

 

说明:

cutoff_frequency : 值大于1表示文档数,0-1.0表示占比。 此处界定 文档频率大于 0.1%的词为高频词。

示例2:

 

GET /_search
{
    "query": {
        "common": {
            "body": {
                "query": "nelly the elephant as a cartoon",
                "cutoff_frequency": 0.001,
                "low_freq_operator": "and"
            }
        }
    }
}

 

说明:low_freq_operator指定对低频词做与操作

可用参数:minimum_should_match (high_freq, low_freq), low_freq_operator (default “or”) and high_freq_operator (default “or”)、 boost and analyzer

示例3:

 

GET /_search
{
    "query": {
        "common": {
            "body": {
                "query": "nelly the elephant as a cartoon",
                "cutoff_frequency": 0.001,
                "minimum_should_match": 2
            }
        }
    }
}

 

示例4:

 

GET /_search
{
    "query": {
        "common": {
            "body": {
                "query": "nelly the elephant not as a cartoon",
                "cutoff_frequency": 0.001,
                "minimum_should_match": {
                    "low_freq" : 2,
                    "high_freq" : 3
                }
            }
        }
    }
}

 

示例5:

8. Query string query

query_string 查询,让我们可以直接用lucene查询语法写一个查询串进行查询,ES中接到请求后,通过查询解析器解析查询串生成对应的查询。使用它要求掌握lucene的查询语法。

 示例1:指定单个字段查询

  1. GET /_search
  2. {
  3. "query": {
  4. "query_string" : {
  5. "default_field" : "content",
  6. "query" : "this AND that OR thus"
  7. }
  8. }
  9. }

 示例2:指定多字段通配符查询

  1. GET /_search
  2. {
  3. "query": {
  4. "query_string" : {
  5. "fields" : ["content", "name.*^5"],
  6. "query" : "this AND that OR thus"
  7. }
  8. }
  9. }

 可与query同用的参数,如 default_field、fields,及query 串的语法请参考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

 9. 查询描述规则语法(查询解析语法)

Term 词项:

单个词项的表示: 电脑
短语的表示: "联想笔记本电脑"

Field 字段:

字段名:
示例: name:“联想笔记本电脑” AND type:电脑
如果name是默认字段,则可写成: “联想笔记本电脑” AND type:电脑
如果查询串是:type:电脑 计算机 手机
注意:只有第一个是type的值,后两个则是使用默认字段。

 Term Modifiers 词项修饰符:

 

10. Simple Query string query

simple_query_string 查同 query_string 查询一样用lucene查询语法写查询串,较query_string不同的地方:更小的语法集;查询串有错误,它会忽略错误的部分,不抛出错误。更适合给用户使用。

 示例:

 

GET /_search
{
  "query": {
    "simple_query_string" : {
        "query": "\"fried eggs\" +(eggplant | potato) -frittata",
        "fields": ["title^5", "body"],
        "default_operator": "and"
    }
  }
}

 

 语法请参考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html

 11. Term level querys

 

官网链接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/term-level-queries.html

 11.1 Term query

term 查询用于查询指定字段包含某个词项的文档。

 示例1:

POST _search
{
  "query": {
    "term" : { "user" : "Kimchy" } 
  }
}

 示例2:加权重

 

GET _search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "status": {
              "value": "urgent",
              "boost": 2
            }
          }
        },
        {
          "term": {
            "status": "normal"
          }
        }
      ]
    }
  }
}

 

 11.2 Terms query

 terms 查询用于查询指定字段包含某些词项的文档

GET /_search
{
    "query": {
        "terms" : { "user" : ["kimchy", "elasticsearch"]}
    }
}

Terms 查询支持嵌套查询的方式来获得查询词项,相当于 in (select term from other)

示例1:Terms query 嵌套查询示例

 

PUT /users/_doc/2
{
    "followers" : ["1", "3"]
}

PUT /tweets/_doc/1
{
    "user" : "1"
}

GET /tweets/_search
{
  "query": {
    "terms": {
      "user": {
        "index": "users",
        "type": "_doc",
        "id": "2",
        "path": "followers"
      }
    }
  }
}

 

查询结果:

  1. {
  2. "took": 14,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1,
  12. "max_score": 1,
  13. "hits": [
  14. {
  15. "_index": "tweets",
  16. "_type": "_doc",
  17. "_id": "1",
  18. "_score": 1,
  19. "_source": {
  20. "user": "1"
  21. }
  22. }
  23. ]
  24. }
  25. }

嵌套查询可用参数说明:

11.3 range query

 范围查询示例1:

  1. GET _search
  2. {
  3. "query": {
  4. "range" : {
  5. "age" : {
  6. "gte" : 10,
  7. "lte" : 20,
  8. "boost" : 2.0
  9. }
  10. }
  11. }
  12. }

  范围查询示例2:

 

GET _search
{
    "query": {
        "range" : {
            "date" : {
                "gte" : "now-1d/d",
                "lt" :  "now/d"
            }
        }
    }
}

 

  范围查询示例3:

 

GET _search
{
    "query": {
        "range" : {
            "born" : {
                "gte": "01/01/2012",
                "lte": "2013",
                "format": "dd/MM/yyyy||yyyy"
            }
        }
    }
}

 范围查询参数说明:

范围查询时间舍入 ||说明:

时间数学计算规则请参考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math

11.4 exists  query

查询指定字段值不为空的文档。相当 SQL 中的 column is not null

GET /_search
{
    "query": {
        "exists" : { "field" : "user" }
    }
}

查询指定字段值为空的文档

 

GET /_search
{
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "user"
        }
      }
    }
  }
}

 

 11.5 prefix query 词项前缀查询

 示例1:

GET /_search
{ "query": {
    "prefix" : { "user" : "ki" }
  }
}

 示例2:加权

GET /_search
{ "query": {
    "prefix" : { "user" :  { "value" : "ki", "boost" : 2.0 } }
  }
}

 11.6 wildcard query 通配符查询: ? *

 示例1:

GET /_search
{
    "query": {
        "wildcard" : { "user" : "ki*y" }
    }
}

 示例2:加权

 

GET /_search
{
  "query": {
    "wildcard": {
      "user": {
        "value": "ki*y",
        "boost": 2
      }
    }
  }}

 

11.7  regexp query   正则查询

示例1:

 

GET /_search
{
    "query": {
        "regexp":{
            "name.first": "s.*y"
        }
    }
}

 

示例2:加权

 

GET /_search
{
    "query": {
        "regexp":{
            "name.first":{
                "value":"s.*y",
                "boost":1.2
            }
        }
    }
}

 

正则语法请参考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#regexp-syntax

11.8 fuzzy query 模糊查询

示例1:

GET /_search
{
    "query": {
       "fuzzy" : { "user" : "ki" }
    }
}

示例2:

 

GET /_search
{
    "query": {
        "fuzzy" : {
            "user" : {
                "value": "ki",
                "boost": 1.0,
                "fuzziness": 2,
                "prefix_length": 0,
                "max_expansions": 100
            }
        }
    }
}

 

11.9 type query   mapping type 查询

 

GET /_search
{
    "query": {
        "type" : {
            "value" : "_doc"
        }
    }
}

 

11.10 ids query   根据文档id查询

 

GET /_search
{
    "query": {
        "ids" : {
            "type" : "_doc",
            "values" : ["1", "4", "100"]
        }
    }
}

 

12. Compound querys 复合查询

 官网链接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/compound-queries.html

 12.1 Constant Score query

 用来包装另一个查询,将查询匹配的文档的评分设为一个常值。

 

GET /_search
{
    "query": {
        "constant_score" : {
            "filter" : {
                "term" : { "user" : "kimchy"}
            },
            "boost" : 1.2
        }
    }
}

 

 12.2 Bool query

 Bool 查询用bool操作来组合多个查询字句为一个查询。 可用的关键字:

 

示例:

 

POST _search
{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "user" : "kimchy" }
      },
      "filter": {
        "term" : { "tag" : "tech" }
      },
      "must_not" : {
        "range" : {
          "age" : { "gte" : 10, "lte" : 20 }
        }
      },
      "should" : [
        { "term" : { "tag" : "wow" } },
        { "term" : { "tag" : "elasticsearch" } }
      ],
      "minimum_should_match" : 1,
      "boost" : 1.0
    }
  }
}

 

 说明:should满足一个或者两个或者都不满足

 

转自:推荐博客地址

https://www.cnblogs.com/leeSmall/p/9206641.html 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/543405
推荐阅读
相关标签
  

闽ICP备14008679号