弹性搜索的布尔查询

时间:2016-05-16 14:26:55

标签: elasticsearch

我在弹性搜索中使用布尔查询。我的疑问是

curl -XGET 'localhost:9200/population/_search' -d '{
   "query":{
        "bool" : {
            "must" : {
                "term" : { "user" : "rahul" }
            },
            "filter": {
                "term" : { "message" : "dsi" }
            },
            "must_not" : {

                    "country" : "pakistan"

            },
            "should" : [
                {
                    "term" : { "country" : "india" }
                },
                {
                    "term" : { "state" : "karnataka" }
                }
            ],

            "minimum_should_match" : 1,
            "boost" : 1.0
        }}

    }'

用户,州,国家/地区在我的弹性搜索中是字段。 但是收到错误。

    {"error":{"root_cause":[{"type":"query_parsing_exception","reason":"[_na] query malformed, no field after start_object","index":"population","line":13,"col":17}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"population","node":"bECY7K9ORPSuLrXpL1DpDw","reason":{"type":"query_parsing_exception","reason":"[_na] query malformed, no field after start_object","index":"population","line":13,"col":17}}]},"status":400}

1 个答案:

答案 0 :(得分:4)

您的bool/must_not条款不正确,您错过了term查询

curl -XGET 'localhost:9200/population/_search' -d '{
   "query":{
        "bool" : {
            "must" : {
                "term" : { "user" : "rahul" }
            },
            "filter": {
                "term" : { "message" : "dsi" }
            },
            "must_not" : {
                "term": { "country" : "pakistan" }      <--- change this line
            },
            "should" : [
                {
                    "term" : { "country" : "india" }
                },
                {
                    "term" : { "state" : "karnataka" }
                }
            ],

            "minimum_should_match" : 1,
            "boost" : 1.0
        }}

    }'