parent
b338cb3393
commit
26894f1438
1 changed files with 50 additions and 32 deletions
|
@ -171,49 +171,67 @@ type columnq struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildQuery(query string) (textQuery string, columnQuerys []columnq, trigramQuerys []columnq) {
|
func buildQuery(query string) (textQuery string, columnQuerys []columnq, trigramQuerys []columnq) {
|
||||||
words := strings.Split(query, " ")
|
tokens := extractTokens(query)
|
||||||
for _, w := range words {
|
for _, token := range tokens {
|
||||||
if w == "" {
|
if token == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
tag := strings.SplitN(w, ":", 2)
|
tag := strings.SplitN(token, ":", 2)
|
||||||
if len(tag) > 1 && tag[1] != "" {
|
value := ""
|
||||||
value := strings.Replace(tag[1], "%", "\\%", 0)
|
if len(tag) > 1 {
|
||||||
|
value = strings.Replace(tag[1], "%", "\\%", 0)
|
||||||
value = strings.Replace(value, "_", "\\_", 0)
|
value = strings.Replace(value, "_", "\\_", 0)
|
||||||
switch tag[0] {
|
}
|
||||||
case "lang":
|
|
||||||
columnQuerys = append(columnQuerys, columnq{"lang", value})
|
switch tag[0] {
|
||||||
case "isbn":
|
case "lang":
|
||||||
columnQuerys = append(columnQuerys, columnq{"isbn", value})
|
columnQuerys = append(columnQuerys, columnq{"lang", value})
|
||||||
case "author":
|
case "isbn":
|
||||||
trigramQuerys = append(trigramQuerys, columnq{"text(authors)", value})
|
columnQuerys = append(columnQuerys, columnq{"isbn", value})
|
||||||
case "title":
|
case "author":
|
||||||
trigramQuerys = append(trigramQuerys, columnq{"title", value})
|
trigramQuerys = append(trigramQuerys, columnq{"text(authors)", value})
|
||||||
case "contributor":
|
case "title":
|
||||||
trigramQuerys = append(trigramQuerys, columnq{"contributor", value})
|
trigramQuerys = append(trigramQuerys, columnq{"title", value})
|
||||||
case "publisher":
|
case "contributor":
|
||||||
trigramQuerys = append(trigramQuerys, columnq{"publisher", value})
|
trigramQuerys = append(trigramQuerys, columnq{"contributor", value})
|
||||||
case "subject":
|
case "publisher":
|
||||||
trigramQuerys = append(trigramQuerys, columnq{"text(tags)", value})
|
trigramQuerys = append(trigramQuerys, columnq{"publisher", value})
|
||||||
case "tag":
|
case "subject":
|
||||||
trigramQuerys = append(trigramQuerys, columnq{"text(tags)", value})
|
trigramQuerys = append(trigramQuerys, columnq{"text(tags)", value})
|
||||||
default:
|
case "tag":
|
||||||
if len(textQuery) != 0 {
|
trigramQuerys = append(trigramQuerys, columnq{"text(tags)", value})
|
||||||
textQuery += " | "
|
default:
|
||||||
}
|
|
||||||
textQuery += w
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if len(textQuery) != 0 {
|
if len(textQuery) != 0 {
|
||||||
lastChar := textQuery[len(textQuery)-1:]
|
lastChar := textQuery[len(textQuery)-1:]
|
||||||
if w != "&" && w != "|" && lastChar != "&" && lastChar != "|" {
|
if token[:1] != "&" && token[:1] != "|" && lastChar != "&" && lastChar != "|" {
|
||||||
textQuery += " | "
|
textQuery += " | "
|
||||||
} else {
|
} else {
|
||||||
textQuery += " "
|
textQuery += " "
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
textQuery += w
|
textQuery += strings.Join(strings.Fields(token), " <-> ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func extractTokens(query string) []string {
|
||||||
|
tokens := []string{}
|
||||||
|
quoted := strings.Split(query, "\"")
|
||||||
|
for i, s := range quoted {
|
||||||
|
if i%2 == 0 {
|
||||||
|
tokens = append(tokens, strings.Fields(s)...)
|
||||||
|
} else {
|
||||||
|
// quoted string
|
||||||
|
if len(tokens) > 0 {
|
||||||
|
lastToken := tokens[len(tokens)-1]
|
||||||
|
if len(lastToken) > 0 && lastToken[len(lastToken)-1] == ':' {
|
||||||
|
tokens[len(tokens)-1] += s
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tokens = append(tokens, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tokens
|
||||||
|
}
|
||||||
|
|
Reference in a new issue