20
votes

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

3 Answers

44
votes

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.

0
votes

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

-1
votes
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"
             }
            }
           }
         }
       }
     }
    }
   }