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.

search_geopointdistance.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 searcher
  15. import (
  16. "github.com/blevesearch/bleve/geo"
  17. "github.com/blevesearch/bleve/index"
  18. "github.com/blevesearch/bleve/numeric"
  19. "github.com/blevesearch/bleve/search"
  20. )
  21. func NewGeoPointDistanceSearcher(indexReader index.IndexReader, centerLon,
  22. centerLat, dist float64, field string, boost float64,
  23. options search.SearcherOptions) (search.Searcher, error) {
  24. // compute bounding box containing the circle
  25. topLeftLon, topLeftLat, bottomRightLon, bottomRightLat, err :=
  26. geo.RectFromPointDistance(centerLon, centerLat, dist)
  27. if err != nil {
  28. return nil, err
  29. }
  30. // build a searcher for the box
  31. boxSearcher, err := boxSearcher(indexReader,
  32. topLeftLon, topLeftLat, bottomRightLon, bottomRightLat,
  33. field, boost, options, false)
  34. if err != nil {
  35. return nil, err
  36. }
  37. dvReader, err := indexReader.DocValueReader([]string{field})
  38. if err != nil {
  39. return nil, err
  40. }
  41. // wrap it in a filtering searcher which checks the actual distance
  42. return NewFilteringSearcher(boxSearcher,
  43. buildDistFilter(dvReader, field, centerLon, centerLat, dist)), nil
  44. }
  45. // boxSearcher builds a searcher for the described bounding box
  46. // if the desired box crosses the dateline, it is automatically split into
  47. // two boxes joined through a disjunction searcher
  48. func boxSearcher(indexReader index.IndexReader,
  49. topLeftLon, topLeftLat, bottomRightLon, bottomRightLat float64,
  50. field string, boost float64, options search.SearcherOptions, checkBoundaries bool) (
  51. search.Searcher, error) {
  52. if bottomRightLon < topLeftLon {
  53. // cross date line, rewrite as two parts
  54. leftSearcher, err := NewGeoBoundingBoxSearcher(indexReader,
  55. -180, bottomRightLat, bottomRightLon, topLeftLat,
  56. field, boost, options, checkBoundaries)
  57. if err != nil {
  58. return nil, err
  59. }
  60. rightSearcher, err := NewGeoBoundingBoxSearcher(indexReader,
  61. topLeftLon, bottomRightLat, 180, topLeftLat, field, boost, options,
  62. checkBoundaries)
  63. if err != nil {
  64. _ = leftSearcher.Close()
  65. return nil, err
  66. }
  67. boxSearcher, err := NewDisjunctionSearcher(indexReader,
  68. []search.Searcher{leftSearcher, rightSearcher}, 0, options)
  69. if err != nil {
  70. _ = leftSearcher.Close()
  71. _ = rightSearcher.Close()
  72. return nil, err
  73. }
  74. return boxSearcher, nil
  75. }
  76. // build geoboundingbox searcher for that bounding box
  77. boxSearcher, err := NewGeoBoundingBoxSearcher(indexReader,
  78. topLeftLon, bottomRightLat, bottomRightLon, topLeftLat, field, boost,
  79. options, checkBoundaries)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return boxSearcher, nil
  84. }
  85. func buildDistFilter(dvReader index.DocValueReader, field string,
  86. centerLon, centerLat, maxDist float64) FilterFunc {
  87. return func(d *search.DocumentMatch) bool {
  88. // check geo matches against all numeric type terms indexed
  89. var lons, lats []float64
  90. var found bool
  91. err := dvReader.VisitDocValues(d.IndexInternalID, func(field string, term []byte) {
  92. // only consider the values which are shifted 0
  93. prefixCoded := numeric.PrefixCoded(term)
  94. shift, err := prefixCoded.Shift()
  95. if err == nil && shift == 0 {
  96. i64, err := prefixCoded.Int64()
  97. if err == nil {
  98. lons = append(lons, geo.MortonUnhashLon(uint64(i64)))
  99. lats = append(lats, geo.MortonUnhashLat(uint64(i64)))
  100. found = true
  101. }
  102. }
  103. })
  104. if err == nil && found {
  105. for i := range lons {
  106. dist := geo.Haversin(lons[i], lats[i], centerLon, centerLat)
  107. if dist <= maxDist/1000 {
  108. return true
  109. }
  110. }
  111. }
  112. return false
  113. }
  114. }