Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

We have one document in elastic search with multiple sections of name/value pair and we want to fetch value's only based on name column value.

"envelopeData": {

  "envelopeName": "Bills",

  "details": {

    "detail": [

      {

        "name": "UC_CORP",

        "value": "76483"

      },

      {

        "name": "UC_CYCLE",

        "value": "V"

      }    

We are expecting only 76483 as result based on name equals to UC_CORP

1 Answer

0 votes
by (44.4k points)

If the field envelopeData.details.detail is nested type then you can perform a match query for the desired name on the nested path and can use inner_hits to get just the value.

Only if the field envelopeData.details.detail is nested you can perform the match query. The match query can be made for the desired name on the nested path. inner_hits can get value.

If it is not nested, map the field envelopeData.details.detail as nested:

PUT stackoverflow

{

  "mappings": {

    "_doc": {

      "properties": {

        "envelopeData.details.detail": {

          "type": "nested" 

        }

      }

    }

  }

}

After that, you can use inner_hits to perform the following query:

GET stackoverflow/_search

{

  "_source": "false", 

  "query": {

    "nested": {

      "path": "envelopeData.details.detail",

      "query": {

        "match": {

          "envelopeData.details.detail.name.keyword": "UC_CORP"

        }

      }, 

      "inner_hits": {

        "_source": "envelopeData.details.detail.value"

      }

    }

  }

}

The output will look like this:

{

  "_index": "stackoverflow",

  "_type": "_doc",

  "_id": "W5GUW2gB3GnGVyg-Sf4T",

  "_score": 0.6931472,

  "_source": {},

  "inner_hits": {

    "envelopeData.details.detail": {

      "hits": {

        "total": 1,

        "max_score": 0.6931472,

        "hits": [

          {

            "_index": "stackoverflow",

            "_type": "_doc",

            "_id": "W5GUW2gB3GnGVyg-Sf4T",

            "_nested": {

              "field": "envelopeData.details.detail",

              "offset": 0

            },

            "_score": 0.6931472,

            "_source": {

              "value": "76483"  -> Outputs value only

            }

          }

        ]

      }

    }

  }

}

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
asked Jul 18, 2019 in AWS by yuvraj (19.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...