Term query not working where type is "text"

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: -

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

This is Mapping json:-

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

usecase: filter on the exact name value.

Thanks,
Shreeshail

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. If you're looking for an exact match using terms on this field I would recommend using the keyword type instead.

Hope that helps!

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

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 instead as recommended in the docs link above to see if that gives you the results for your data set that you want?

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

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

Thanks @carly.richmond for your support

1 Like

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