Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch: Can I query the .raw field?

I am attempting to query the .raw version of a field that I have in my elasticsearch (version 5.0.0) index. The name of the field is 'region' and its mapping is the following:

{

  "properties": {
    "region": {
      "type": "text", "analyzer": "custom_analyzer", 
      "fields": {
        "raw": { 
              "type":  "keyword", "index": "not_analyzed"
        }
      }
    }
  }
}

Note that I had initially set the region mapping with the analyzed version and in the second step I updated the mapping by adding the raw version of the field.

As far as I understood from the documentation and a similar question here, I can query the raw field to get the documents whose regions exactly match my query. However, by using the following query, no results are returned.

{
   "match": {
        "region.raw": "Northern Ireland"
   }
}

In contrary, by using the analyzed version of my field, the results are returned as expected.

{
   "match": {
        "region": "Northern Ireland"
   }
}

As I need to use the raw field, I have the following questions:

  • Is it correct the way I have set my mapping for the region field?
  • Is it correct the way I query?

Thank you very much for your help.

like image 407
christinabo Avatar asked Jan 18 '17 15:01

christinabo


1 Answers

Nope, you query is correct. The problem is that you have to reindex your data in order to index with the new mapping version.

like image 122
betto86 Avatar answered Sep 26 '22 15:09

betto86