Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic search mapping for nested json objects

Tags:

I need help regarding elastic search mapping of nested json document. I search in web a lot but didnt find any good to the point info. Suppose I have this type of data..

{   "name" : "Zach",   "car" : [     {       "make" : "Saturn",       "model" : "SL"     },     {       "make" : "Subaru",       "model" : "Imprezza"     }   ] } {   "name" : "Bob",   "car" : [     {       "make" : "Saturn",       "model" : "Imprezza"     }   ] } 

where car can have any number of data objects inside. According to the elastic search doc I came to know that for nested json I have to specify the type as nested. But there has no information regarding how I will specify the mapping info of variables under that nested type.

Like in the example above I can write the mapping like this.

{   "person":{     "properties":{       "name" : {         "type" : "string"       },       "car":{         "type" : "nested"       }     }   } } 

but how to provide mapping info for car.make & car.model??

Will this work fine without any future problem?

{   "person": {     "properties": {       "name" : {         "type" : "string"       },       "car": {         "type" : "nested"         "properties": {           "make": {....},           "model": {....}         }       }     }   } } 
like image 614
Jayadratha Mondal Avatar asked May 18 '15 16:05

Jayadratha Mondal


People also ask

What is nested mapping in Elasticsearch?

The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.

What is inner hits in Elasticsearch?

The inner hits feature can be used for this. This feature returns per search hit in the search response additional nested hits that caused a search hit to match in a different scope. Inner hits can be used by defining an inner_hits definition on a nested , has_child or has_parent query and filter.

What is a nested field?

When a packed class contains an instance field that is a packed type, the data for that field is packed directly into the containing class. The field is known as a nested field .

What is nested datatype?

Nested data types are structured data types for some common data patterns. Nested data types support structs, arrays, and maps. A struct is similar to a relational table. It groups object properties together.


1 Answers

You can do it like this:

PUT /my_index {   "mappings": {     "blogpost": {       "properties": {         "comments": {           "type": "nested",            "properties": {             "name":    { "type": "string"  },             "comment": { "type": "string"  },             "age":     { "type": "short"   },             "stars":   { "type": "short"   },             "date":    { "type": "date"    }           }         }       }     }   } } 

Quote from this section of the ES definitive guide.

like image 183
Andrei Stefan Avatar answered Oct 25 '22 03:10

Andrei Stefan