1
// Copyright (c) 2014 Couchbase, Inc.
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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.
20
"github.com/blevesearch/bleve/analysis"
23
func RegisterTokenFilter(name string, constructor TokenFilterConstructor) {
24
_, exists := tokenFilters[name]
26
panic(fmt.Errorf("attempted to register duplicate token filter named '%s'", name))
28
tokenFilters[name] = constructor
31
type TokenFilterConstructor func(config map[string]interface{}, cache *Cache) (analysis.TokenFilter, error)
32
type TokenFilterRegistry map[string]TokenFilterConstructor
34
type TokenFilterCache struct {
38
func NewTokenFilterCache() *TokenFilterCache {
39
return &TokenFilterCache{
44
func TokenFilterBuild(name string, config map[string]interface{}, cache *Cache) (interface{}, error) {
45
cons, registered := tokenFilters[name]
47
return nil, fmt.Errorf("no token filter with name or type '%s' registered", name)
49
tokenFilter, err := cons(config, cache)
51
return nil, fmt.Errorf("error building token filter: %v", err)
53
return tokenFilter, nil
56
func (c *TokenFilterCache) TokenFilterNamed(name string, cache *Cache) (analysis.TokenFilter, error) {
57
item, err := c.ItemNamed(name, cache, TokenFilterBuild)
61
return item.(analysis.TokenFilter), nil
64
func (c *TokenFilterCache) DefineTokenFilter(name string, typ string, config map[string]interface{}, cache *Cache) (analysis.TokenFilter, error) {
65
item, err := c.DefineItem(name, typ, config, cache, TokenFilterBuild)
67
if err == ErrAlreadyDefined {
68
return nil, fmt.Errorf("token filter named '%s' already defined", name)
72
return item.(analysis.TokenFilter), nil
75
func TokenFilterTypesAndInstances() ([]string, []string) {
76
emptyConfig := map[string]interface{}{}
77
emptyCache := NewCache()
79
var instances []string
80
for name, cons := range tokenFilters {
81
_, err := cons(emptyConfig, emptyCache)
83
instances = append(instances, name)
85
types = append(types, name)
88
return types, instances