Shooting Stars
Shooting Stars

Reputation: 835

Elasticsearch Filtering woes, uppercase vs lowercase field filtering

I have a field in my index called "status" of keyword type.

When I try to filter with {"term": {"status": "Publish"}}, it returns no hits When I try to filter with {"term": {"status": 'publish"}}, it returns the correct result.

This would be one thing if the status was input as lowercase, but they're actually uppercase.

My kibana GET returns the products with "status": "Publish".

I also remember inserting the statuses with uppercase values. So why can I only filter by lowercase?

The big caveat, and I know its suspicious to do this, is I attempted to add the "status" mapping after the item indices were already created. Thats the main culprit for me right now as to why this is happening.

Does anyone know why the filtering only works with lowercase values when the actual value in the mapping is uppercase?

Upvotes: 0

Views: 826

Answers (1)

Bhavya
Bhavya

Reputation: 16172

The standard analyzer is the default analyzer if no analyzer is specified. So, Publish gets indexed as publish.

If you have not explicitly defined any mapping then you need to add .keyword to the status field. This uses the keyword analyzer instead of the standard analyzer (notice the ".keyword" after status field).

The term query does not apply any analyzers to the search term, so will only look for that exact term in the inverted index. So to search for the exact term, you need to use status.keyword OR change the mapping of the field.

{
  "query": {
    "term": {
      "status.keyword": "Publish"
    }
  }
}

Upvotes: 1

Related Questions