Using nested objects
Nested objects can come in handy in certain situations. Basically, with nested objects, ElasticSearch allows us to connect multiple documents together—one main document and multiple dependent ones. Now, imagine that we have a shop with clothes and we store the size and color of each T-shirt. Our standard, non-nested mappings could look like the following code (stored in cloth.json
):
{ "cloth" : { "properties" : { "name" : {"type" : "string", "store" : "yes", "index" : "analyzed"}, "size" : {"type" : "string", "store" : "yes", "index" : "not_analyzed"}, "color" : {"type" : "string", "store" : "yes", "index" : "not_analyzed"} } } }
Now imagine that we have a T-shirt in our shop that we only have in the XXL size in "red" and XL size in "black". So our example document could look like the following code:
{ "name" : "Test shirt", "size" : [ "XXL", "XL" ], "color" : [ "red", "black" ] }
But if one of our clients were to search our shop in order to find the XXL...