30

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": {....}
        }
      }
    }
  }
}

3 Answers 3

69

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.

Sign up to request clarification or add additional context in comments.

5 Comments

You gave the impression you are not sure of the mappings correctness. You haven't mentioned anything else.
Yes sir, I was not sure about correctness of my mapping code. Now I'm sure. Thank you....
@AndreiStefan what if we do not provide properties under nested field? will elasticsearch map it properly?
@ANKIT if the mapping already exists and that specific field is nested, any new document that you add to that existent index that has a new sub-field belonging to the nested field, ES will automatically add those new sub-fields under the nested field already existent.
What if number of child fields not fixed. eg - we have 4 fields in one row and 5 fields in another row then is there option to map "properties": {* : {"type": "string"}}
0

Arrays of objects do not work as you would expect: you cannot query each object independently of the other objects in the array. If you need to be able to do this then you should use the nested data type instead of the object data type.

  tasks: {
      type: 'nested',
      properties: {
        id: {
          type: 'text',
        },
        description: {
          type: 'text',
        },
        assignee: {
          type: 'nested',
          properties: {
            id: {
              type: 'text',
            },
            thumbnail: {
              type: 'text',
            },
          },
        },
      },
    },

https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html

Comments

-2
put /my_index/_mapping/my_type 
{
 "person": {
   "properties": {
     "name" : {
       "type": "text",
       "analyzer": "string_lowercase",
       "fields": {
         "keyword": {
           "type": "keyword"
         }
       }
      },
      "car": {
        "type" : "nested",
        "properties": {
          "make": { 
            "type": "text",
            "analyzer": "string_lowercase",
            "fields": {
            "keyword": {
              "type": "keyword"
            }
           }
          },
          "model": { 
            "type": "text",
            "analyzer": "string_lowercase",
            "fields": {
            "keyword": {
              "type": "keyword"
             }
            }
           }
         }
       }
     }
    }
   }

2 Comments

Sorry, code without any explanation doesn't qualify as a valid answer.
While answers are always appreciated, it really helps to provide some additional info regarding how your code solves the problem at hand. Doing so aids those with similar (but not identical) problems. Not everyone may be familiar with your exact coding logic, but may understand your general approach or concept. To help improve your answer, please provide some context surrounding it, and see the help article on writing great answers for some tips on how to make your answers count :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.