package postgres import "strings" // TSQueryFromSearch generates a TS query from a typical search string. func TSQueryFromSearch(search string) string { if strings.HasPrefix(search, "tsq:") { return search[4:] } tokens := strings.Split(search, " ") inQuotes := false result := "" for i, token := range tokens { clearQuotes := false startQuotes := false if strings.HasPrefix(token, "\"") { if len(token) > 1 { token = token[1:] } startQuotes = true } if strings.HasSuffix(token, "\"") { token = token[:len(token)-1] startQuotes = false clearQuotes = true } token := strings.Trim(token, ",.;:()\"") if len(token) > 0 { if startQuotes { if i > 0 { result += " & " } result += "(" } else if inQuotes { result += "<->" } else if i != 0 { result += " & " } result += token } if startQuotes { inQuotes = true } if clearQuotes { inQuotes = false result += ")" } } if inQuotes { return result } return strings.TrimLeft(result, " ") }