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.

124 lines
3.3 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. "reflect"
  17. "github.com/blevesearch/bleve/analysis"
  18. "github.com/blevesearch/bleve/size"
  19. )
  20. var reflectStaticSizeCompositeField int
  21. func init() {
  22. var cf CompositeField
  23. reflectStaticSizeCompositeField = int(reflect.TypeOf(cf).Size())
  24. }
  25. const DefaultCompositeIndexingOptions = IndexField
  26. type CompositeField struct {
  27. name string
  28. includedFields map[string]bool
  29. excludedFields map[string]bool
  30. defaultInclude bool
  31. options IndexingOptions
  32. totalLength int
  33. compositeFrequencies analysis.TokenFrequencies
  34. }
  35. func NewCompositeField(name string, defaultInclude bool, include []string, exclude []string) *CompositeField {
  36. return NewCompositeFieldWithIndexingOptions(name, defaultInclude, include, exclude, DefaultCompositeIndexingOptions)
  37. }
  38. func NewCompositeFieldWithIndexingOptions(name string, defaultInclude bool, include []string, exclude []string, options IndexingOptions) *CompositeField {
  39. rv := &CompositeField{
  40. name: name,
  41. options: options,
  42. defaultInclude: defaultInclude,
  43. includedFields: make(map[string]bool, len(include)),
  44. excludedFields: make(map[string]bool, len(exclude)),
  45. compositeFrequencies: make(analysis.TokenFrequencies),
  46. }
  47. for _, i := range include {
  48. rv.includedFields[i] = true
  49. }
  50. for _, e := range exclude {
  51. rv.excludedFields[e] = true
  52. }
  53. return rv
  54. }
  55. func (c *CompositeField) Size() int {
  56. sizeInBytes := reflectStaticSizeCompositeField + size.SizeOfPtr +
  57. len(c.name)
  58. for k, _ := range c.includedFields {
  59. sizeInBytes += size.SizeOfString + len(k) + size.SizeOfBool
  60. }
  61. for k, _ := range c.excludedFields {
  62. sizeInBytes += size.SizeOfString + len(k) + size.SizeOfBool
  63. }
  64. return sizeInBytes
  65. }
  66. func (c *CompositeField) Name() string {
  67. return c.name
  68. }
  69. func (c *CompositeField) ArrayPositions() []uint64 {
  70. return []uint64{}
  71. }
  72. func (c *CompositeField) Options() IndexingOptions {
  73. return c.options
  74. }
  75. func (c *CompositeField) Analyze() (int, analysis.TokenFrequencies) {
  76. return c.totalLength, c.compositeFrequencies
  77. }
  78. func (c *CompositeField) Value() []byte {
  79. return []byte{}
  80. }
  81. func (c *CompositeField) NumPlainTextBytes() uint64 {
  82. return 0
  83. }
  84. func (c *CompositeField) includesField(field string) bool {
  85. shouldInclude := c.defaultInclude
  86. _, fieldShouldBeIncluded := c.includedFields[field]
  87. if fieldShouldBeIncluded {
  88. shouldInclude = true
  89. }
  90. _, fieldShouldBeExcluded := c.excludedFields[field]
  91. if fieldShouldBeExcluded {
  92. shouldInclude = false
  93. }
  94. return shouldInclude
  95. }
  96. func (c *CompositeField) Compose(field string, length int, freq analysis.TokenFrequencies) {
  97. if c.includesField(field) {
  98. c.totalLength += length
  99. c.compositeFrequencies.MergeAll(field, freq)
  100. }
  101. }