diff --git a/select_handler.go b/select_handler.go index 4ed18b9..00370c8 100644 --- a/select_handler.go +++ b/select_handler.go @@ -291,7 +291,7 @@ func handleSelectWhere(expr *sqlparser.Expr, topLevel bool, parent *sqlparser.Ex return "", errors.New("elasticsql: error expression cannot be nil here") } - switch (*expr).(type) { + switch e := (*expr).(type) { case *sqlparser.AndExpr: return handleSelectWhereAndExpr(expr, topLevel, parent) @@ -336,39 +336,44 @@ func handleSelectWhere(expr *sqlparser.Expr, topLevel bool, parent *sqlparser.Ex case *sqlparser.NotExpr: return "", errors.New("elasticsql: not expression currently not supported") case *sqlparser.FuncExpr: - params := (*expr).(*sqlparser.FuncExpr).Exprs - if len(params) > 3 || len(params) < 2 { - return "", errors.New("elasticsql: the multi_match must have 2 or 3 params, (query, fields and type) or (query, fields)") - } - - var typ, query, fields string - for i := 0; i < len(params); i++ { - elem := strings.Replace(sqlparser.String(params[i]), "`", "", -1) // a = b - kv := strings.Split(elem, "=") - if len(kv) != 2 { - return "", errors.New("elasticsql: the param should be query = xxx, field = yyy, type = zzz") + switch e.Name.Lowered() { + case "multi_match": + params := e.Exprs + if len(params) > 3 || len(params) < 2 { + return "", errors.New("elasticsql: the multi_match must have 2 or 3 params, (query, fields and type) or (query, fields)") } - k, v := strings.TrimSpace(kv[0]), strings.TrimSpace(kv[1]) - switch k { - case "type": - typ = strings.Replace(v, "'", "", -1) - case "query": - query = strings.Replace(v, "`", "", -1) - query = strings.Replace(query, "'", "", -1) - case "fields": - fieldList := strings.Split(strings.TrimRight(strings.TrimLeft(v, "("), ")"), ",") - for idx, field := range fieldList { - fieldList[idx] = fmt.Sprintf(`"%v"`, strings.TrimSpace(field)) + + var typ, query, fields string + for i := 0; i < len(params); i++ { + elem := strings.Replace(sqlparser.String(params[i]), "`", "", -1) // a = b + kv := strings.Split(elem, "=") + if len(kv) != 2 { + return "", errors.New("elasticsql: the param should be query = xxx, field = yyy, type = zzz") + } + k, v := strings.TrimSpace(kv[0]), strings.TrimSpace(kv[1]) + switch k { + case "type": + typ = strings.Replace(v, "'", "", -1) + case "query": + query = strings.Replace(v, "`", "", -1) + query = strings.Replace(query, "'", "", -1) + case "fields": + fieldList := strings.Split(strings.TrimRight(strings.TrimLeft(v, "("), ")"), ",") + for idx, field := range fieldList { + fieldList[idx] = fmt.Sprintf(`"%v"`, strings.TrimSpace(field)) + } + fields = strings.Join(fieldList, ",") + default: + return "", errors.New("elaticsql: unknow param for multi_match") } - fields = strings.Join(fieldList, ",") - default: - return "", errors.New("elaticsql: unknow param for multi_match") } + if typ == "" { + return fmt.Sprintf(`{"multi_match" : {"query" : "%v", "fields" : [%v]}}`, query, fields), nil + } + return fmt.Sprintf(`{"multi_match" : {"query" : "%v", "type" : "%v", "fields" : [%v]}}`, query, typ, fields), nil + default: + return "", errors.New("elaticsql: function in where not supported" + e.Name.Lowered()) } - if typ == "" { - return fmt.Sprintf(`{"multi_match" : {"query" : "%v", "fields" : [%v]}}`, query, fields), nil - } - return fmt.Sprintf(`{"multi_match" : {"query" : "%v", "type" : "%v", "fields" : [%v]}}`, query, typ, fields), nil } return "", errors.New("elaticsql: logically cannot reached here")