Mapping an object
The object type is one of the most common field aggregation structures in documental databases.
An object is a base structure (analogous to a record in SQL): in JSON types, they are defined as key/value pairs inside the {}
symbols.
Elasticsearch extends the traditional use of objects (which are flat in DBMS), thus allowing for recursive embedded objects.
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. Again, I suggest using the Kibana console, which provides code completion and better character escaping for Elasticsearch.
How to do it…
We can rewrite the mapping code from the previous recipe using an array of items:
PUT test/_doc/_mapping { "properties" : { "id" : {"type" : "keyword"}, "date" : {"type" : "date"}, "customer_id" : {"type" : "keyword", "store" : true}, "sent" : {"type" : "boolean"}, "item" : { "type" : "object", "properties" : { "name" : {"type" : "text"}, "quantity" : {"type" : "integer"}, "price" : {"type" : "double"}, "vat" : {"type" : "double"} } } } }
How it works…
Elasticsearch speaks native JSON, so every complex JSON structure can be mapped in it.
When Elasticsearch is parsing an object type, it tries to extract fields and processes them as its defined mapping. If not, it learns the structure of the object using reflection.
The most important attributes of an object are as follows:
properties
: This is a collection of fields or objects (we can consider them as columns in the SQL world).enabled
: This establishes whether or not the object should be processed. If it's set tofalse
, the data contained in the object is not indexed and it cannot be searched (the default istrue
).dynamic
: This allows Elasticsearch to add new field names to the object using a reflection on the values of the inserted data. If it's set tofalse
, when you try to index an object containing a new field type, it'll be rejected silently. If it's set tostrict
, when a new field type is present in the object, an error will be raised, skipping the indexing process. The dynamic parameter allows you to be safe about making changes to the document's structure (the default istrue
).
The most used attribute is properties
, which allows you to map the fields of the object in Elasticsearch fields.
Disabling the indexing part of the document reduces the index size; however, the data cannot be searched. In other words, you end up with a smaller file on disk, but there is a cost in terms of functionality.
See also
Some special objects are described in the following recipes:
- The Mapping a document recipe
- The Managing a child document with a join field recipe
- The Mapping nested objects recipe