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.

135 lines
3.7 KiB

  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) Value() []byte {
  77. return t.value
  78. }
  79. func (t *TextField) GoString() string {
  80. 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)
  81. }
  82. func (t *TextField) NumPlainTextBytes() uint64 {
  83. return t.numPlainTextBytes
  84. }
  85. func NewTextField(name string, arrayPositions []uint64, value []byte) *TextField {
  86. return NewTextFieldWithIndexingOptions(name, arrayPositions, value, DefaultTextIndexingOptions)
  87. }
  88. func NewTextFieldWithIndexingOptions(name string, arrayPositions []uint64, value []byte, options IndexingOptions) *TextField {
  89. return &TextField{
  90. name: name,
  91. arrayPositions: arrayPositions,
  92. options: options,
  93. value: value,
  94. numPlainTextBytes: uint64(len(value)),
  95. }
  96. }
  97. func NewTextFieldWithAnalyzer(name string, arrayPositions []uint64, value []byte, analyzer *analysis.Analyzer) *TextField {
  98. return &TextField{
  99. name: name,
  100. arrayPositions: arrayPositions,
  101. options: DefaultTextIndexingOptions,
  102. analyzer: analyzer,
  103. value: value,
  104. numPlainTextBytes: uint64(len(value)),
  105. }
  106. }
  107. func NewTextFieldCustom(name string, arrayPositions []uint64, value []byte, options IndexingOptions, analyzer *analysis.Analyzer) *TextField {
  108. return &TextField{
  109. name: name,
  110. arrayPositions: arrayPositions,
  111. options: options,
  112. analyzer: analyzer,
  113. value: value,
  114. numPlainTextBytes: uint64(len(value)),
  115. }
  116. }