Ask Your Question
0

Propose a structure for hierarchical data in ElasticSearch.

asked 2023-06-04 23:46:15 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-04 23:54:01 +0000

lalupa gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-04 23:46:15 +0000

Seen: 16 times

Last updated: Jun 04 '23