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