1

I am new to elasticsearch. I have nested data.Users->Cars. I need help in writing nested mappings.

I have seen ES site regarding nesting query and basic one I am able to do. I ran into trouble while creating mapping for lets say depth 2/3.

The following mapping I am trying to create but it does not seem to be working.

I need to be able to query something like: get me all the documents where users.usertype=salaried and cars.make=honda.

Here is my mapping:

{
  "mappings": {
    "properties": {
      "users": {
        "type": "nested",
        "usertype": {
          "type": "text"
        },
        "cars": {
          "type": "nested",
          "properties": {
            "make": {
              "type": "text"
            },
            "model": {
              "type": "text"
            }
          }
        }
      }
    }
  }
}

Here is my sample data:

{
  "users": [
    {
      "usertype": "salaried",
      "cars": [
        {
          "make": "honda"
        },
        {
          "year": "2016"
        }
      ]
    },
    {
      "usertype": "business",
      "cars": [
        {
          "make": "BMW"
        },
        {
          "year": "2018"
        }
      ]
    }
  ]
}

While creating mapping I am getting following error:

"caused_by": {
     "type": "mapper_parsing_exception",
     "reason": "Mapping definition for [user] has unsupported parameters:  [details : {type=nested, properties={make={type=text}}}]"
}
Mike Doe
  • 16,349
  • 11
  • 65
  • 88
Tuhin Subhra Mandal
  • 473
  • 1
  • 5
  • 15

1 Answers1

0

You should define usertype as a property of users field as below:

 {
    "mappings": {
        "properties": {
            "users": {
                "type": "nested",
                "properties": {
                    "usertype": {
                        "type": "text"
                    },
                    "cars": {
                        "type": "nested",
                        "properties": {
                            "make": {
                                "type": "text"
                            },
                            "model": {
                                "type": "text"
                            }
                        }
                    }
                }
            }
        }
    }
}

And i think there is a problem in your sample data it should be

{
    "users": [
        {
            "usertype": "salaried",
            "cars": [
                {
                    "make": "honda",
                    "year": "2016"
                }
            ]
        },
        {
            "usertype": "business",
            "cars": [
                {
                    "make": "BMW",
                    "year": "2018"
                }
            ]
        }
    ]
}

year and make properties should be in the same object, not separates ones

Pierre Mallet
  • 7,053
  • 2
  • 19
  • 30
Nishant
  • 7,504
  • 1
  • 21
  • 34