# Can I label which entry of a nested property triggered a match in a nested query?

**URL:** <https://discuss.elastic.co/t/can-i-label-which-entry-of-a-nested-property-triggered-a-match-in-a-nested-query/319833>\
**Category:** Elasticsearch\
**Created:** [November 25, 2022, 3:22pm UTC](https://discuss.elastic.co/t/can-i-label-which-entry-of-a-nested-property-triggered-a-match-in-a-nested-query/319833 "2022-11-25T15:22:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamFIREcracker](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iamfirecracker/32/113790_2.png) [@iamFIREcracker](https://discuss.elastic.co/u/iamFIREcracker)\
**Post date:** [November 25, 2022, 3:22pm UTC](https://discuss.elastic.co/t/can-i-label-which-entry-of-a-nested-property-triggered-a-match-in-a-nested-query/319833/1 "2022-11-25T15:22:43Z")

</div>

Let's say you were trying to model a "user" and decided that the following schema would be good enough for the job:

- `first`, the user's first name
- `last`, the user's last name
- `custom`, an array of of custom fields (i.e. objects with two properties: `name` and `value`)

With this in mind, you might end up creating an index as follows:

```auto
PUT /test-index-20221125-nrveqscnji
{
  "mappings": {
    "properties": {
      "first": { "type": "text" },
      "last": { "type": "text" },
      "custom": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "keyword"
          },
          "value": {
            "type": "text"
          }
        }
      }
    }
  }
}

```

Let's add a couple of users to the index. Bill Gates, living in Seattle, and founder of the Bill & Melinda Gates foundation:

```auto
PUT /test-index-20221125-nrveqscnji/_doc/1
{
  "first": "Bill",
  "last": "Gates",
  "custom": [
    {
      "name": "location",
      "value": "Seattle"
    },
    {
      "name": "bio",
      "value": "Founder of Bill & Melinda Gates foundation"
    }
  ]
}

```

Ivo Jurek, CEO of Gates Corporation:

```auto
PUT /test-index-20221125-nrveqscnji/_doc/2
{
  "first": "Ivo",
  "last": "Jurek",
  "custom": [
    {
      "name": "title",
      "value": "CEO of Gates Corporation"
    }
  ]
}

```

Now, let's say we wanted to search all the documents matching `gates`; one could implement that with the following query:

```auto
GET /test-index-20221125-nrveqscnji/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "first": {
              "query": "gates"
            }
          }
        },
        {
          "match": {
            "last": {
              "query": "gates"
            }
          }
        },
        {
          "nested": {
            "path": "custom",
            "query": {
              "match_phrase_prefix": {
                "custom.value": {
                  "query": "gates"
                }
              }
            }
          }
        }
      ]
    }
  }
}

```

That will work just fine; but what if we wanted to know _why_ a specific document matched? Named queries can partially help:

```auto
GET /test-index-20221125-nrveqscnji/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "first": {
              "query": "gates",
              "_name": "first"
            }
          }
        },
        {
          "match": {
            "last": {
              "query": "gates",
              "_name": "last"
            }
          }
        },
        {
          "nested": {
            "path": "custom",
            "query": {
              "match_phrase_prefix": {
                "custom.value": {
                  "query": "gates"
                }
              }
            },
            "_name": "custom"
          }
        }
      ]
    }
  }
}

```

With the above, we will indeed be able to understand if a match happened on the first name, on the last name, or on _any_ of the custom fields; but what if (I promise this is the last time) I wanted to know _which_ of the custom fields (i.e. which entry of the nested property) triggered a match? Is it possible to do that, or do I have to post-process the query result to achieve that?

A possible solution would be to write many nested queries, one per custom field, and each with a different `_named` value:

```auto
GET /test-index-20221125-nrveqscnji/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "first": {
              "query": "gates",
              "_name": "first"
            }
          }
        },
        {
          "match": {
            "last": {
              "query": "gates",
              "_name": "last"
            }
          }
        },
        {
          "nested": {
            "path": "custom",
            "query": {
              "bool": {
                "must": [
                  {
                    "match_phrase_prefix": {
                      "custom.value": {
                        "query": "gates"
                      }
                    }
                  },
                  {
                    "match": {
                      "custom.name": "location"
                    }
                  }
                ]
              }
            },
            "_name": "custom_location"
          }
        },
        {
          "nested": {
            "path": "custom",
            "query": {
              "bool": {
                "must": [
                  {
                    "match_phrase_prefix": {
                      "custom.value": {
                        "query": "gates"
                      }
                    }
                  },
                  {
                    "match": {
                      "custom.name": "bio"
                    }
                  }
                ]
              }
            },
            "_name": "custom_bio"
          }
        },
        {
          "nested": {
            "path": "custom",
            "query": {
              "bool": {
                "must": [
                  {
                    "match_phrase_prefix": {
                      "custom.value": {
                        "query": "gates"
                      }
                    }
                  },
                  {
                    "match": {
                      "custom.name": "title"
                    }
                  }
                ]
              }
            },
            "_name": "custom_title"
          }
        }
      ]
    }
  }
}

```

But what if (I know, I lied), you don't know the full list of custom fields upfront; or, even worse, the list is kind of big (in the 100-1000 range)?

Thanks in advance,  
M.

---

<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:** [December 23, 2022, 3:23pm UTC](https://discuss.elastic.co/t/can-i-label-which-entry-of-a-nested-property-triggered-a-match-in-a-nested-query/319833/2 "2022-12-23T15:23:42Z")

</div>

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