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.

field_geopoint.go 3.6KB

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