0

I have nested data in some documents, in some it is not. Suppose below is the mapping.

{
"mappings": {
         "item": {
            "properties": {
               "id": {
                  "type": "string"
               },
               "nestedType": {
                  "type": "nested",
                  "properties": {
                     "item1": {
                        "type": "long"
                     },
                     "item2": {
                        "type": "string"
                     }
                    }
                }
            }
         }
    }
}

I want to query on id basis, and want nested element to include in my response where item1 = 1234. But i do not want to filter response. If item1 != 1234 or item1 doesnot exist.

Actually, I don't want nested query to effect my Hits result. but to include inner hits if match found other no result in inner hits.

3
  • So you want to filter on id and you want the nested element to be present in the response only if nestedType.item1 = 1234? But if nestedType.item1 is not present or has a different value than 1234, you don't want the nested element to be present in the response? Commented Feb 1, 2016 at 4:50
  • Actualy, I don't want nested query to effect my Hits result. but to include inner hits if match found other no result in inner hits. Commented Feb 1, 2016 at 5:30
  • @Val does my question clear now? Please suggest if i am missing something required. Commented Feb 1, 2016 at 5:41

1 Answer 1

1

You can try following query. This will fetch result in inner_hits only if it exists.

{
"query": {
  "bool": {
     "must": [
        {
           "term": {
              "id": "idValue"
           }
        }
     ],
     "should": [
        {
           "nested": {
              "path": "nestedType",
              "query": {
                 "match": {
                    "item2": "item2Value"
                 }
              },
              "inner_hits": {}
           }
        }
     ]
   }
 }
}
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.