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.

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