Using must and must_not on same field

Hi All,

I have a requirement like, need to extract documents based on two fields and out of those two fields i need to exclude one field(optional), and extract data. So for this requirement below is my ES query.

{
"query": {
    "bool": {
        "must": [
            {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "keys": {
                                    "value": "216340000",
                                    "boost": 1
                                }
                            }
                        },
                        {
                            "term": {
                                "code": {
                                    "value": 1067,
                                    "boost": 1
                                }
                            }
                        }
                    ],
                    "adjust_pure_negative": true,
                    "boost": 1
                }
            },
            {
                "bool": {
                    "must_not": [
                        {
                            "term": {
                                "keys": {
                                    "value": "216340000",
                                    "boost": 1
                                }
                            }
                        }
                    ],
                    "adjust_pure_negative": true,
                    "boost": 1
                }
            }
        ],
        "adjust_pure_negative": true,
        "boost": 1
    }
}

}

When I am executing above query, I am getting zero count. But I am suppose to get some count like 20+ count.

Could someone suggest me on my query whether I have written write or I did any mistake in query.

Thanks,
Ram Prasad G

I'm not sure I fully understand the query. You want the field keys to have the value 216340000 and also to not have that value? That seems contradictory unless I misread the query.

Can you show a sample document that should match this?

Its a kind of contradictory, but not as per the requirement. User will be having two different kinds of criterias, one is match criteria and other is exclude criteria. So, there is a chance of selecting same field in both the criteria's so, the above query i have written. Thats the reason I have given complete query with match query with two fields and must_not with one field.

Please let me know if we can do this in ES.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.