ElasticSearch Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

Adding metadata to a mapping

Sometimes, when working with a mapping, you need to store some additional data to be used for display purposes, ORM facilities, and permissions, or you simply need to track them in the mapping.

ElasticSearch allows you to store any kind of JSON data you want in the mapping with the _meta special field.

Getting ready

You need a working ElasticSearch cluster.

How to do it...

The _meta mapping field can be populated with any data you want:

{
  "order": {
    "_meta": {
      "attr1": ["value1", "value2"],
      "attr2": {
        "attr3": "value3"
      }
    }
  }
}

How it works...

When ElasticSearch processes a mapping and finds a _meta field, it stores the field in the global mapping status and propagates the information to all the cluster nodes.

The _meta field is only used for storage purposes; it's not indexed or searchable. It can be used to do the following:

  • Storing type metadata
  • Storing ORM (Object Relational Mapping) related information
  • Storing type permission information
  • Storing extra type information (such as the icon or filename used to display the type)
  • Storing template parts to render web interfaces