# Nested doc match only one doc and query term

**URL:** <https://discuss.elastic.co/t/nested-doc-match-only-one-doc-and-query-term/110711>\
**Category:** Elasticsearch\
**Created:** [December 7, 2017, 4:03pm UTC](https://discuss.elastic.co/t/nested-doc-match-only-one-doc-and-query-term/110711 "2017-12-07T16:03:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tinman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tinman/32/133391_2.png) [@tinman](https://discuss.elastic.co/u/tinman)\
**Post date:** [December 7, 2017, 4:03pm UTC](https://discuss.elastic.co/t/nested-doc-match-only-one-doc-and-query-term/110711/1 "2017-12-07T16:03:59Z")

</div>

Slight background:  
ES version: 5.5,  
Use case: a part will have a list of attributes and each attribute as the mapping below shows has name/value combo.

Mapping:

```
{
  "parts": {
    "mappings": {
      "basic": {
        "properties": {
          "attributes": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "text"
              },
              "value": {
                "type": "text"
              }
            }
          },
          "description": {
            "type": "text"
          },
          "partNumber": {
            "type": "text"
          },
          "subCommodity": {
            "properties": {
              "name": {
                "type": "text"
              }
            }
          }
        }
      }
    }
  }
}

```

I am trying to query like so:

```
{
  "query": {
    "nested" : {
      "query" : {
        "match" : {
          "attributes.value" : {
            "query" : "red helmet"
          }
        }
      },
      "path" : "attributes",
      "score_mode" : "avg"
    }
  }
}

```

When I run that against a doc,

```
{
  "partNumber": "ABDEF-123456",
  "attributes": [
    {
      "name": "Color/Finish",
      "value": "Red"
    },
    {
      "name": "Type",
      "value": "Helmet"
    }
  ],
  "subCommodity": {
    "name": "Offroad Full Face 1/pc Adult Helmet"
  }
}

```

The `_explain` shows that "red" does not match but "helmet" does:

```
{
    "explanation": {
        "value": 6.135972,
        "description": "sum of:",
        "details": [
    ..
            "description": "weight(attributes.value:helmet in 260803) [PerFieldSimilarity], result of:",
        ]
    }
}

```

now when I query the non-nested field, `subCommodity.name`, on multiple query terms like so:

```
{
  "query": {
    "match": {
      "subCommodity.name": "Offroad Helmet"
    }
  }
}

```

The resulting explain indicates that both "offroad" and "helmet" match:

```
{
    "explanation": {
        "value": 4.0912876,
        "description": "sum of:",
        "details": [
            {
                "value": 4.0912876,
                "description": "sum of:",
                "details": [
                    {
                        "description": "weight(subCommodity.name:offroad in 260814) [PerFieldSimilarity], result of:",
                    },
                    {
                        "description": "weight(subCommodity.name:helmet in 260814) [PerFieldSimilarity], result of:",
                    }
                ]
            }
    ..
}

```

As a final example when I modify the single match on "red helmet" into a match per word like so,

```
{
  "query": {
    "bool": {
      "must": [{
        "nested" : {
          "query" : {
            "match" : {
              "attributes.value" : {
                "query" : "red"
              }
            }
          },
          "path" : "attributes",
          "score_mode" : "avg"
        }
      },{
        "nested" : {
          "query" : {
            "match" : {
              "attributes.value": {
                "query" : "helmet"
              }
            }
          },
          "path" : "attributes",
          "score_mode" : "avg"
        }
      }]
    }
  }
}

```

And run an explain against the example doc, both "red" and "helmet" match on `attributes.value`:

```
"description": "weight(attributes.value:red in 260810) [PerFieldSimilarity], result of:",
..
"description": "weight(attributes.value:helmet in 260803) [PerFieldSimilarity], result of:",

```

Now we can see both "red" and "helmet" matcn a nested doc at the path, `attributes.value`, in this query.

Does a nested doc query regardless of score mode only consider or report a single nested doc?

When I change the score modes in the explain the scores do not change so I do not believe more  
than one nested doc is involved ie. once ES query phase finds an attribute matching "helmet" it  
skips the remaining query terms notably "red".

---

<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:** [January 4, 2018, 4:04pm UTC](https://discuss.elastic.co/t/nested-doc-match-only-one-doc-and-query-term/110711/2 "2018-01-04T16:04:04Z")

</div>

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