/
tsquery.go
178 lines (168 loc) · 3.98 KB
/
tsquery.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package fts
import (
"strings"
)
func AppQueryToTsquery(str string) string {
l := queryLexer{newLexer(str)}
var tokens []TokenValue
for {
token, ok := l.readQueryToken()
if !ok {
break
}
tokens = append(tokens, token)
}
return appQueryTokensToTsquery(tokens...)
}
type queryLexer struct {
lexer
}
type Token string
const (
TokenQuoted Token = "QUOTED"
TokenOpenParens Token = "OPEN_PARENS"
TokenCloseParens Token = "CLOSE_PARENS"
TokenOperator Token = "OPERATOR"
TokenNegation Token = "NEGATION"
TokenPhrase Token = "PHRASE"
TokenWildcard Token = "WILDCARD"
)
type TokenValue struct {
StartPos int
Length int
Token Token
Value string
}
type Operator string
const (
OperatorAnd Operator = "+"
OperatorOr Operator = "|"
OperatorFollowedBy Operator = "."
)
func (l *queryLexer) readQueryToken() (TokenValue, bool) {
for {
if l.isEof() {
return TokenValue{}, false
}
start := l.pos
if l.readChar('(') {
return TokenValue{l.pos, 1, TokenOpenParens, "("}, true
}
if l.readChar(')') {
return TokenValue{l.pos, 1, TokenCloseParens, ")"}, true
}
if l.readChar('&') {
return TokenValue{l.pos, 1, TokenOperator, string(OperatorAnd)}, true
}
if l.readChar('|') {
return TokenValue{l.pos, 1, TokenOperator, string(OperatorOr)}, true
}
if l.readChar('.') {
return TokenValue{l.pos, 1, TokenOperator, string(OperatorFollowedBy)}, true
}
if l.readChar('!') {
return TokenValue{l.pos, 1, TokenNegation, "-"}, true
}
if l.readChar('*') {
return TokenValue{l.pos, 1, TokenWildcard, "*"}, true
}
if quoted, _ := l.readQuotedString('"'); quoted != "" {
return TokenValue{start, l.pos - start, TokenQuoted, quoted}, true
}
if phrase := l.readWhile(IsWordChar); phrase != "" {
return TokenValue{start, l.pos - start, TokenPhrase, phrase}, true
}
l.read()
}
}
func appQueryTokensToTsquery(tokens ...TokenValue) string {
var parts []string
i := 0
outer:
for {
var operator Operator
var negated bool
for {
if i >= len(tokens) {
break outer
}
token := tokens[i]
addExpr := func(expr string) {
switch operator {
case OperatorAnd:
parts = append(parts, "&")
case OperatorOr:
parts = append(parts, "|")
case OperatorFollowedBy:
parts = append(parts, "<->")
default:
if len(parts) > 0 {
parts = append(parts, "&")
}
}
if negated {
parts = append(parts, "!")
}
if len(tokens) > i+1 && tokens[i+1].Token == TokenWildcard {
expr += ":*"
i++
}
parts = append(parts, expr)
operator = ""
negated = false
}
switch token.Token {
case TokenOperator:
operator = Operator(token.Value)
case TokenNegation:
negated = true
case TokenQuoted:
tokenized := TokenizeFlat(token.Value)
var quotedWords []string
for _, word := range tokenized {
quotedWords = append(quotedWords, quoteLexeme(word, false))
}
if len(quotedWords) > 0 {
addExpr(strings.Join(quotedWords, " <-> "))
}
case TokenPhrase:
tokenized := Tokenize(token.Value)
var phrases []string
for _, phrase := range tokenized {
var quotedWords []string
for _, word := range phrase {
quotedWords = append(quotedWords, quoteLexeme(word, false))
}
if len(quotedWords) > 0 {
phrases = append(phrases, strings.Join(quotedWords, " <-> "))
}
}
if len(phrases) > 0 {
addExpr(strings.Join(phrases, " & "))
}
case TokenOpenParens:
var parensTokens []TokenValue
depth := 1
for j := i + 1; j < len(tokens); j++ {
if tokens[j].Token == TokenOpenParens {
depth++
}
if tokens[j].Token == TokenCloseParens {
depth--
if depth == 0 {
break
}
}
parensTokens = append(parensTokens, tokens[j])
}
i += len(parensTokens)
parensExpr := appQueryTokensToTsquery(parensTokens...)
if len(parensExpr) > 0 {
addExpr("(" + parensExpr + ")")
}
}
i++
}
}
return strings.Join(parts, " ")
}