-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.go
134 lines (114 loc) · 2.79 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2016 Evans. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gsebleve
import (
"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/mapping"
)
// Option gse bleve option structure
type Option struct {
Index string
Dicts, Stop, Opt, Trim string
Alpha bool
Name, Sep string
}
// NewMappingSep new separator mapping
func NewMappingSep(sep string, trim ...string) (*mapping.IndexMappingImpl, error) {
mapping1 := bleve.NewIndexMapping()
trimOpt := ""
if len(trim) > 0 {
trimOpt = trim[0]
}
err := mapping1.AddCustomTokenizer(SeparateName, map[string]interface{}{
"type": SeparateName,
"sep": sep,
"trim": trimOpt,
})
if err != nil {
return nil, err
}
err = mapping1.AddCustomAnalyzer(SeparateName, map[string]interface{}{
"type": SeparateName,
"tokenizer": SeparateName,
})
if err != nil {
return nil, err
}
mapping1.DefaultAnalyzer = SeparateName
return mapping1, nil
}
// NewMapping new bleve index mapping
func NewMapping(opt Option) (*mapping.IndexMappingImpl, error) {
mapping1 := bleve.NewIndexMapping()
err := mapping1.AddCustomTokenizer(TokenName, map[string]interface{}{
"type": TokenName,
"dicts": opt.Dicts,
"stop": opt.Stop,
"opt": opt.Opt,
"trim": opt.Trim,
"alpha": opt.Alpha,
})
if err != nil {
return nil, err
}
err = mapping1.AddCustomAnalyzer(TokenName, map[string]interface{}{
"type": TokenName,
"tokenizer": TokenName,
})
if err != nil {
return nil, err
}
mapping1.DefaultAnalyzer = TokenName
return mapping1, nil
}
// New new bleve index
func New(opt Option) (bleve.Index, error) {
var (
mapping1 *mapping.IndexMappingImpl
err error
)
if opt.Name == "sep" {
mapping1, err = NewMappingSep(opt.Sep, opt.Trim)
} else {
mapping1, err = NewMapping(opt)
}
if err != nil {
return nil, err
}
return bleve.New(opt.Index, mapping1)
}
// NewMem new bleve index only memory
func NewMem(opt Option) (bleve.Index, error) {
mapping1, err := NewMapping(opt)
if err != nil {
return nil, err
}
return bleve.NewMemOnly(mapping1)
}
// NewDoc new bleve index document mapping
func NewDoc() *mapping.DocumentMapping {
return bleve.NewDocumentMapping()
}
// NewTextMap new text field mapping with gse
func NewTextMap() *mapping.FieldMapping {
return &mapping.FieldMapping{
Type: "text",
Analyzer: "gse",
Store: true,
Index: true,
IncludeInAll: true,
DocValues: true,
}
}
// NewSepMap new text field mapping with sep
func NewSepMap() *mapping.FieldMapping {
return &mapping.FieldMapping{
Type: "text",
Analyzer: "sep",
Store: true,
Index: true,
IncludeInAll: true,
DocValues: true,
}
}