# Terms are not working with multilevel nested query

**URL:** <https://discuss.elastic.co/t/terms-are-not-working-with-multilevel-nested-query/306428>\
**Category:** Elasticsearch\
**Tags:** kql-kibana-query-language, eql-elastic-query-language\
**Created:** [June 6, 2022, 6:51am UTC](https://discuss.elastic.co/t/terms-are-not-working-with-multilevel-nested-query/306428 "2022-06-06T06:51:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ravee1112](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ravee1112/32/106648_2.png) [@ravee1112](https://discuss.elastic.co/u/ravee1112)\
**Post date:** [June 6, 2022, 6:51am UTC](https://discuss.elastic.co/t/terms-are-not-working-with-multilevel-nested-query/306428/1 "2022-06-06T06:51:15Z")

</div>

Below is my mapping for index:

```auto
{
  "properties" : {
    "solutions" : {
      "type" : "nested",
      "properties" : {
        "deliverySkills" : {
          "type" : "nested",
          "properties" : {
            "mappingList" : {
              "type" : "text"
            },
            "name" : {
              "type" : "text"
            }
          }
        }
      }
    }
  }
}

```

Mine data for above mapping is:

```auto
{
  "solutions" : [
    {
      "deliverySkills" : [
        {
          "name" : "Python",
          "mappingList" : []
        },
        {
          "name" : "JSON",
          "mappingList" : []
        },
        {
          "name" : "API design",
          "mappingList" : [
            "Design API"
          ]
        }
      ]
    },
    {
      "deliverySkills" : [
        {
          "name" : "HTTP",
          "mappingList" : []
        },
        {
          "name" : "HTTPS",
          "mappingList" : []
        },
        {
          "name" : "JavaScript",
          "mappingList" : []
        }
      ]
    }
  ]
  }

```

I tried with the below query:

```auto
{
  "query": {
    "nested": {
      "path": "solutions",
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "solutions.deliverySkills",
                "query": {
                  "terms": {
                    "solutions.deliverySkills.name": ["JSON", "Python"]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

```

But it's not working. When i tried with the match instead of terms than it is working but i need to use the terms as i have to check from the array

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [June 6, 2022, 1:09pm UTC](https://discuss.elastic.co/t/terms-are-not-working-with-multilevel-nested-query/306428/2 "2022-06-06T13:09:02Z")

</div>

Hi!!

If you want to use the [Terms Query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html) the value must be exactly the same as the document.

> Returns documents that contain one or more exact terms in a provided field.
> 
> The terms query is the same as the term query, except you can search for multiple values.

You need add in name field this:

```auto
    "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }

```

Your mapping:

```auto
{
  "mappings": {
    "properties": {
      "solutions": {
        "type": "nested",
        "properties": {
          "deliverySkills": {
            "type": "nested",
            "properties": {
              "mappingList": {
                "type": "text"
              },
              "name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

Query:

```auto
{
  "query": {
    "nested": {
      "path": "solutions",
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "solutions.deliverySkills",
                "query": {
                  "terms": {
                    "solutions.deliverySkills.name.keyword": ["JSON", "Python"]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![ravee1112](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ravee1112/32/106648_2.png) [@ravee1112](https://discuss.elastic.co/u/ravee1112)\
**Post date:** [June 8, 2022, 2:53pm UTC](https://discuss.elastic.co/t/terms-are-not-working-with-multilevel-nested-query/306428/4 "2022-06-08T14:53:04Z")

</div>

I tried using the above solution but the result is the same as i am getting no results. Also the values are exactly same as the document.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [June 8, 2022, 3:16pm UTC](https://discuss.elastic.co/t/terms-are-not-working-with-multilevel-nested-query/306428/5 "2022-06-08T15:16:07Z")

</div>

> [@RabBit\_BR](#):
>
> ```auto
> "name": {
> "type": "text",
> "fields": {
> "keyword": {
> "type": "keyword",
> "ignore_above": 256
> }
> }
> 
> ```

You add this part in your mapping?

---

<div class="post-metadata">

**Author:** ![ravee1112](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ravee1112/32/106648_2.png) [@ravee1112](https://discuss.elastic.co/u/ravee1112)\
**Post date:** [June 9, 2022, 7:10am UTC](https://discuss.elastic.co/t/terms-are-not-working-with-multilevel-nested-query/306428/6 "2022-06-09T07:10:08Z")

</div>

Yes, I have added this and tried but not working

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [June 9, 2022, 3:15pm UTC](https://discuss.elastic.co/t/terms-are-not-working-with-multilevel-nested-query/306428/7 "2022-06-09T15:15:25Z")

</div>

Could you send the query that is being executed? I can test it here.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [June 10, 2022, 12:31am UTC](https://discuss.elastic.co/t/terms-are-not-working-with-multilevel-nested-query/306428/8 "2022-06-10T00:31:58Z")

</div>

I will give a little supplementary explanation.

First, if you have never read this warning:

> WARNING: Avoid using the `term` query for [`text`](https://www.elastic.co/guide/en/elasticsearch/reference/current/text.html) fields.  
> By default, Elasticsearch changes the values of `text` fields as part of [analysis](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html). This can make finding exact matches for `text` field values difficult.  
> To search `text` field values, use the [`match`](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html) query instead.

please read [this doc](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html#:~:text=You%20can%20use%20the%20term,for%20text%20field%20values%20difficult).

`Text` fields are analyzed (split for tokens, delete stop words, convert lower case... etc by default) to tokens before indexed but the phrase for term/terms query are not analyzed. The phrase must be exactly the same as the token after analysis. That's the reason why term/terms query doesn't hit the document.

Besides RabBit's suggestion to add keyword field, there are an option only to change the type itself from `text` to `keyword` just as:

```auto
"name": {
                "type": "keyword"
}

```

. To change the mappings, you need delete and create the index again or re-index from the index to another index.

---

<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:** [July 8, 2022, 12:32am UTC](https://discuss.elastic.co/t/terms-are-not-working-with-multilevel-nested-query/306428/9 "2022-07-08T00:32:19Z")

</div>

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