0

My elastic search index having nested fields. and I want to use must, which contains a must_not query in it with a nested query. I have tried a must_not query separately in the following way:

{
    "bool": {
        "must_not": [{
            "nested": {
                "path": "fields",
                "query": {
                    "terms": {
                        "fields.value.raw": [
                            "200"
                        ]
                    }
                }
            }
        }]
    }
}

above query gives me a valid result but when I was tried this with must query then it will not give me any result.I am using following query:

{
        "bool": {
            "must": [{
                "nested": {
                    "path": "fields",
                    "query": {
                        "bool": {
                            "must": [{
                                "match": {
                                    "fields.uid": "number"
                                }
                            }, {
                                "bool": {
                                    "must_not": [{
                                        "nested": {
                                            "path": "fields",
                                            "query": {
                                                "terms": {
                                                    "fields.value.raw": [
                                                        "200"
                                                    ]
                                                }
                                            }
                                        }
                                    }]
                                }
                            }]
                        }
                    }
                }
            }]
        }
    }

above query not gives me a valid result. What is wrong in above query? How I can use a must_not in must with nested query?

2
  • It would help if you wrote a sentence or two describing what you want the query to do. For example, I want all documents where price equals 10. Commented Oct 9, 2018 at 18:54
  • my nested schema as follows: { "fields": { "type": "nested", "properties": { "uid": { "type": "keyword" }, "value": { "type": "text", "fields": { "raw": { "type": "keyword" } } } } } } so I want documents having fields.uid is the number and the value of number field(fields.value) does not contain "200 " so need to add must_not in must, How I can get the valid result? Commented Oct 10, 2018 at 12:29

1 Answer 1

3

You should use must and must_not in the same bool query.

{
    "bool": {
        "must_not": [{
            "nested": {
                "path": "fields",
                "query": {
                    "terms": {
                        "fields.value.raw": [
                            "200"
                        ]
                    }
                }
            }
        }],
        "must": [{
            "match": {
                "fields.uid": "number"
            }
        }]
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

above query not giving me a valid response I want to check and query with the following condition for nested objects: documents having fields.uid is number and the number does not contain "200 " so need to add must_not in must, How I can get the valid result?
this is the only things worked for me, thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.