0

I have data with nested as well as non-nested components. I cannot remap the data, it is in production. Sample is

{
   "name":[
      "first":"Tom",
      "last":"Smith"
   ],
   "status":"married"
}

I need to query within nested as well as non-nested values . Ie, all entries who are not 'married' but whose first names are 'Tom'.

To do a query just for 'Tom', I would do

GET /_search
{
  "query": {
    "nested": {
      "path": "name",
      "query": {
        "bool": {
          "must": [
            { 
              "match" : {"name.first" : "Tom"}}
          ]
        }
      }
    }
  }
}

But how do I combine this with a must_not query on status(which is non nested)

1 Answer 1

1

You need to put a must_not behind the must. I tested this in my environment (except I used term instead of match) and it filtered the unwanted elements.

GET /_search
{
  "query": {
    "nested": {
      "path": "name",
      "query": {
        "bool": {
          "must": [
            { 
              "match" : {"name.first" : "Tom"}}
          ],
          "must_not": [
            { 
              "match" : {"status" : "married"}}
          ]
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.