You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

geo_boundingbox.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2017 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package query
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/blevesearch/bleve/geo"
  19. "github.com/blevesearch/bleve/index"
  20. "github.com/blevesearch/bleve/mapping"
  21. "github.com/blevesearch/bleve/search"
  22. "github.com/blevesearch/bleve/search/searcher"
  23. )
  24. type GeoBoundingBoxQuery struct {
  25. TopLeft []float64 `json:"top_left,omitempty"`
  26. BottomRight []float64 `json:"bottom_right,omitempty"`
  27. FieldVal string `json:"field,omitempty"`
  28. BoostVal *Boost `json:"boost,omitempty"`
  29. }
  30. func NewGeoBoundingBoxQuery(topLeftLon, topLeftLat, bottomRightLon, bottomRightLat float64) *GeoBoundingBoxQuery {
  31. return &GeoBoundingBoxQuery{
  32. TopLeft: []float64{topLeftLon, topLeftLat},
  33. BottomRight: []float64{bottomRightLon, bottomRightLat},
  34. }
  35. }
  36. func (q *GeoBoundingBoxQuery) SetBoost(b float64) {
  37. boost := Boost(b)
  38. q.BoostVal = &boost
  39. }
  40. func (q *GeoBoundingBoxQuery) Boost() float64 {
  41. return q.BoostVal.Value()
  42. }
  43. func (q *GeoBoundingBoxQuery) SetField(f string) {
  44. q.FieldVal = f
  45. }
  46. func (q *GeoBoundingBoxQuery) Field() string {
  47. return q.FieldVal
  48. }
  49. func (q *GeoBoundingBoxQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, options search.SearcherOptions) (search.Searcher, error) {
  50. field := q.FieldVal
  51. if q.FieldVal == "" {
  52. field = m.DefaultSearchField()
  53. }
  54. if q.BottomRight[0] < q.TopLeft[0] {
  55. // cross date line, rewrite as two parts
  56. leftSearcher, err := searcher.NewGeoBoundingBoxSearcher(i, -180, q.BottomRight[1], q.BottomRight[0], q.TopLeft[1], field, q.BoostVal.Value(), options, true)
  57. if err != nil {
  58. return nil, err
  59. }
  60. rightSearcher, err := searcher.NewGeoBoundingBoxSearcher(i, q.TopLeft[0], q.BottomRight[1], 180, q.TopLeft[1], field, q.BoostVal.Value(), options, true)
  61. if err != nil {
  62. _ = leftSearcher.Close()
  63. return nil, err
  64. }
  65. return searcher.NewDisjunctionSearcher(i, []search.Searcher{leftSearcher, rightSearcher}, 0, options)
  66. }
  67. return searcher.NewGeoBoundingBoxSearcher(i, q.TopLeft[0], q.BottomRight[1], q.BottomRight[0], q.TopLeft[1], field, q.BoostVal.Value(), options, true)
  68. }
  69. func (q *GeoBoundingBoxQuery) Validate() error {
  70. return nil
  71. }
  72. func (q *GeoBoundingBoxQuery) UnmarshalJSON(data []byte) error {
  73. tmp := struct {
  74. TopLeft interface{} `json:"top_left,omitempty"`
  75. BottomRight interface{} `json:"bottom_right,omitempty"`
  76. FieldVal string `json:"field,omitempty"`
  77. BoostVal *Boost `json:"boost,omitempty"`
  78. }{}
  79. err := json.Unmarshal(data, &tmp)
  80. if err != nil {
  81. return err
  82. }
  83. // now use our generic point parsing code from the geo package
  84. lon, lat, found := geo.ExtractGeoPoint(tmp.TopLeft)
  85. if !found {
  86. return fmt.Errorf("geo location top_left not in a valid format")
  87. }
  88. q.TopLeft = []float64{lon, lat}
  89. lon, lat, found = geo.ExtractGeoPoint(tmp.BottomRight)
  90. if !found {
  91. return fmt.Errorf("geo location bottom_right not in a valid format")
  92. }
  93. q.BottomRight = []float64{lon, lat}
  94. q.FieldVal = tmp.FieldVal
  95. q.BoostVal = tmp.BoostVal
  96. return nil
  97. }