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

178 lines
5.5KB

  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. dvReader, err := indexReader.DocValueReader([]string{field})
  38. if err != nil {
  39. return nil, err
  40. }
  41. if len(onBoundaryTerms) > 0 {
  42. rawOnBoundarySearcher, err := NewMultiTermSearcherBytes(indexReader,
  43. onBoundaryTerms, field, boost, options, false)
  44. if err != nil {
  45. return nil, err
  46. }
  47. // add filter to check points near the boundary
  48. onBoundarySearcher = NewFilteringSearcher(rawOnBoundarySearcher,
  49. buildRectFilter(dvReader, field, minLon, minLat, maxLon, maxLat))
  50. openedSearchers = append(openedSearchers, onBoundarySearcher)
  51. }
  52. var notOnBoundarySearcher search.Searcher
  53. if len(notOnBoundaryTerms) > 0 {
  54. var err error
  55. notOnBoundarySearcher, err = NewMultiTermSearcherBytes(indexReader,
  56. notOnBoundaryTerms, field, boost, options, false)
  57. if err != nil {
  58. cleanupOpenedSearchers()
  59. return nil, err
  60. }
  61. openedSearchers = append(openedSearchers, notOnBoundarySearcher)
  62. }
  63. if onBoundarySearcher != nil && notOnBoundarySearcher != nil {
  64. rv, err := NewDisjunctionSearcher(indexReader,
  65. []search.Searcher{
  66. onBoundarySearcher,
  67. notOnBoundarySearcher,
  68. },
  69. 0, options)
  70. if err != nil {
  71. cleanupOpenedSearchers()
  72. return nil, err
  73. }
  74. return rv, nil
  75. } else if onBoundarySearcher != nil {
  76. return onBoundarySearcher, nil
  77. } else if notOnBoundarySearcher != nil {
  78. return notOnBoundarySearcher, nil
  79. }
  80. return NewMatchNoneSearcher(indexReader)
  81. }
  82. var geoMaxShift = document.GeoPrecisionStep * 4
  83. var geoDetailLevel = ((geo.GeoBits << 1) - geoMaxShift) / 2
  84. func ComputeGeoRange(term uint64, shift uint,
  85. sminLon, sminLat, smaxLon, smaxLat float64,
  86. checkBoundaries bool) (
  87. onBoundary [][]byte, notOnBoundary [][]byte) {
  88. split := term | uint64(0x1)<<shift
  89. var upperMax uint64
  90. if shift < 63 {
  91. upperMax = term | ((uint64(1) << (shift + 1)) - 1)
  92. } else {
  93. upperMax = 0xffffffffffffffff
  94. }
  95. lowerMax := split - 1
  96. onBoundary, notOnBoundary = relateAndRecurse(term, lowerMax, shift,
  97. sminLon, sminLat, smaxLon, smaxLat, checkBoundaries)
  98. plusOnBoundary, plusNotOnBoundary := relateAndRecurse(split, upperMax, shift,
  99. sminLon, sminLat, smaxLon, smaxLat, checkBoundaries)
  100. onBoundary = append(onBoundary, plusOnBoundary...)
  101. notOnBoundary = append(notOnBoundary, plusNotOnBoundary...)
  102. return
  103. }
  104. func relateAndRecurse(start, end uint64, res uint,
  105. sminLon, sminLat, smaxLon, smaxLat float64,
  106. checkBoundaries bool) (
  107. onBoundary [][]byte, notOnBoundary [][]byte) {
  108. minLon := geo.MortonUnhashLon(start)
  109. minLat := geo.MortonUnhashLat(start)
  110. maxLon := geo.MortonUnhashLon(end)
  111. maxLat := geo.MortonUnhashLat(end)
  112. level := ((geo.GeoBits << 1) - res) >> 1
  113. within := res%document.GeoPrecisionStep == 0 &&
  114. geo.RectWithin(minLon, minLat, maxLon, maxLat,
  115. sminLon, sminLat, smaxLon, smaxLat)
  116. if within || (level == geoDetailLevel &&
  117. geo.RectIntersects(minLon, minLat, maxLon, maxLat,
  118. sminLon, sminLat, smaxLon, smaxLat)) {
  119. if !within && checkBoundaries {
  120. return [][]byte{
  121. numeric.MustNewPrefixCodedInt64(int64(start), res),
  122. }, nil
  123. }
  124. return nil,
  125. [][]byte{
  126. numeric.MustNewPrefixCodedInt64(int64(start), res),
  127. }
  128. } else if level < geoDetailLevel &&
  129. geo.RectIntersects(minLon, minLat, maxLon, maxLat,
  130. sminLon, sminLat, smaxLon, smaxLat) {
  131. return ComputeGeoRange(start, res-1, sminLon, sminLat, smaxLon, smaxLat,
  132. checkBoundaries)
  133. }
  134. return nil, nil
  135. }
  136. func buildRectFilter(dvReader index.DocValueReader, field string,
  137. minLon, minLat, maxLon, maxLat float64) FilterFunc {
  138. return func(d *search.DocumentMatch) bool {
  139. var lon, lat float64
  140. var found bool
  141. err := dvReader.VisitDocValues(d.IndexInternalID, func(field string, term []byte) {
  142. // only consider the values which are shifted 0
  143. prefixCoded := numeric.PrefixCoded(term)
  144. shift, err := prefixCoded.Shift()
  145. if err == nil && shift == 0 {
  146. var i64 int64
  147. i64, err = prefixCoded.Int64()
  148. if err == nil {
  149. lon = geo.MortonUnhashLon(uint64(i64))
  150. lat = geo.MortonUnhashLat(uint64(i64))
  151. found = true
  152. }
  153. }
  154. })
  155. if err == nil && found {
  156. return geo.BoundingBoxContains(lon, lat,
  157. minLon, minLat, maxLon, maxLat)
  158. }
  159. return false
  160. }
  161. }
上海开阖软件有限公司 沪ICP备12045867号-1