Store Json field as String in Elastic search?

17,414

Solution 1

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".

Solution 2

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

So mapping should be like this:

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

Solution 3

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

Share:
17,414

Related videos on Youtube

NIlesh Sharma
Author by

NIlesh Sharma

A curious guy, looking for 'The Matrix' in this world

Updated on June 20, 2022

Comments

  • NIlesh Sharma
    NIlesh Sharma almost 2 years

    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 ?