ElasticSearch: Understanding Nested Data Types

{
name: “vk”,
age: “29”,
attributes: “normal”,
description: “hi i am happy and .....”
}
{
"movie_name": "Rang de basanti",
"release_year": "2006",
"actors": [
{
"name": "aamir", // actual name of the actor
"character_name": "dj" //name of the character he played
},
{
"name": "sharman",
"character_name": "sukhi"
}]
}}
POST my_movie_index/_doc
{
"movie_name": "Rang de basanti",
"actors": [
{
"name": "aamir",
"character_name": "dj"
},
{
"name": "sharman",
"character_name": "sukhi"
}
]
}
{
"my_movie_index": {
"mappings": {
"_doc": {
"properties": {
"actors": {
"properties": {
"character_name": {
"type": "text",
"fields": {
.......
I will not show the full mapping , as we are just concerned with mapping `actors` keys which by default is Object.
{
"movie_name": "Rang de basanti",
"actors.name": ["aamir","sharman"],
"actors.character_name": ["dj","sukhi"]
}
GET my_movie_index/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"movie_name.keyword": "Rang de basanti"
}
},
{
"term":{
"actors.name.keyword": "aamir"
}
},
{
"term":{
"actors.character_name.keyword": "sukhi"
}
}
]
}
}
}

The fix

PUT my_movie_index
{
"mappings": {
"_doc": {
"properties": {
"actors": {
"type":"nested",
"properties": {
"character_name": {
"type": "text",
"fields": {
..... //rest of the mapping
GET my_movie_index/_search
{
"query": {
"bool": {
"must": [
{"terms": {"movie_name.keyword": ["Rang de basanti"] }},
{
"nested": {
"path": "actors",
"query": {
"bool": {
"must": [
{"term": {"actors.name.keyword": "aamir"}},
{"term": {"actors.character_name.keyword": "sukhi"}}
]
}
}
}
}
]
}
}
}
// RETURNS NO DATA AS EXPECTED.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vipulbest

Vipulbest

Full Stack Web Application Engineer. Currently handling back end operations at Synup!