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.

184 lines
5.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 registry
  15. import (
  16. "fmt"
  17. "github.com/blevesearch/bleve/analysis"
  18. "github.com/blevesearch/bleve/search/highlight"
  19. )
  20. var stores = make(KVStoreRegistry, 0)
  21. var indexTypes = make(IndexTypeRegistry, 0)
  22. // highlight
  23. var fragmentFormatters = make(FragmentFormatterRegistry, 0)
  24. var fragmenters = make(FragmenterRegistry, 0)
  25. var highlighters = make(HighlighterRegistry, 0)
  26. // analysis
  27. var charFilters = make(CharFilterRegistry, 0)
  28. var tokenizers = make(TokenizerRegistry, 0)
  29. var tokenMaps = make(TokenMapRegistry, 0)
  30. var tokenFilters = make(TokenFilterRegistry, 0)
  31. var analyzers = make(AnalyzerRegistry, 0)
  32. var dateTimeParsers = make(DateTimeParserRegistry, 0)
  33. type Cache struct {
  34. CharFilters *CharFilterCache
  35. Tokenizers *TokenizerCache
  36. TokenMaps *TokenMapCache
  37. TokenFilters *TokenFilterCache
  38. Analyzers *AnalyzerCache
  39. DateTimeParsers *DateTimeParserCache
  40. FragmentFormatters *FragmentFormatterCache
  41. Fragmenters *FragmenterCache
  42. Highlighters *HighlighterCache
  43. }
  44. func NewCache() *Cache {
  45. return &Cache{
  46. CharFilters: NewCharFilterCache(),
  47. Tokenizers: NewTokenizerCache(),
  48. TokenMaps: NewTokenMapCache(),
  49. TokenFilters: NewTokenFilterCache(),
  50. Analyzers: NewAnalyzerCache(),
  51. DateTimeParsers: NewDateTimeParserCache(),
  52. FragmentFormatters: NewFragmentFormatterCache(),
  53. Fragmenters: NewFragmenterCache(),
  54. Highlighters: NewHighlighterCache(),
  55. }
  56. }
  57. func typeFromConfig(config map[string]interface{}) (string, error) {
  58. prop, ok := config["type"]
  59. if !ok {
  60. return "", fmt.Errorf("'type' property is not defined")
  61. }
  62. typ, ok := prop.(string)
  63. if !ok {
  64. return "", fmt.Errorf("'type' property must be a string, not %T", prop)
  65. }
  66. return typ, nil
  67. }
  68. func (c *Cache) CharFilterNamed(name string) (analysis.CharFilter, error) {
  69. return c.CharFilters.CharFilterNamed(name, c)
  70. }
  71. func (c *Cache) DefineCharFilter(name string, config map[string]interface{}) (analysis.CharFilter, error) {
  72. typ, err := typeFromConfig(config)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return c.CharFilters.DefineCharFilter(name, typ, config, c)
  77. }
  78. func (c *Cache) TokenizerNamed(name string) (analysis.Tokenizer, error) {
  79. return c.Tokenizers.TokenizerNamed(name, c)
  80. }
  81. func (c *Cache) DefineTokenizer(name string, config map[string]interface{}) (analysis.Tokenizer, error) {
  82. typ, err := typeFromConfig(config)
  83. if err != nil {
  84. return nil, fmt.Errorf("cannot resolve '%s' tokenizer type: %s", name, err)
  85. }
  86. return c.Tokenizers.DefineTokenizer(name, typ, config, c)
  87. }
  88. func (c *Cache) TokenMapNamed(name string) (analysis.TokenMap, error) {
  89. return c.TokenMaps.TokenMapNamed(name, c)
  90. }
  91. func (c *Cache) DefineTokenMap(name string, config map[string]interface{}) (analysis.TokenMap, error) {
  92. typ, err := typeFromConfig(config)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return c.TokenMaps.DefineTokenMap(name, typ, config, c)
  97. }
  98. func (c *Cache) TokenFilterNamed(name string) (analysis.TokenFilter, error) {
  99. return c.TokenFilters.TokenFilterNamed(name, c)
  100. }
  101. func (c *Cache) DefineTokenFilter(name string, config map[string]interface{}) (analysis.TokenFilter, error) {
  102. typ, err := typeFromConfig(config)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return c.TokenFilters.DefineTokenFilter(name, typ, config, c)
  107. }
  108. func (c *Cache) AnalyzerNamed(name string) (*analysis.Analyzer, error) {
  109. return c.Analyzers.AnalyzerNamed(name, c)
  110. }
  111. func (c *Cache) DefineAnalyzer(name string, config map[string]interface{}) (*analysis.Analyzer, error) {
  112. typ, err := typeFromConfig(config)
  113. if err != nil {
  114. return nil, err
  115. }
  116. return c.Analyzers.DefineAnalyzer(name, typ, config, c)
  117. }
  118. func (c *Cache) DateTimeParserNamed(name string) (analysis.DateTimeParser, error) {
  119. return c.DateTimeParsers.DateTimeParserNamed(name, c)
  120. }
  121. func (c *Cache) DefineDateTimeParser(name string, config map[string]interface{}) (analysis.DateTimeParser, error) {
  122. typ, err := typeFromConfig(config)
  123. if err != nil {
  124. return nil, err
  125. }
  126. return c.DateTimeParsers.DefineDateTimeParser(name, typ, config, c)
  127. }
  128. func (c *Cache) FragmentFormatterNamed(name string) (highlight.FragmentFormatter, error) {
  129. return c.FragmentFormatters.FragmentFormatterNamed(name, c)
  130. }
  131. func (c *Cache) DefineFragmentFormatter(name string, config map[string]interface{}) (highlight.FragmentFormatter, error) {
  132. typ, err := typeFromConfig(config)
  133. if err != nil {
  134. return nil, err
  135. }
  136. return c.FragmentFormatters.DefineFragmentFormatter(name, typ, config, c)
  137. }
  138. func (c *Cache) FragmenterNamed(name string) (highlight.Fragmenter, error) {
  139. return c.Fragmenters.FragmenterNamed(name, c)
  140. }
  141. func (c *Cache) DefineFragmenter(name string, config map[string]interface{}) (highlight.Fragmenter, error) {
  142. typ, err := typeFromConfig(config)
  143. if err != nil {
  144. return nil, err
  145. }
  146. return c.Fragmenters.DefineFragmenter(name, typ, config, c)
  147. }
  148. func (c *Cache) HighlighterNamed(name string) (highlight.Highlighter, error) {
  149. return c.Highlighters.HighlighterNamed(name, c)
  150. }
  151. func (c *Cache) DefineHighlighter(name string, config map[string]interface{}) (highlight.Highlighter, error) {
  152. typ, err := typeFromConfig(config)
  153. if err != nil {
  154. return nil, err
  155. }
  156. return c.Highlighters.DefineHighlighter(name, typ, config, c)
  157. }