Search code examples
elasticsearchelasticsearch-mapping

Overriding default keyword analysis for elasticsearch


I am trying to configure an elasticsearch index to have a default indexing policy of analysis with the keyword analyzer, and then overriding it on some fields, to allow them to be free text analyzed. So effectively opt-in free text analysis, where I am explicitly specifying in the mapping which fields are analysed for free text matching. My mapping defintion looks like this:

PUT test_index
{
   "mappings":{
      "test_type":{
         "index_analyzer":"keyword",
         "search_analyzer":"standard",
         "properties":{
            "standard":{
               "type":"string",
               "index_analyzer":"standard"
            },
            "keyword":{
               "type":"string"
            }
         }
      }
   }
}

So standard should be an analyzed field, and keyword should be exact match only. However when I insert some sample data with the following command:

POST test_index/test_type
{
  "standard":"a dog in a rug",
  "keyword":"sheepdog"
}

I am not getting any matches against the following query:

GET test_index/test_type/_search?q=dog

However I do get matches against:

GET test_index/test_type/_search?q=*dog*

Which makes me think that the standard field is not being analyzed. Does anyone know what I am doing wrong?


Solution

  • Nothing's wrong with the index created. Change your query to GET test_index/test_type/_search?q=standard:dog and it should return the expected results.

    If you do not want to specify field name in the query, update your mapping such that you provide the index_analyzer and search_analyzer values explicitly for each field with no default values. See below:

    PUT test_index
    {
       "mappings": {
          "test_type": {
             "properties": {
                "standard": {
                   "type": "string",
                   "index_analyzer": "standard",
                   "search_analyzer": "standard"
                },
                "keyword": {
                   "type": "string",
                   "index_analyzer": "keyword",
                   "search_analyzer": "standard"
                }
             }
          }
       }
    }
    

    Now if you try GET test_index/test_type/_search?q=dog, you'll get the desired results.