19
votes

I am trying to index a json field in elastic search, I have given it external mapping that this field should be treated as string and not json, also indexing is not required for it, so no need to analyze it. The mapping for this is following

"json_field": {
    "type": "string",
    "index": "no"
},

Still at the time of indexing, this field is getting analyzed and because of that I am getting MapperParsingException

in Short How can we store json as a string in elastic search without getting analyzed ?

3

3 Answers

26
votes

Finally got it, if you want to store JSON as a string, without analyzing it, the mapping should be like this

"json_field": {
    "type": "object",
    "enabled" : false
},

The enabled flag allows to disable parsing and indexing a named object completely. This is handy when a portion of the JSON document contains an arbitrary JSON which should not be indexed, nor added to the mapping.

Update - From ES version 7.12 "enabled" has been changed to "index".

2
votes

As of today ElasticSearch 7.12 "enabled" is now "index".

So mapping should be like this:

"json_field": {
    "type": "object",
    "index" : false
},
0
votes

Solution

Set "enabled": false for the field.

curl -X PUT "localhost:9200/{{INDEX-NAME}}/_mapping/doc" -H 'Content-Type: application/json' -d'
{
    "properties" : {
      "json_field" : {
        "type" : "object",
        "enabled": false
      }
    }
}

Note: This cannot be applied to the existing field. Either pass it in mapping during the creation of index or you can always create a new field.

Explanation

The enabled setting, which can be applied only to the top-level mapping definition and to object fields, causes Elasticsearch to skip parsing of the contents of the field entirely. The JSON can still be retrieved from the _source field, but it is not searchable or stored in any other way:

Ref: Elasticsearch Docs