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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. type filterFunc func(key []byte) bool
  23. var GeoBitsShift1 = (geo.GeoBits << 1)
  24. var GeoBitsShift1Minus1 = GeoBitsShift1 - 1
  25. func NewGeoBoundingBoxSearcher(indexReader index.IndexReader, minLon, minLat,
  26. maxLon, maxLat float64, field string, boost float64,
  27. options search.SearcherOptions, checkBoundaries bool) (
  28. search.Searcher, error) {
  29. // track list of opened searchers, for cleanup on early exit
  30. var openedSearchers []search.Searcher
  31. cleanupOpenedSearchers := func() {
  32. for _, s := range openedSearchers {
  33. _ = s.Close()
  34. }
  35. }
  36. // do math to produce list of terms needed for this search
  37. onBoundaryTerms, notOnBoundaryTerms, err := ComputeGeoRange(0, GeoBitsShift1Minus1,
  38. minLon, minLat, maxLon, maxLat, checkBoundaries, indexReader, field)
  39. if err != nil {
  40. return nil, err
  41. }
  42. var onBoundarySearcher search.Searcher
  43. dvReader, err := indexReader.DocValueReader([]string{field})
  44. if err != nil {
  45. return nil, err
  46. }
  47. if len(onBoundaryTerms) > 0 {
  48. rawOnBoundarySearcher, err := NewMultiTermSearcherBytes(indexReader,
  49. onBoundaryTerms, field, boost, options, false)
  50. if err != nil {
  51. return nil, err
  52. }
  53. // add filter to check points near the boundary
  54. onBoundarySearcher = NewFilteringSearcher(rawOnBoundarySearcher,
  55. buildRectFilter(dvReader, field, minLon, minLat, maxLon, maxLat))
  56. openedSearchers = append(openedSearchers, onBoundarySearcher)
  57. }
  58. var notOnBoundarySearcher search.Searcher
  59. if len(notOnBoundaryTerms) > 0 {
  60. var err error
  61. notOnBoundarySearcher, err = NewMultiTermSearcherBytes(indexReader,
  62. notOnBoundaryTerms, field, boost, options, false)
  63. if err != nil {
  64. cleanupOpenedSearchers()
  65. return nil, err
  66. }
  67. openedSearchers = append(openedSearchers, notOnBoundarySearcher)
  68. }
  69. if onBoundarySearcher != nil && notOnBoundarySearcher != nil {
  70. rv, err := NewDisjunctionSearcher(indexReader,
  71. []search.Searcher{
  72. onBoundarySearcher,
  73. notOnBoundarySearcher,
  74. },
  75. 0, options)
  76. if err != nil {
  77. cleanupOpenedSearchers()
  78. return nil, err
  79. }
  80. return rv, nil
  81. } else if onBoundarySearcher != nil {
  82. return onBoundarySearcher, nil
  83. } else if notOnBoundarySearcher != nil {
  84. return notOnBoundarySearcher, nil
  85. }
  86. return NewMatchNoneSearcher(indexReader)
  87. }
  88. var geoMaxShift = document.GeoPrecisionStep * 4
  89. var geoDetailLevel = ((geo.GeoBits << 1) - geoMaxShift) / 2
  90. func ComputeGeoRange(term uint64, shift uint,
  91. sminLon, sminLat, smaxLon, smaxLat float64, checkBoundaries bool,
  92. indexReader index.IndexReader, field string) (
  93. onBoundary [][]byte, notOnBoundary [][]byte, err error) {
  94. preallocBytesLen := 32
  95. preallocBytes := make([]byte, preallocBytesLen)
  96. makePrefixCoded := func(in int64, shift uint) (rv numeric.PrefixCoded) {
  97. if len(preallocBytes) <= 0 {
  98. preallocBytesLen = preallocBytesLen * 2
  99. preallocBytes = make([]byte, preallocBytesLen)
  100. }
  101. rv, preallocBytes, err =
  102. numeric.NewPrefixCodedInt64Prealloc(in, shift, preallocBytes)
  103. return rv
  104. }
  105. var fieldDict index.FieldDictContains
  106. var isIndexed filterFunc
  107. if irr, ok := indexReader.(index.IndexReaderContains); ok {
  108. fieldDict, err = irr.FieldDictContains(field)
  109. if err != nil {
  110. return nil, nil, err
  111. }
  112. isIndexed = func(term []byte) bool {
  113. found, err := fieldDict.Contains(term)
  114. return err == nil && found
  115. }
  116. }
  117. defer func() {
  118. if fieldDict != nil {
  119. if fd, ok := fieldDict.(index.FieldDict); ok {
  120. cerr := fd.Close()
  121. if cerr != nil {
  122. err = cerr
  123. }
  124. }
  125. }
  126. }()
  127. if isIndexed == nil {
  128. isIndexed = func(term []byte) bool {
  129. if indexReader != nil {
  130. reader, err := indexReader.TermFieldReader(term, field, false, false, false)
  131. if err != nil || reader == nil {
  132. return false
  133. }
  134. if reader.Count() == 0 {
  135. _ = reader.Close()
  136. return false
  137. }
  138. _ = reader.Close()
  139. }
  140. return true
  141. }
  142. }
  143. var computeGeoRange func(term uint64, shift uint) // declare for recursion
  144. relateAndRecurse := func(start, end uint64, res, level uint) {
  145. minLon := geo.MortonUnhashLon(start)
  146. minLat := geo.MortonUnhashLat(start)
  147. maxLon := geo.MortonUnhashLon(end)
  148. maxLat := geo.MortonUnhashLat(end)
  149. within := res%document.GeoPrecisionStep == 0 &&
  150. geo.RectWithin(minLon, minLat, maxLon, maxLat,
  151. sminLon, sminLat, smaxLon, smaxLat)
  152. if within || (level == geoDetailLevel &&
  153. geo.RectIntersects(minLon, minLat, maxLon, maxLat,
  154. sminLon, sminLat, smaxLon, smaxLat)) {
  155. codedTerm := makePrefixCoded(int64(start), res)
  156. if isIndexed(codedTerm) {
  157. if !within && checkBoundaries {
  158. onBoundary = append(onBoundary, codedTerm)
  159. } else {
  160. notOnBoundary = append(notOnBoundary, codedTerm)
  161. }
  162. }
  163. } else if level < geoDetailLevel &&
  164. geo.RectIntersects(minLon, minLat, maxLon, maxLat,
  165. sminLon, sminLat, smaxLon, smaxLat) {
  166. computeGeoRange(start, res-1)
  167. }
  168. }
  169. computeGeoRange = func(term uint64, shift uint) {
  170. if err != nil {
  171. return
  172. }
  173. split := term | uint64(0x1)<<shift
  174. var upperMax uint64
  175. if shift < 63 {
  176. upperMax = term | ((uint64(1) << (shift + 1)) - 1)
  177. } else {
  178. upperMax = 0xffffffffffffffff
  179. }
  180. lowerMax := split - 1
  181. level := (GeoBitsShift1 - shift) >> 1
  182. relateAndRecurse(term, lowerMax, shift, level)
  183. relateAndRecurse(split, upperMax, shift, level)
  184. }
  185. computeGeoRange(term, shift)
  186. if err != nil {
  187. return nil, nil, err
  188. }
  189. return onBoundary, notOnBoundary, err
  190. }
  191. func buildRectFilter(dvReader index.DocValueReader, field string,
  192. minLon, minLat, maxLon, maxLat float64) FilterFunc {
  193. return func(d *search.DocumentMatch) bool {
  194. // check geo matches against all numeric type terms indexed
  195. var lons, lats []float64
  196. var found bool
  197. err := dvReader.VisitDocValues(d.IndexInternalID, func(field string, term []byte) {
  198. // only consider the values which are shifted 0
  199. prefixCoded := numeric.PrefixCoded(term)
  200. shift, err := prefixCoded.Shift()
  201. if err == nil && shift == 0 {
  202. var i64 int64
  203. i64, err = prefixCoded.Int64()
  204. if err == nil {
  205. lons = append(lons, geo.MortonUnhashLon(uint64(i64)))
  206. lats = append(lats, geo.MortonUnhashLat(uint64(i64)))
  207. found = true
  208. }
  209. }
  210. })
  211. if err == nil && found {
  212. for i := range lons {
  213. if geo.BoundingBoxContains(lons[i], lats[i],
  214. minLon, minLat, maxLon, maxLat) {
  215. return true
  216. }
  217. }
  218. }
  219. return false
  220. }
  221. }