4

在简要阅读了弹性搜索教程之后,我正在尝试使以下查询工作一段时间:

{
"query" :
{ 
    "size" : "10",
    "query" : {
        "bool" : {
            "must" : {
                "custom_score" : {
                    "query" : {
                        "text" : { "phrase" : {"query" : "iphone4", "type" : "phrase_prefix"}}
                    },
                    "params" : {
                            "param1" : 1000
                     },
                    "script" : "_score * param1 "
                }
            },
            "should" : [{ 
                "custom_score" : {
                    "query" : { "match_all" : {}},
                    "params" : {
                            "param2" : 0.001
                     },
                    "script" : "_score * doc['matches'].value * param2 "
                }
            },{
                "custom_score" : {
                    "query" : {"match_all" : {}},
                    "params" : {
                            "param3" : 0.001
                     },
                    "script" : "_score * doc['hits'].value * param3 "
                }
            }]

        }
    }
}
}

当我尝试运行它时,我收到以下错误:

{
    "error": "SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[jJz-l-ENSV-0inWp2Sf8Bw][searches][2]: SearchParseException[[searches][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\n\"query\" :\n{ \n \t\"size\" : \"10\",\n \t\"query\" : {\n\t\t\"bool\" : {\n\t\t\t\"must\" : {\n\t\t\t\t\"custom_score\" : {\n\t\t\t\t\t\"query\" : {\n\t\t\t\t\t\t\"text\" : { \"phrase\" : {\"query\" : \"iphone4\", \"type\" : \"phrase_prefix\"}}\n\t\t\t\t\t},\n\t\t\t\t\t\"params\" : {\n        \t\t\t\t\t\"param1\" : 1000\n   \t\t\t\t\t },\n\t\t\t\t\t\"script\" : \"_score * param1 \"\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"should\" : [{ \n\t\t\t\t\"custom_score\" : {\n\t\t\t\t\t\"query\" : { \"match_all\" : {}},\n\t\t\t\t\t\"params\" : {\n        \t\t\t\t\t\"param2\" : 0.001\n   \t\t\t\t\t },\n\t\t\t\t\t\"script\" : \"_score * doc['matches'].value * param2 \"\n\t\t\t\t}\n\t\t\t},{\n\t\t\t\t\"custom_score\" : {\n\t\t\t\t\t\"query\" : {\"match_all\" : {}},\n\t\t\t\t\t\"params\" : {\n        \t\t\t\t\t\"param3\" : 0.001\n   \t\t\t\t\t },\n\t\t\t\t\t\"script\" : \"_score * doc['hits'].value * param3 \"\n\t\t\t\t}\n\t\t\t}]\n\t     \t\n\t\t}\n\t}\n}\n}]]]; nested: QueryParsingException[[searches] [_na] query malformed, no field after start_object]; }{[jJz-l-ENSV-0inWp2Sf8Bw][searches][4]: SearchParseException[[searches][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\n\"query\" :\n{ \n \t\"size\" : \"10\",\n \t\"query\" : {\n\t\t\"bool\" : {\n\t\t\t\"must\" : {\n\t\t\t\t\"custom_score\" : {\n\t\t\t\t\t\"query\" : {\n\t\t\t\t\t\t\"text\" : { \"phrase\" : {\"query\" : \"iphone4\", \"type\" : \"phrase_prefix\"}}\n\t\t\t\t\t},\n\t\t\t\t\t\"params\" : {\n        \t\t\t\t\t\"param1\" : 1000\n   \t\t\t\t\t },\n\t\t\t\t\t\"script\" : \"_score * param1 \"\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"should\" : [{ \n\t\t\t\t\"custom_score\" : {\n\t\t\t\t\t\"query\" : { \"match_all\" : {}},\n\t\t\t\t\t\"params\" : {\n        \t\t\t\t\t\"param2\" : 0.001\n   \t\t\t\t\t },\n\t\t\t\t\t\"script\" : \"_score * doc['matches'].value * param2 \"\n\t\t\t\t}\n\t\t\t},{\n\t\t\t\t\"custom_score\" : {\n\t\t\t\t\t\"query\" : {\"match_all\" : {}},\n\t\t\t\t\t\"params\" : {\n        \t\t\t\t\t\"param3\" : 0.001\n   \t\t\t\t\t },\n\t\t\t\t\t\"script\" : \"_score * doc['hits'].value * param3 \"\n\t\t\t\t}\n\t\t\t}]\n\t     \t\n\t\t}\n\t}\n}\n}]]]; nested: QueryParsingException[[searches] [_na] query malformed, no field after start_object]; }]",
    "status": 500
}

你觉得我的错在哪里?关于我应该如何进行的任何建议?

4

1 回答 1

2

您需要将嵌套查询与外部查询“合并”。试试这个:

{
    "query" : { 
        "bool" : {
            "must" : {
                "custom_score" : {
                    "query" : {
                        "text" : { "phrase" : {"query" : "iphone4", "type" : "phrase_prefix"}}
                    },
                    "params" : {
                            "param1" : 1000
                     },
                    "script" : "_score * param1 "
                }
            },
            "should" : [{ 
                "custom_score" : {
                    "query" : { "match_all" : {}},
                    "params" : {
                            "param2" : 0.001
                     },
                    "script" : "_score * doc['matches'].value * param2 "
                }
            },{
                "custom_score" : {
                    "query" : {"match_all" : {}},
                    "params" : {
                            "param3" : 0.001
                     },
                    "script" : "_score * doc['hits'].value * param3 "
                }
            }]

        }
    },
    "size" : "10"
}

如果您使用 curl 运行它,您可能还需要替换''\''

于 2012-10-16T14:08:54.057 回答