# Term query not working where type is "text"

**URL:** <https://discuss.elastic.co/t/term-query-not-working-where-type-is-text/324422>\
**Category:** Elasticsearch\
**Created:** [February 1, 2023, 12:22pm UTC](https://discuss.elastic.co/t/term-query-not-working-where-type-is-text/324422 "2023-02-01T12:22:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shebbi](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@shebbi](https://discuss.elastic.co/u/shebbi)\
**Post date:** [February 1, 2023, 12:22pm UTC](https://discuss.elastic.co/t/term-query-not-working-where-type-is-text/324422/1 "2023-02-01T12:22:28Z")

</div>

Hi Team,

We are using ES 7.16.2 version and below is the query which we have generated using Java apis, Please let us know why it is not working or are we doing something wrong?

**Query: -**

```auto
{
  "bool" : {
    "must" : [
      {
        "term" : {
          "name" : {
            "value" : "User task",
            "boost" : 1.0
          }
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

```

**This is Mapping json:-**

```auto
"name": {
        "type": "text"
      },

```

usecase: filter on the exact name value.

Thanks,  
Shreeshail

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [February 1, 2023, 1:52pm UTC](https://discuss.elastic.co/t/term-query-not-working-where-type-is-text/324422/2 "2023-02-01T13:52:08Z")

</div>

Hi Shreeshail,

By not working, do you mean the query is returning no results, or is returning a particular error? Can you share an example of the document(s) you are trying to find with your query?

[The term query is not recommended for text fields as per the docs](https://www.elastic.co/guide/en/elasticsearch/reference/7.16/query-dsl-term-query.html). If you're looking for an exact match using terms on this field I would recommend using the `keyword` type instead.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![shebbi](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@shebbi](https://discuss.elastic.co/u/shebbi)\
**Post date:** [February 1, 2023, 5:36pm UTC](https://discuss.elastic.co/t/term-query-not-working-where-type-is-text/324422/3 "2023-02-01T17:36:52Z")

</div>

Thanks @carly.richmond for your response,

It is giving result which is empty even though it has records, we cannot change type to keyword as it is not editable.

Thanks,  
Shreeshail

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [February 1, 2023, 5:46pm UTC](https://discuss.elastic.co/t/term-query-not-working-where-type-is-text/324422/4 "2023-02-01T17:46:44Z")

</div>

Bu not editable, do you mean the index mapping is not editable? That's right, you would need to create a temp index with the new mapping including the type change, reindex the documents to the new index, delete the old one and rename the new one.

If you cannot recreate, have you tried using [`match`](https://www.elastic.co/guide/en/elasticsearch/reference/7.16/query-dsl-match-query.html) instead as recommended in the docs link above to see if that gives you the results for your data set that you want?

---

<div class="post-metadata">

**Author:** ![shebbi](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@shebbi](https://discuss.elastic.co/u/shebbi)\
**Post date:** [February 1, 2023, 6:01pm UTC](https://discuss.elastic.co/t/term-query-not-working-where-type-is-text/324422/5 "2023-02-01T18:01:47Z")

</div>

Yeah I tried with match query but it is also not giving exact match results, please have a look into below example,

We have 2 names with values

1. **Task 1**
2. **Task 1 test**

If i search **Task 1** with match query it is returning both **Task 1** and **Task 1 test** (which we don't want, it should return only **Task 1** ).

Thanks,  
Shreeshail

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [February 2, 2023, 10:00am UTC](https://discuss.elastic.co/t/term-query-not-working-where-type-is-text/324422/6 "2023-02-02T10:00:22Z")

</div>

Yes, so if you want an exact match, **Task 1** but not **Task 1 test** you'll need `keyword` type with `term` query.

---

<div class="post-metadata">

**Author:** ![shebbi](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@shebbi](https://discuss.elastic.co/u/shebbi)\
**Post date:** [February 2, 2023, 10:05am UTC](https://discuss.elastic.co/t/term-query-not-working-where-type-is-text/324422/7 "2023-02-02T10:05:08Z")

</div>

Thanks @carly.richmond for your support

---

<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:** [March 2, 2023, 10:05am UTC](https://discuss.elastic.co/t/term-query-not-working-where-type-is-text/324422/8 "2023-03-02T10:05:18Z")

</div>

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