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.9 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 mapping
  15. type customAnalysis struct {
  16. CharFilters map[string]map[string]interface{} `json:"char_filters,omitempty"`
  17. Tokenizers map[string]map[string]interface{} `json:"tokenizers,omitempty"`
  18. TokenMaps map[string]map[string]interface{} `json:"token_maps,omitempty"`
  19. TokenFilters map[string]map[string]interface{} `json:"token_filters,omitempty"`
  20. Analyzers map[string]map[string]interface{} `json:"analyzers,omitempty"`
  21. DateTimeParsers map[string]map[string]interface{} `json:"date_time_parsers,omitempty"`
  22. }
  23. func (c *customAnalysis) registerAll(i *IndexMappingImpl) error {
  24. for name, config := range c.CharFilters {
  25. _, err := i.cache.DefineCharFilter(name, config)
  26. if err != nil {
  27. return err
  28. }
  29. }
  30. if len(c.Tokenizers) > 0 {
  31. // put all the names in map tracking work to do
  32. todo := map[string]struct{}{}
  33. for name := range c.Tokenizers {
  34. todo[name] = struct{}{}
  35. }
  36. registered := 1
  37. errs := []error{}
  38. // as long as we keep making progress, keep going
  39. for len(todo) > 0 && registered > 0 {
  40. registered = 0
  41. errs = []error{}
  42. for name := range todo {
  43. config := c.Tokenizers[name]
  44. _, err := i.cache.DefineTokenizer(name, config)
  45. if err != nil {
  46. errs = append(errs, err)
  47. } else {
  48. delete(todo, name)
  49. registered++
  50. }
  51. }
  52. }
  53. if len(errs) > 0 {
  54. return errs[0]
  55. }
  56. }
  57. for name, config := range c.TokenMaps {
  58. _, err := i.cache.DefineTokenMap(name, config)
  59. if err != nil {
  60. return err
  61. }
  62. }
  63. for name, config := range c.TokenFilters {
  64. _, err := i.cache.DefineTokenFilter(name, config)
  65. if err != nil {
  66. return err
  67. }
  68. }
  69. for name, config := range c.Analyzers {
  70. _, err := i.cache.DefineAnalyzer(name, config)
  71. if err != nil {
  72. return err
  73. }
  74. }
  75. for name, config := range c.DateTimeParsers {
  76. _, err := i.cache.DefineDateTimeParser(name, config)
  77. if err != nil {
  78. return err
  79. }
  80. }
  81. return nil
  82. }
  83. func newCustomAnalysis() *customAnalysis {
  84. rv := customAnalysis{
  85. CharFilters: make(map[string]map[string]interface{}),
  86. Tokenizers: make(map[string]map[string]interface{}),
  87. TokenMaps: make(map[string]map[string]interface{}),
  88. TokenFilters: make(map[string]map[string]interface{}),
  89. Analyzers: make(map[string]map[string]interface{}),
  90. DateTimeParsers: make(map[string]map[string]interface{}),
  91. }
  92. return &rv
  93. }