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_geoboundingbox.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/document"
  17. "github.com/blevesearch/bleve/geo"
  18. "github.com/blevesearch/bleve/index"
  19. "github.com/blevesearch/bleve/numeric"
  20. "github.com/blevesearch/bleve/search"
  21. )
  22. func NewGeoBoundingBoxSearcher(indexReader index.IndexReader, minLon, minLat,
  23. maxLon, maxLat float64, field string, boost float64,
  24. options search.SearcherOptions, checkBoundaries bool) (
  25. search.Searcher, error) {
  26. // track list of opened searchers, for cleanup on early exit
  27. var openedSearchers []search.Searcher
  28. cleanupOpenedSearchers := func() {
  29. for _, s := range openedSearchers {
  30. _ = s.Close()
  31. }
  32. }
  33. // do math to produce list of terms needed for this search
  34. onBoundaryTerms, notOnBoundaryTerms := ComputeGeoRange(0, (geo.GeoBits<<1)-1,
  35. minLon, minLat, maxLon, maxLat, checkBoundaries)
  36. var onBoundarySearcher search.Searcher
  37. if len(onBoundaryTerms) > 0 {
  38. rawOnBoundarySearcher, err := NewMultiTermSearcherBytes(indexReader,
  39. onBoundaryTerms, field, boost, options, false)
  40. if err != nil {
  41. return nil, err
  42. }
  43. // add filter to check points near the boundary
  44. onBoundarySearcher = NewFilteringSearcher(rawOnBoundarySearcher,
  45. buildRectFilter(indexReader, field, minLon, minLat, maxLon, maxLat))
  46. openedSearchers = append(openedSearchers, onBoundarySearcher)
  47. }
  48. var notOnBoundarySearcher search.Searcher
  49. if len(notOnBoundaryTerms) > 0 {
  50. var err error
  51. notOnBoundarySearcher, err = NewMultiTermSearcherBytes(indexReader,
  52. notOnBoundaryTerms, field, boost, options, false)
  53. if err != nil {
  54. cleanupOpenedSearchers()
  55. return nil, err
  56. }
  57. openedSearchers = append(openedSearchers, notOnBoundarySearcher)
  58. }
  59. if onBoundarySearcher != nil && notOnBoundarySearcher != nil {
  60. rv, err := NewDisjunctionSearcher(indexReader,
  61. []search.Searcher{
  62. onBoundarySearcher,
  63. notOnBoundarySearcher,
  64. },
  65. 0, options)
  66. if err != nil {
  67. cleanupOpenedSearchers()
  68. return nil, err
  69. }
  70. return rv, nil
  71. } else if onBoundarySearcher != nil {
  72. return onBoundarySearcher, nil
  73. } else if notOnBoundarySearcher != nil {
  74. return notOnBoundarySearcher, nil
  75. }
  76. return NewMatchNoneSearcher(indexReader)
  77. }
  78. var geoMaxShift = document.GeoPrecisionStep * 4
  79. var geoDetailLevel = ((geo.GeoBits << 1) - geoMaxShift) / 2
  80. func ComputeGeoRange(term uint64, shift uint,
  81. sminLon, sminLat, smaxLon, smaxLat float64,
  82. checkBoundaries bool) (
  83. onBoundary [][]byte, notOnBoundary [][]byte) {
  84. split := term | uint64(0x1)<<shift
  85. var upperMax uint64
  86. if shift < 63 {
  87. upperMax = term | ((uint64(1) << (shift + 1)) - 1)
  88. } else {
  89. upperMax = 0xffffffffffffffff
  90. }
  91. lowerMax := split - 1
  92. onBoundary, notOnBoundary = relateAndRecurse(term, lowerMax, shift,
  93. sminLon, sminLat, smaxLon, smaxLat, checkBoundaries)
  94. plusOnBoundary, plusNotOnBoundary := relateAndRecurse(split, upperMax, shift,
  95. sminLon, sminLat, smaxLon, smaxLat, checkBoundaries)
  96. onBoundary = append(onBoundary, plusOnBoundary...)
  97. notOnBoundary = append(notOnBoundary, plusNotOnBoundary...)
  98. return
  99. }
  100. func relateAndRecurse(start, end uint64, res uint,
  101. sminLon, sminLat, smaxLon, smaxLat float64,
  102. checkBoundaries bool) (
  103. onBoundary [][]byte, notOnBoundary [][]byte) {
  104. minLon := geo.MortonUnhashLon(start)
  105. minLat := geo.MortonUnhashLat(start)
  106. maxLon := geo.MortonUnhashLon(end)
  107. maxLat := geo.MortonUnhashLat(end)
  108. level := ((geo.GeoBits << 1) - res) >> 1
  109. within := res%document.GeoPrecisionStep == 0 &&
  110. geo.RectWithin(minLon, minLat, maxLon, maxLat,
  111. sminLon, sminLat, smaxLon, smaxLat)
  112. if within || (level == geoDetailLevel &&
  113. geo.RectIntersects(minLon, minLat, maxLon, maxLat,
  114. sminLon, sminLat, smaxLon, smaxLat)) {
  115. if !within && checkBoundaries {
  116. return [][]byte{
  117. numeric.MustNewPrefixCodedInt64(int64(start), res),
  118. }, nil
  119. }
  120. return nil,
  121. [][]byte{
  122. numeric.MustNewPrefixCodedInt64(int64(start), res),
  123. }
  124. } else if level < geoDetailLevel &&
  125. geo.RectIntersects(minLon, minLat, maxLon, maxLat,
  126. sminLon, sminLat, smaxLon, smaxLat) {
  127. return ComputeGeoRange(start, res-1, sminLon, sminLat, smaxLon, smaxLat,
  128. checkBoundaries)
  129. }
  130. return nil, nil
  131. }
  132. func buildRectFilter(indexReader index.IndexReader, field string,
  133. minLon, minLat, maxLon, maxLat float64) FilterFunc {
  134. return func(d *search.DocumentMatch) bool {
  135. var lon, lat float64
  136. var found bool
  137. err := indexReader.DocumentVisitFieldTerms(d.IndexInternalID,
  138. []string{field}, func(field string, term []byte) {
  139. // only consider the values which are shifted 0
  140. prefixCoded := numeric.PrefixCoded(term)
  141. shift, err := prefixCoded.Shift()
  142. if err == nil && shift == 0 {
  143. var i64 int64
  144. i64, err = prefixCoded.Int64()
  145. if err == nil {
  146. lon = geo.MortonUnhashLon(uint64(i64))
  147. lat = geo.MortonUnhashLat(uint64(i64))
  148. found = true
  149. }
  150. }
  151. })
  152. if err == nil && found {
  153. return geo.BoundingBoxContains(lon, lat,
  154. minLon, minLat, maxLon, maxLat)
  155. }
  156. return false
  157. }
  158. }