简体   繁体   中英

Boolean query on elastic search

I am using boolean queries in elastic search. My queries is

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
        }}

    }'

where user ,state,country are field in my elastic search. but getting error.

    {"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}

Your bool/must_not clause is not correct, you're missing a term query

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
        }}

    }'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM