# How to return only the matched object in a nested field, instead of the whole object?

**URL:** <https://discuss.elastic.co/t/how-to-return-only-the-matched-object-in-a-nested-field-instead-of-the-whole-object/251073>\
**Category:** Elasticsearch\
**Created:** [October 6, 2020, 6:13am UTC](https://discuss.elastic.co/t/how-to-return-only-the-matched-object-in-a-nested-field-instead-of-the-whole-object/251073 "2020-10-06T06:13:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bart\_de\_Man](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bart_de_man/32/48319_2.png) [@Bart\_de\_Man](https://discuss.elastic.co/u/Bart_de_Man)\
**Post date:** [October 6, 2020, 6:13am UTC](https://discuss.elastic.co/t/how-to-return-only-the-matched-object-in-a-nested-field-instead-of-the-whole-object/251073/1 "2020-10-06T06:13:56Z")

</div>

**Elasticsearch version** ( `bin/elasticsearch --version` ):  
5.6.11  
**Plugins installed** :

**JVM version** ( `java -version` ):  
openjdk version "1.8.0\_222"  
OpenJDK Runtime Environment (build 1.8.0\_222-8u222-b10-1ubuntu1~16.04.1-b10)  
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

**OS version** ( `uname -a` if on a Unix-like system):  
Linux myserver 4.4.0-131-generic #157-Ubuntu SMP Thu Jul 12 15:51:36 UTC 2018 x86\_64 x86\_64 x86\_64 GNU/Linux

How do I retrieve only the matched element in a nested object instead of the whole object? in my case being the: my\_nested\_field.my\_object.name that matches my criteria?

my mapping:

```auto
{
  "my_super_special_index": {
    "aliases": {
      "my_super_special_index_alias": {}
    },
    "mappings": {
      "my_super_special_index": {
        "properties": {
          "my_nested_field": {
            "type": "nested",
            "properties": {
              "my_object": {
                "properties": {
                  "id": {
                    "type": "integer"
                  },
                  "last_known_party": {
                    "type": "boolean"
                  },
                  "name": {
                    "type": "text",
                    "store": true,
                    "analyzer": "translation_index_analyzer",
                    "search_analyzer": "translation_search_analyzer"
                  },
                  "name_raw": {
                    "type": "keyword"
                  },
                }
              }
            }
          }
        }
      }
    }
  }
}

```

my query:

```auto
GET my_super_special_index/_search
{
  "_source": "my_nested_field",
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "my_nested_field",
            "query": {
              "bool": {
                "should": [
                  {
                    "match_phrase": {
                      "my_nested_field.my_object.name": {
                        "query": "my name"
                      }
                    }
                  },
                  {
                    "match": {
                      "my_nested_field.my_object.name": {
                        "query": "my name",
                        "boost": 100
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "size": 50
}

```

What im getting as result :

```auto
{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1337,
    "hits": [
      {
        "_index": "my_super_special_index",
        "_type": "my_super_special_index",
        "_score": 2433.0676,
        "_source": {
          "id": "4712182",
          "my_nested_fields": [
            {
              ...
              "my_object": [
                {
                  "id": "22222",
                  "name": "name i want",
                  "name_raw": "name i want",
                  "last_known_party": true
                },
                {
                  "id": "13333",
                  "name": "hiyoo definitely doesnt match",
                  "name_raw": "hiyoo definitely doesnt match",
                  "last_known_party": true
                }
              ],
              "my_other_object": [
                {
                  "id": "26672",
                  "name": "dont really like this",
                  "name_raw": "dont really like this",
                  "last_known_party": true
                }
              ]
            }
            ],
        }
      },
      
      {
        "_index": "my_super_special_index",
        "_type": "my_super_special_index",
        "_score": 2422.111,
        "_source": {
          "id": "357878",
          "my_nested_fields": [
            {
              ...
              "my_object": [
                {
                  "id": "661111",
                  "name": "ratatoille",
                  "name_raw": "ratatoille",
                  "last_known_party": true
                },
                {
                  "id": "2334",
                  "name": "name i want or close match",
                  "name_raw": "name i want or close match",
                  "last_known_party": true
                }
              ],
              "my_other_object": [
                {
                  "id": "63111",
                  "name": "ttttt ok 1337",
                  "name_raw": "ttttt ok 1337",
                  "last_known_party": true
                }
              ]
            }
            ],
        }
      }
  }
}

```

What I wish to get from ES:

```auto
{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1337,
    "hits": [
      {
        "_index": "my_super_special_index",
        "_type": "my_super_special_index",
        "_score": 2433.0676,
        "_source": {
          "id": "4712182",
          "my_nested_fields": [
            {
              ...
              "my_object": [
                {
                  "id": "22222",
                  "name": "name i want",
                  "name_raw": "name i want",
                  "last_known_party": true
                }
              ]
            }
            ],
        }
      },
      
      {
        "_index": "my_super_special_index",
        "_type": "my_super_special_index",
        "_score": 2422.111,
        "_source": {
          "id": "357878",
          "my_nested_fields": [
            {
              ...
              "my_object": [
                {
                  "id": "2334",
                  "name": "name i want or close match",
                  "name_raw": "name i want or close match",
                  "last_known_party": true
                }
              ]
            }
            ],
        }
      }
  }
}

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 6, 2020, 7:51am UTC](https://discuss.elastic.co/t/how-to-return-only-the-matched-object-in-a-nested-field-instead-of-the-whole-object/251073/2 "2020-10-06T07:51:11Z")

</div>

There is the [inner hits feature](https://www.elastic.co/guide/en/elasticsearch/reference/7.9/inner-hits.html), however I do not remember, if this was already part of Elasticsearch 5.6 - which you should upgrade, as it is EOL since more than 1.5 years. I don't think so, though.

---

<div class="post-metadata">

**Author:** ![Bart\_de\_Man](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bart_de_man/32/48319_2.png) [@Bart\_de\_Man](https://discuss.elastic.co/u/Bart_de_Man)\
**Post date:** [October 6, 2020, 10:16am UTC](https://discuss.elastic.co/t/how-to-return-only-the-matched-object-in-a-nested-field-instead-of-the-whole-object/251073/3 "2020-10-06T10:16:59Z")

</div>

I had to restructure a little bit so i wouldn't retrieve the whole array as was happening now with said objects inside.

Is there really no other way to obtain just the object im looking for inside said array with the inner\_hits? Clearly elasticsearch knows what i was looking for and want just only that match and not the array its in.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 3, 2020, 10:17am UTC](https://discuss.elastic.co/t/how-to-return-only-the-matched-object-in-a-nested-field-instead-of-the-whole-object/251073/4 "2020-11-03T10:17:18Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
