Mapping arrays
Array or multi-value fields are very common in data models (such as multiple phone numbers, addresses, names, aliases, and so on), but they're not natively supported in traditional SQL solutions.
In SQL, multi-value fields require you to create accessory tables that must be joined to gather all the values, leading to poor performance when the cardinality of the records is huge.
Elasticsearch, which works natively in JSON, provides support for multi-value fields transparently.
Getting ready
You will need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe of Chapter 1, Getting Started.
To execute the commands in this recipe, you can use any HTTP client, such as curl (https://curl.haxx.se/), Postman (https://www.getpostman.com/), or similar. I suggest using the Kibana console, which provides code completion and better character escaping for Elasticsearch.
How to do it…
To use an Array
type in our mapping, perform the following steps:
- Every field is automatically managed as an array. For example, to store tags for a document, the mapping would be as follows:
{ "properties" : { "name" : {"type" : "keyword"}, "tag" : {"type" : "keyword", "store" : true}, ... }
- This mapping is valid for indexing both documents. The following is the code for
document1
:{"name": "document1", "tag": "awesome"}
- The following is the code for
document2
:{"name": "document2", "tag": ["cool", "awesome", "amazing"] }
How it works…
Elasticsearch transparently manages the array: there is no difference if you declare a single value or a multi-value due to its Lucene core nature.
Multi-values for fields are managed in Lucene, so you can add them to a document with the same field name. For people with a SQL background, this behavior may be quite strange, but this is a key point in the NoSQL world as it reduces the need for a join query and creates different tables to manage multi-values. An array of embedded objects has the same behavior as simple fields.