本站源代码
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.

123 lines
3.8KB

  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)
  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) (
  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, false)
  57. if err != nil {
  58. return nil, err
  59. }
  60. rightSearcher, err := NewGeoBoundingBoxSearcher(indexReader,
  61. topLeftLon, bottomRightLat, 180, topLeftLat, field, boost, options, false)
  62. if err != nil {
  63. _ = leftSearcher.Close()
  64. return nil, err
  65. }
  66. boxSearcher, err := NewDisjunctionSearcher(indexReader,
  67. []search.Searcher{leftSearcher, rightSearcher}, 0, options)
  68. if err != nil {
  69. _ = leftSearcher.Close()
  70. _ = rightSearcher.Close()
  71. return nil, err
  72. }
  73. return boxSearcher, nil
  74. }
  75. // build geoboundinggox searcher for that bounding box
  76. boxSearcher, err := NewGeoBoundingBoxSearcher(indexReader,
  77. topLeftLon, bottomRightLat, bottomRightLon, topLeftLat, field, boost,
  78. options, false)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return boxSearcher, nil
  83. }
  84. func buildDistFilter(dvReader index.DocValueReader, field string,
  85. centerLon, centerLat, maxDist float64) FilterFunc {
  86. return func(d *search.DocumentMatch) bool {
  87. var lon, lat float64
  88. var found bool
  89. err := dvReader.VisitDocValues(d.IndexInternalID, func(field string, term []byte) {
  90. // only consider the values which are shifted 0
  91. prefixCoded := numeric.PrefixCoded(term)
  92. shift, err := prefixCoded.Shift()
  93. if err == nil && shift == 0 {
  94. i64, err := prefixCoded.Int64()
  95. if err == nil {
  96. lon = geo.MortonUnhashLon(uint64(i64))
  97. lat = geo.MortonUnhashLat(uint64(i64))
  98. found = true
  99. }
  100. }
  101. })
  102. if err == nil && found {
  103. dist := geo.Haversin(lon, lat, centerLon, centerLat)
  104. if dist <= maxDist/1000 {
  105. return true
  106. }
  107. }
  108. return false
  109. }
  110. }
上海开阖软件有限公司 沪ICP备12045867号-1