Store Json field as String in Elastic search? Store Json field as String in Elastic search? elasticsearch elasticsearch

Store Json field as String in Elastic search?


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


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

So mapping should be like this:

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


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