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

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:

{
  "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:

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 :

{
  "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:

{
  "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!

There is the inner hits feature, 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.

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.

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