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, "\"") { token = token[1:] startQuotes = true } if strings.HasSuffix(token, "\"") { token = token[:len(token)-1] clearQuotes = true } 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, " ") }