Search code examples
springelasticsearchspring-data-elasticsearch

Nested Query Elastic Search


Currently I am trying to search/filter a nested Document in Elastic Search Spring Data.

The Current Document Structure is:

{
  "id": 1,
  "customername": "Cust@123",
  "policydetails": {
    "address": {
      "city": "Irvine",
      "state": "CA",
      "address2": "23994384, Out OF World",
      "post_code": "92617"
    },
    "policy_data": [
      {
        "id": 1,
        "status": true,
        "issue": "Variation Issue"
      },
      {
        "id": 32,
        "status": false,
        "issue": "NoiseIssue"
      }
    ]
  }
}

Now we need to filter out the policy_data which has Noise Issue and If there is no Policy Data which has Noise Issue the policy_data will be null inside the parent document.

I have tried to use this Query

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "customername": "Cust@345"
          }
        },
        {
          "nested": {
            "path": "policiesDetails.policy_data",
            "query": {
              "bool": {
                "must": {
                  "terms": {
                    "policiesDetails.policy_data.issue": [
                      "Noise Issue"
                    ]
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

This works Fine to filter nested Document. But If the Nested Document does not has the match it removes the entire document from the view.

What i want is if nested filter does not match:-

{
  "id": 1,
  "customername": "Cust@123",
  "policydetails": {
    "address": {
      "city": "Irvine",
      "state": "CA",
      "address2": "23994384, Out OF World",
      "post_code": "92617"
    },
    "policy_data": null
  }

Solution

  • If any nested document is not found then parent document will not be returned.

    You can use should clause for policy_data. If nested document is found it will be returned under inner_hits otherwise parent document will be returned

    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "customername": "Cust@345"
              }
            }
          ],
          "should": [
            {
              "nested": {
                "path": "policydetails.policy_data",
                "inner_hits": {},  --> to return matched policy_data
                "query": {
                  "bool": {
                    "must": {
                      "terms": {
                        "policydetails.policy_data.issue": [
                          "Noise Issue"
                        ]
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      },
      "_source": ["id","customername","policydetails.address"] --> selected fields
    }
    

    Result:

    {
      "_index" : "index116",
      "_type" : "_doc",
      "_id" : "f1SxGHoB5tcHqHDtAkTC",
      "_score" : 0.2876821,
      "_source" : {
        "policydetails" : {
          "address" : {
            "city" : "Irvine",
            "address2" : "23994384, Out OF World",
            "post_code" : "92617",
            "state" : "CA"
          }
        },
        "id" : 1,
        "customername" : "Cust@123"
      },
      "inner_hits" : {
        "policydetails.policy_data" : {
          "hits" : {
            "total" : {
              "value" : 0,
              "relation" : "eq"
            },
            "max_score" : null,
            "hits" : [ ]  -->  nested query result , matched document returned
          }
        }
      }
    }