search_queries_geo_bounding_box.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import "errors"
  6. // GeoBoundingBoxQuery allows to filter hits based on a point location using
  7. // a bounding box.
  8. //
  9. // For more details, see:
  10. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-geo-bounding-box-query.html
  11. type GeoBoundingBoxQuery struct {
  12. name string
  13. top *float64
  14. left *float64
  15. bottom *float64
  16. right *float64
  17. typ string
  18. queryName string
  19. }
  20. // NewGeoBoundingBoxQuery creates and initializes a new GeoBoundingBoxQuery.
  21. func NewGeoBoundingBoxQuery(name string) *GeoBoundingBoxQuery {
  22. return &GeoBoundingBoxQuery{
  23. name: name,
  24. }
  25. }
  26. func (q *GeoBoundingBoxQuery) TopLeft(top, left float64) *GeoBoundingBoxQuery {
  27. q.top = &top
  28. q.left = &left
  29. return q
  30. }
  31. func (q *GeoBoundingBoxQuery) TopLeftFromGeoPoint(point *GeoPoint) *GeoBoundingBoxQuery {
  32. return q.TopLeft(point.Lat, point.Lon)
  33. }
  34. func (q *GeoBoundingBoxQuery) BottomRight(bottom, right float64) *GeoBoundingBoxQuery {
  35. q.bottom = &bottom
  36. q.right = &right
  37. return q
  38. }
  39. func (q *GeoBoundingBoxQuery) BottomRightFromGeoPoint(point *GeoPoint) *GeoBoundingBoxQuery {
  40. return q.BottomRight(point.Lat, point.Lon)
  41. }
  42. func (q *GeoBoundingBoxQuery) BottomLeft(bottom, left float64) *GeoBoundingBoxQuery {
  43. q.bottom = &bottom
  44. q.left = &left
  45. return q
  46. }
  47. func (q *GeoBoundingBoxQuery) BottomLeftFromGeoPoint(point *GeoPoint) *GeoBoundingBoxQuery {
  48. return q.BottomLeft(point.Lat, point.Lon)
  49. }
  50. func (q *GeoBoundingBoxQuery) TopRight(top, right float64) *GeoBoundingBoxQuery {
  51. q.top = &top
  52. q.right = &right
  53. return q
  54. }
  55. func (q *GeoBoundingBoxQuery) TopRightFromGeoPoint(point *GeoPoint) *GeoBoundingBoxQuery {
  56. return q.TopRight(point.Lat, point.Lon)
  57. }
  58. // Type sets the type of executing the geo bounding box. It can be either
  59. // memory or indexed. It defaults to memory.
  60. func (q *GeoBoundingBoxQuery) Type(typ string) *GeoBoundingBoxQuery {
  61. q.typ = typ
  62. return q
  63. }
  64. func (q *GeoBoundingBoxQuery) QueryName(queryName string) *GeoBoundingBoxQuery {
  65. q.queryName = queryName
  66. return q
  67. }
  68. // Source returns JSON for the function score query.
  69. func (q *GeoBoundingBoxQuery) Source() (interface{}, error) {
  70. // {
  71. // "geo_bounding_box" : {
  72. // ...
  73. // }
  74. // }
  75. if q.top == nil {
  76. return nil, errors.New("geo_bounding_box requires top latitude to be set")
  77. }
  78. if q.bottom == nil {
  79. return nil, errors.New("geo_bounding_box requires bottom latitude to be set")
  80. }
  81. if q.right == nil {
  82. return nil, errors.New("geo_bounding_box requires right longitude to be set")
  83. }
  84. if q.left == nil {
  85. return nil, errors.New("geo_bounding_box requires left longitude to be set")
  86. }
  87. source := make(map[string]interface{})
  88. params := make(map[string]interface{})
  89. source["geo_bounding_box"] = params
  90. box := make(map[string]interface{})
  91. box["top_left"] = []float64{*q.left, *q.top}
  92. box["bottom_right"] = []float64{*q.right, *q.bottom}
  93. params[q.name] = box
  94. if q.typ != "" {
  95. params["type"] = q.typ
  96. }
  97. if q.queryName != "" {
  98. params["_name"] = q.queryName
  99. }
  100. return source, nil
  101. }