1

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}

1 Answer 1

4

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

    }'
Sign up to request clarification or add additional context in comments.

2 Comments

plz,explain me what changes i need to do.
you just have a single line to change. See my updated answer. "term": { "country" : "pakistan" } instead of just "country" : "pakistan"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.