32_Bounding_box.asciidoc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. [[geo-bounding-box]]
  2. === 地理坐标盒模型过滤器
  3. 这是目前为止最有效的地理坐标过滤器了,因为它计算起来非常简单。 ((("geo_bounding_box filter")))((("filtering", "by geo-points", "geo_bounding_box filter"))) 你指定一个矩形的 `顶部` , `底部` , `左边界` ,和 `右边界` ,然后过滤器只需判断坐标的经度是否在左右边界之间,纬度是否在上下边界之间:
  4. [source,json]
  5. ---------------------
  6. GET /attractions/restaurant/_search
  7. {
  8. "query": {
  9. "filtered": {
  10. "filter": {
  11. "geo_bounding_box": {
  12. "location": { <1>
  13. "top_left": {
  14. "lat": 40.8,
  15. "lon": -74.0
  16. },
  17. "bottom_right": {
  18. "lat": 40.7,
  19. "lon": -73.0
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }
  27. ---------------------
  28. <1> 这些坐标也可以用 `bottom_left` 和 `top_right` 来表示。
  29. [[optimize-bounding-box]]
  30. ==== 优化盒模型
  31. `地理坐标盒模型过滤器` 不需要把所有坐标点都加载到内存里。((("geo_bounding_box filter", "optimization"))) 因为它要做的
  32. 只是简单判断 `lat` 和 `lon` 坐标数值是否在给定的范围内,可以用倒排索引做一个((("range filters"))) `range` 过滤来实现目标。
  33. 要使用这种优化方式,需要把 `geo_point` 字段 ((("latitude/longitude pairs", "geo-point fields mapped to index lat/lon values separately")))用 `lat` 和 `lon` 的方式分别映射到索引中:
  34. [source,json]
  35. -----------------------
  36. PUT /attractions
  37. {
  38. "mappings": {
  39. "restaurant": {
  40. "properties": {
  41. "name": {
  42. "type": "string"
  43. },
  44. "location": {
  45. "type": "geo_point",
  46. "lat_lon": true <1>
  47. }
  48. }
  49. }
  50. }
  51. }
  52. -----------------------
  53. <1> `location.lat` 和 `location.lon` 字段将被分别索引。它们可以被用于检索,但是不会在检索结果中返回。
  54. 然后,查询时你需要告诉 Elasticesearch 使用已索引的 `lat` 和 `lon` :
  55. [source,json]
  56. ---------------------
  57. GET /attractions/restaurant/_search
  58. {
  59. "query": {
  60. "filtered": {
  61. "filter": {
  62. "geo_bounding_box": {
  63. "type": "indexed", <1>
  64. "location": {
  65. "top_left": {
  66. "lat": 40.8,
  67. "lon": -74.0
  68. },
  69. "bottom_right": {
  70. "lat": 40.7,
  71. "lon": -73.0
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. ---------------------
  80. <1> 设置 `type` 参数为 `indexed` (替代默认值 `memory` )来明确告诉 Elasticsearch 对这个过滤器使用倒排索引。
  81. CAUTION: `geo_point` 类型的字段可以包含多个地理坐标点,但是针对经度纬度分别索引的这种优化方式只对包含单个坐标点的字段有效。