# Unable to do terms query

**URL:** <https://discuss.elastic.co/t/unable-to-do-terms-query/231937>\
**Category:** Elasticsearch\
**Created:** [May 11, 2020, 6:34am UTC](https://discuss.elastic.co/t/unable-to-do-terms-query/231937 "2020-05-11T06:34:12Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmedbrandver](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@ahmedbrandver](https://discuss.elastic.co/u/ahmedbrandver)\
**Post date:** [May 11, 2020, 6:34am UTC](https://discuss.elastic.co/t/unable-to-do-terms-query/231937/1 "2020-05-11T06:34:12Z")

</div>

I am unable to a terms query on my data when I check the mapping field type was **text**. Is there a way where I can do an **IN** query on this field. There is only single data which is an id of my different database.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 11, 2020, 7:55am UTC](https://discuss.elastic.co/t/unable-to-do-terms-query/231937/2 "2020-05-11T07:55:33Z")

</div>

Welcome!

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![ahmedbrandver](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@ahmedbrandver](https://discuss.elastic.co/u/ahmedbrandver)\
**Post date:** [May 11, 2020, 9:45am UTC](https://discuss.elastic.co/t/unable-to-do-terms-query/231937/3 "2020-05-11T09:45:00Z")

</div>

Thank you for your instant reply. This is my mapping

```auto
"parsedIdentifiers": {
                    "properties": {
                        "imageUrl": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "productBvid": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                }

```

And productBvid container the uuid of products. I want to add a `terms` query in it so that I can get only those products which _ids_ I have provided. I am doing query like this

```auto
{
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "parsedIdentifiers.productBvid": ["4b1acb4f-8c6a-45c4-9b1d-676a338ce468"]
                        }
                    }
                ],
                "filter": [
                    {
                        "term": {
                            "service_name": "starship"
                        }
                    },
                    {
                        "term": {
                            "audit": "true"
                        }
                    }
                ]
            }
        },
        "sort": [
            {
                "@timestamp": "desc"
            }
        ],
        "size": 2
    }

```

but I am unable to get any data. But when I run this query

```auto
{
        "query": {
            "bool": {
                "must": [
                    {
                        "match": {
                            "parsedIdentifiers.productBvid": "4b1acb4f-8c6a-45c4-9b1d-676a338cd468"
                        }
                    }
                ],
                "filter": [
                    {
                        "term": {
                            "service_name": "starship"
                        }
                    },
                    {
                        "term": {
                            "audit": "true"
                        }
                    }
                ]
            }
        },
        "sort": [
            {
                "@timestamp": "desc"
            }
        ],
        "size": 2
    }

```

it works fine.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 11, 2020, 11:03am UTC](https://discuss.elastic.co/t/unable-to-do-terms-query/231937/4 "2020-05-11T11:03:29Z")

</div>

It's probably because of the type of the field or the analyzer used.  
If you need more help regarding this, please provide a full script we can iterate on to help.

Also have a look at the analyze API. It's often helpful.

---

<div class="post-metadata">

**Author:** ![ahmedbrandver](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@ahmedbrandver](https://discuss.elastic.co/u/ahmedbrandver)\
**Post date:** [May 11, 2020, 11:22am UTC](https://discuss.elastic.co/t/unable-to-do-terms-query/231937/5 "2020-05-11T11:22:47Z")

</div>

Elasticsearch is not allowing me to update the type. And the value which I am searching are like this `4b1acb4f-8c6a-45c4-9b1d-676a338ce468` what did you mean by analyzer? Token analyzer?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 11, 2020, 12:14pm UTC](https://discuss.elastic.co/t/unable-to-do-terms-query/231937/6 "2020-05-11T12:14:15Z")

</div>

> [@ahmedbrandver](#):
>
> Elasticsearch is not allowing me to update the type.

You need to reindex.

If you are using the default mapping, there's a chance that if you append `.keyword` to the field name, that will work.

Just a guess as I can't see your mapping.

> [@ahmedbrandver](#):
>
> what did you mean by analyzer?

I meant [Anatomy of an analyzer | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer-anatomy.html)

---

<div class="post-metadata">

**Author:** ![ahmedbrandver](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@ahmedbrandver](https://discuss.elastic.co/u/ahmedbrandver)\
**Post date:** [May 12, 2020, 3:58am UTC](https://discuss.elastic.co/t/unable-to-do-terms-query/231937/7 "2020-05-12T03:58:48Z")

</div>

> [@dadoonet](#):
>
> Just a guess as I can't see your mapping.

This is my mapping which is set as default

```auto
"parsedIdentifiers": {
                    "properties": {
                        "imageUrl": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "productBvid": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                }

```

This is the result of Tokken Analyzer

```auto
{
  "tokens": [
    {
      "token": "e3bb4e66",
      "start_offset": 0,
      "end_offset": 8,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "014c",
      "start_offset": 9,
      "end_offset": 13,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "49e6",
      "start_offset": 14,
      "end_offset": 18,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "b306",
      "start_offset": 19,
      "end_offset": 23,
      "type": "<ALPHANUM>",
      "position": 3
    },
    {
      "token": "c1c32fbfadc7",
      "start_offset": 24,
      "end_offset": 36,
      "type": "<ALPHANUM>",
      "position": 4
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 12, 2020, 8:57am UTC](https://discuss.elastic.co/t/unable-to-do-terms-query/231937/8 "2020-05-12T08:57:26Z")

</div>

So as I said, try:

```auto
{
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "parsedIdentifiers.productBvid.keyword": ["4b1acb4f-8c6a-45c4-9b1d-676a338ce468"]
                        }
                    }
                ],
                "filter": [
                    {
                        "term": {
                            "service_name": "starship"
                        }
                    },
                    {
                        "term": {
                            "audit": "true"
                        }
                    }
                ]
            }
        },
        "sort": [
            {
                "@timestamp": "desc"
            }
        ],
        "size": 2
    }

```

---

<div class="post-metadata">

**Author:** ![ahmedbrandver](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@ahmedbrandver](https://discuss.elastic.co/u/ahmedbrandver)\
**Post date:** [May 12, 2020, 9:01am UTC](https://discuss.elastic.co/t/unable-to-do-terms-query/231937/9 "2020-05-12T09:01:58Z")

</div>

> [@dadoonet](#):
>
> "terms": { "parsedIdentifiers.productBvid.keyword": ["4b1acb4f-8c6a-45c4-9b1d-676a338ce468"] }

After trying I get following response

```auto
{
    "took": 1232,
    "timed_out": false,
    "_shards": {
        "total": 158,
        "successful": 158,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}

```

My result is still not found

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 12, 2020, 10:11am UTC](https://discuss.elastic.co/t/unable-to-do-terms-query/231937/10 "2020-05-12T10:11:31Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

A script is something we can paste in Kibana and run.

---

<div class="post-metadata">

**Author:** ![ahmedbrandver](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@ahmedbrandver](https://discuss.elastic.co/u/ahmedbrandver)\
**Post date:** [May 12, 2020, 3:11pm UTC](https://discuss.elastic.co/t/unable-to-do-terms-query/231937/11 "2020-05-12T15:11:31Z")

</div>

Your solution worked. I mess up with the uuid that's why it wasn't working. Thank you 🙂

---

<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:** [June 9, 2020, 3:11pm UTC](https://discuss.elastic.co/t/unable-to-do-terms-query/231937/12 "2020-06-09T15:11:34Z")

</div>

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