field_text.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/size"
  20. )
  21. var reflectStaticSizeTextField int
  22. func init() {
  23. var f TextField
  24. reflectStaticSizeTextField = int(reflect.TypeOf(f).Size())
  25. }
  26. const DefaultTextIndexingOptions = IndexField | DocValues
  27. type TextField struct {
  28. name string
  29. arrayPositions []uint64
  30. options IndexingOptions
  31. analyzer *analysis.Analyzer
  32. value []byte
  33. numPlainTextBytes uint64
  34. }
  35. func (t *TextField) Size() int {
  36. return reflectStaticSizeTextField + size.SizeOfPtr +
  37. len(t.name) +
  38. len(t.arrayPositions)*size.SizeOfUint64 +
  39. len(t.value)
  40. }
  41. func (t *TextField) Name() string {
  42. return t.name
  43. }
  44. func (t *TextField) ArrayPositions() []uint64 {
  45. return t.arrayPositions
  46. }
  47. func (t *TextField) Options() IndexingOptions {
  48. return t.options
  49. }
  50. func (t *TextField) Analyze() (int, analysis.TokenFrequencies) {
  51. var tokens analysis.TokenStream
  52. if t.analyzer != nil {
  53. bytesToAnalyze := t.Value()
  54. if t.options.IsStored() {
  55. // need to copy
  56. bytesCopied := make([]byte, len(bytesToAnalyze))
  57. copy(bytesCopied, bytesToAnalyze)
  58. bytesToAnalyze = bytesCopied
  59. }
  60. tokens = t.analyzer.Analyze(bytesToAnalyze)
  61. } else {
  62. tokens = analysis.TokenStream{
  63. &analysis.Token{
  64. Start: 0,
  65. End: len(t.value),
  66. Term: t.value,
  67. Position: 1,
  68. Type: analysis.AlphaNumeric,
  69. },
  70. }
  71. }
  72. fieldLength := len(tokens) // number of tokens in this doc field
  73. tokenFreqs := analysis.TokenFrequency(tokens, t.arrayPositions, t.options.IncludeTermVectors())
  74. return fieldLength, tokenFreqs
  75. }
  76. func (t *TextField) Analyzer() *analysis.Analyzer {
  77. return t.analyzer
  78. }
  79. func (t *TextField) Value() []byte {
  80. return t.value
  81. }
  82. func (t *TextField) GoString() string {
  83. return fmt.Sprintf("&document.TextField{Name:%s, Options: %s, Analyzer: %v, Value: %s, ArrayPositions: %v}", t.name, t.options, t.analyzer, t.value, t.arrayPositions)
  84. }
  85. func (t *TextField) NumPlainTextBytes() uint64 {
  86. return t.numPlainTextBytes
  87. }
  88. func NewTextField(name string, arrayPositions []uint64, value []byte) *TextField {
  89. return NewTextFieldWithIndexingOptions(name, arrayPositions, value, DefaultTextIndexingOptions)
  90. }
  91. func NewTextFieldWithIndexingOptions(name string, arrayPositions []uint64, value []byte, options IndexingOptions) *TextField {
  92. return &TextField{
  93. name: name,
  94. arrayPositions: arrayPositions,
  95. options: options,
  96. value: value,
  97. numPlainTextBytes: uint64(len(value)),
  98. }
  99. }
  100. func NewTextFieldWithAnalyzer(name string, arrayPositions []uint64, value []byte, analyzer *analysis.Analyzer) *TextField {
  101. return &TextField{
  102. name: name,
  103. arrayPositions: arrayPositions,
  104. options: DefaultTextIndexingOptions,
  105. analyzer: analyzer,
  106. value: value,
  107. numPlainTextBytes: uint64(len(value)),
  108. }
  109. }
  110. func NewTextFieldCustom(name string, arrayPositions []uint64, value []byte, options IndexingOptions, analyzer *analysis.Analyzer) *TextField {
  111. return &TextField{
  112. name: name,
  113. arrayPositions: arrayPositions,
  114. options: options,
  115. analyzer: analyzer,
  116. value: value,
  117. numPlainTextBytes: uint64(len(value)),
  118. }
  119. }