-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.go
More file actions
102 lines (92 loc) · 3.15 KB
/
token.go
File metadata and controls
102 lines (92 loc) · 3.15 KB
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
package query
import (
"fmt"
"github.com/blevesearch/bleve/v2/search/query"
"sort"
)
// 内置关键字
const (
tokenTypeLeftParenthesis = "(" // (
tokenTypeRightParenthesis = ")" // )
tokenTypeEquals = "=" // LIKE ?
tokenTypeStrongEquals = "==" // =
tokenTypeNotEquals = "!=" // NOT LIKE ?
tokenTypeRegexpEquals = "~=" // REGEXP
tokenTypeRegexpNotEquals = "!~=" // NOT REGEXP ?
tokenTypeWildcardEquals = "*=" // Wildcard
tokenTypeWildcardNotEquals = "!*=" // NOT Wildcard
tokenTypeAND = "&&" // AND
tokenTypeAND1 = "and" // AND
tokenTypeOR = "||" // OR
tokenTypeOR1 = "or" // OR
tokenTypeError = "error" // 语法错误时,显示对应内容
tokenTypeStart = "start" // Token开始
tokenTypeEnd = "end" // Token结束
tokenTypeString = `"` // "
tokenTypeSpace = " " // 空格
tokenTypeQuery = "query" // bleve
)
// 内置关键字
var systemKeywords = []string{}
// 用户输入关键字
var userKeyword = []string{}
var keywordFuncion = map[string]func(str string) string{}
type tokenChain struct {
Type string
Value string
Query query.Query
}
func newToken(t, v string) *tokenChain {
return &tokenChain{Type: t, Value: v}
}
// 添加关键字 -> 按长度进行排序(添加ip和ipx两个关键字,未进行排序,会匹配到ip后就返回token,导致存在一个x字符)
func CustomKeywords(keyword ...string) error {
systemKeywords = []string{
tokenTypeLeftParenthesis, // (
tokenTypeRightParenthesis, // )
tokenTypeEquals, // =
tokenTypeStrongEquals, // ==
tokenTypeNotEquals, // !=
tokenTypeRegexpEquals, // ~=
tokenTypeRegexpNotEquals, // !~=
tokenTypeWildcardEquals, // "*="
tokenTypeWildcardNotEquals, // "!*="
tokenTypeAND, // and
tokenTypeAND1, // &&
tokenTypeOR, // or
tokenTypeOR1, // ||
}
userKeyword = []string{}
for i := 0; i < len(keyword); i++ {
if inArr(systemKeywords, keyword[i]) {
return fmt.Errorf("%s keyword already exists", keyword[i])
}
systemKeywords = append(systemKeywords, keyword[i])
userKeyword = append(userKeyword, keyword[i])
}
sorts := make(map[int][]string)
lens := make([]int, 0)
for i := 0; i < len(systemKeywords); i++ {
if _, ok := sorts[len(systemKeywords[i])]; !ok {
lens = append(lens, len(systemKeywords[i]))
sorts[len(systemKeywords[i])] = make([]string, 0)
}
sorts[len(systemKeywords[i])] = append(sorts[len(systemKeywords[i])], systemKeywords[i])
}
systemKeywords = make([]string, 0)
sort.Ints(lens)
for i := len(lens) - 1; i >= 0; i-- {
sort.Strings(sorts[lens[i]])
for j := 0; j < len(sorts[lens[i]]); j++ {
systemKeywords = append(systemKeywords, sorts[lens[i]][j])
}
}
return nil
}
// 目前仅针对bleve - 不同的Index需要重新设置关键字
func CustomKeywordHookFunction(keyword map[string]func(str string) string) {
keywordFuncion = make(map[string]func(str string) string)
for k, f := range keyword {
keywordFuncion[k] = f
}
}