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.

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