field_geopoint.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 document
  15. import (
  16. "fmt"
  17. "reflect"
  18. "github.com/blevesearch/bleve/analysis"
  19. "github.com/blevesearch/bleve/geo"
  20. "github.com/blevesearch/bleve/numeric"
  21. "github.com/blevesearch/bleve/size"
  22. )
  23. var reflectStaticSizeGeoPointField int
  24. func init() {
  25. var f GeoPointField
  26. reflectStaticSizeGeoPointField = int(reflect.TypeOf(f).Size())
  27. }
  28. var GeoPrecisionStep uint = 9
  29. type GeoPointField struct {
  30. name string
  31. arrayPositions []uint64
  32. options IndexingOptions
  33. value numeric.PrefixCoded
  34. numPlainTextBytes uint64
  35. }
  36. func (n *GeoPointField) Size() int {
  37. return reflectStaticSizeGeoPointField + size.SizeOfPtr +
  38. len(n.name) +
  39. len(n.arrayPositions)*size.SizeOfUint64
  40. }
  41. func (n *GeoPointField) Name() string {
  42. return n.name
  43. }
  44. func (n *GeoPointField) ArrayPositions() []uint64 {
  45. return n.arrayPositions
  46. }
  47. func (n *GeoPointField) Options() IndexingOptions {
  48. return n.options
  49. }
  50. func (n *GeoPointField) Analyze() (int, analysis.TokenFrequencies) {
  51. tokens := make(analysis.TokenStream, 0)
  52. tokens = append(tokens, &analysis.Token{
  53. Start: 0,
  54. End: len(n.value),
  55. Term: n.value,
  56. Position: 1,
  57. Type: analysis.Numeric,
  58. })
  59. original, err := n.value.Int64()
  60. if err == nil {
  61. shift := GeoPrecisionStep
  62. for shift < 64 {
  63. shiftEncoded, err := numeric.NewPrefixCodedInt64(original, shift)
  64. if err != nil {
  65. break
  66. }
  67. token := analysis.Token{
  68. Start: 0,
  69. End: len(shiftEncoded),
  70. Term: shiftEncoded,
  71. Position: 1,
  72. Type: analysis.Numeric,
  73. }
  74. tokens = append(tokens, &token)
  75. shift += GeoPrecisionStep
  76. }
  77. }
  78. fieldLength := len(tokens)
  79. tokenFreqs := analysis.TokenFrequency(tokens, n.arrayPositions, n.options.IncludeTermVectors())
  80. return fieldLength, tokenFreqs
  81. }
  82. func (n *GeoPointField) Value() []byte {
  83. return n.value
  84. }
  85. func (n *GeoPointField) Lon() (float64, error) {
  86. i64, err := n.value.Int64()
  87. if err != nil {
  88. return 0.0, err
  89. }
  90. return geo.MortonUnhashLon(uint64(i64)), nil
  91. }
  92. func (n *GeoPointField) Lat() (float64, error) {
  93. i64, err := n.value.Int64()
  94. if err != nil {
  95. return 0.0, err
  96. }
  97. return geo.MortonUnhashLat(uint64(i64)), nil
  98. }
  99. func (n *GeoPointField) GoString() string {
  100. return fmt.Sprintf("&document.GeoPointField{Name:%s, Options: %s, Value: %s}", n.name, n.options, n.value)
  101. }
  102. func (n *GeoPointField) NumPlainTextBytes() uint64 {
  103. return n.numPlainTextBytes
  104. }
  105. func NewGeoPointFieldFromBytes(name string, arrayPositions []uint64, value []byte) *GeoPointField {
  106. return &GeoPointField{
  107. name: name,
  108. arrayPositions: arrayPositions,
  109. value: value,
  110. options: DefaultNumericIndexingOptions,
  111. numPlainTextBytes: uint64(len(value)),
  112. }
  113. }
  114. func NewGeoPointField(name string, arrayPositions []uint64, lon, lat float64) *GeoPointField {
  115. return NewGeoPointFieldWithIndexingOptions(name, arrayPositions, lon, lat, DefaultNumericIndexingOptions)
  116. }
  117. func NewGeoPointFieldWithIndexingOptions(name string, arrayPositions []uint64, lon, lat float64, options IndexingOptions) *GeoPointField {
  118. mhash := geo.MortonHash(lon, lat)
  119. prefixCoded := numeric.MustNewPrefixCodedInt64(int64(mhash), 0)
  120. return &GeoPointField{
  121. name: name,
  122. arrayPositions: arrayPositions,
  123. value: prefixCoded,
  124. options: options,
  125. // not correct, just a place holder until we revisit how fields are
  126. // represented and can fix this better
  127. numPlainTextBytes: uint64(8),
  128. }
  129. }