I wanted to create a mapping for movies
that look like:
{ title: "The Artist", genres: ["Drama", "Comedy"] }
In the ElasticSearch documentation I see this example:
"properties" : { "message" : {"type" : "string"}, "tags" : {"type" : "string", "index_name" : "tag"}, ... }
However, now I am confused.. What I expected to see was:
"properties" : { "message" : {"type" : "string"}, "tags" : {"type" : "array"} }
So, why does the example just provide a reference to another index? How would I define that "tags" index? Or, when would I use that Array mapping?
In Elasticsearch, there is no dedicated array type. Any field can contain zero or more values by default, however, all values in the array must be of the same datatype. So you don't have to specify anything specific in the mapping to store an array of values.
To convert an array of objects to a Map , call the map() method on the array and on each iteration return an array containing the key and value. Then pass the array of key-value pairs to the Map() constructor to create the Map object.
Mapping is the process of defining how a document, and the fields it contains, are stored and indexed. Each document is a collection of fields, which each have their own data type. When mapping your data, you create a mapping definition, which contains a list of fields that are pertinent to the document.
So ElasticSearch doesn't need to specify that a mapping is an array. You can treat any mapping as an array by using square brackets:
{ title: ["The Artist", "Formerly known as Prince" ], genres: ["Drama", "Comedy"], ... }
See the last sentence on the page:
We could, of course, name the field as tag and skip the index_name all together
The "index_name" mapping just allows you to define an alias in the plural form of tag -> tags.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With