Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an Array mapping in ES?

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?

like image 889
poseid Avatar asked Sep 19 '13 19:09

poseid


People also ask

How do I store an array in Elasticsearch?

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.

How do you map an array to an object?

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.

What is the use of mapping in Elasticsearch?

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.


1 Answers

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.

like image 67
Mark J Miller Avatar answered Oct 06 '22 02:10

Mark J Miller