1 | initial version |
A common structure for hierarchical data in ElasticSearch is to use nested objects.
For example, let's say you have a dataset of employees and their departments. Each department can have multiple employees, and each employee has a unique identifier, as well as their name, job title, etc.
You could structure your ElasticSearch index as follows:
{
"mappings": {
"properties": {
"department": {
"type": "text"
},
"employees": {
"type": "nested",
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "text"
},
"job_title": {
"type": "text"
},
"salary": {
"type": "integer"
}
}
}
}
}
}
In this example, the "employees" field is a nested object containing properties for each employee. This allows you to easily query and filter by department, as well as by individual employees within a department.
To create a document in this index, you could use the following JSON structure:
{
"department": "HR",
"employees": [
{
"id": "123",
"name": "John Smith",
"job_title": "HR Manager",
"salary": 100000
},
{
"id": "456",
"name": "Jane Doe",
"job_title": "HR Representative",
"salary": 50000
}
]
}
This structure allows you to easily retrieve all employees in a department, search for specific employees by name or ID, and aggregate data by department or by employee.