field_numeric.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (c) 2014 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/numeric"
  20. "github.com/blevesearch/bleve/size"
  21. )
  22. var reflectStaticSizeNumericField int
  23. func init() {
  24. var f NumericField
  25. reflectStaticSizeNumericField = int(reflect.TypeOf(f).Size())
  26. }
  27. const DefaultNumericIndexingOptions = StoreField | IndexField | DocValues
  28. const DefaultPrecisionStep uint = 4
  29. type NumericField struct {
  30. name string
  31. arrayPositions []uint64
  32. options IndexingOptions
  33. value numeric.PrefixCoded
  34. numPlainTextBytes uint64
  35. }
  36. func (n *NumericField) Size() int {
  37. return reflectStaticSizeNumericField + size.SizeOfPtr +
  38. len(n.name) +
  39. len(n.arrayPositions)*size.SizeOfPtr
  40. }
  41. func (n *NumericField) Name() string {
  42. return n.name
  43. }
  44. func (n *NumericField) ArrayPositions() []uint64 {
  45. return n.arrayPositions
  46. }
  47. func (n *NumericField) Options() IndexingOptions {
  48. return n.options
  49. }
  50. func (n *NumericField) 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 := DefaultPrecisionStep
  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 += DefaultPrecisionStep
  76. }
  77. }
  78. fieldLength := len(tokens)
  79. tokenFreqs := analysis.TokenFrequency(tokens, n.arrayPositions, n.options.IncludeTermVectors())
  80. return fieldLength, tokenFreqs
  81. }
  82. func (n *NumericField) Value() []byte {
  83. return n.value
  84. }
  85. func (n *NumericField) Number() (float64, error) {
  86. i64, err := n.value.Int64()
  87. if err != nil {
  88. return 0.0, err
  89. }
  90. return numeric.Int64ToFloat64(i64), nil
  91. }
  92. func (n *NumericField) GoString() string {
  93. return fmt.Sprintf("&document.NumericField{Name:%s, Options: %s, Value: %s}", n.name, n.options, n.value)
  94. }
  95. func (n *NumericField) NumPlainTextBytes() uint64 {
  96. return n.numPlainTextBytes
  97. }
  98. func NewNumericFieldFromBytes(name string, arrayPositions []uint64, value []byte) *NumericField {
  99. return &NumericField{
  100. name: name,
  101. arrayPositions: arrayPositions,
  102. value: value,
  103. options: DefaultNumericIndexingOptions,
  104. numPlainTextBytes: uint64(len(value)),
  105. }
  106. }
  107. func NewNumericField(name string, arrayPositions []uint64, number float64) *NumericField {
  108. return NewNumericFieldWithIndexingOptions(name, arrayPositions, number, DefaultNumericIndexingOptions)
  109. }
  110. func NewNumericFieldWithIndexingOptions(name string, arrayPositions []uint64, number float64, options IndexingOptions) *NumericField {
  111. numberInt64 := numeric.Float64ToInt64(number)
  112. prefixCoded := numeric.MustNewPrefixCodedInt64(numberInt64, 0)
  113. return &NumericField{
  114. name: name,
  115. arrayPositions: arrayPositions,
  116. value: prefixCoded,
  117. options: options,
  118. // not correct, just a place holder until we revisit how fields are
  119. // represented and can fix this better
  120. numPlainTextBytes: uint64(8),
  121. }
  122. }