How do you build a org chart?

I have the following data :

Parent_id | ID | Company name
null        1    Company A
1           2    Company B
2           3    Company C
3           4    Company D
3           5    Company E

My goal it is to create a graph that resembles the hierarchy of that data.

How can I define this relationship?

The Graph api currently relies on entities of the same type (in your case, companies) being found in the same field. If you plot companies from the id and parentId field then id:CompanyB will be represented as a different vertex from parentId:CompanyB. To overcome this you have to put a copy of company names into a common field e.g. parent_and_child_ids.

Here is your data and the mapping as an example:

DELETE org
PUT org
{
   "settings": {
	  "number_of_replicas": "0",
	  "number_of_shards": "1"
   },
   "mappings": {
	  "doc": {
		 "properties": {
			"id": {
			   "type": "string",
			   "index": "not_analyzed",
			   "copy_to":"parent_and_child_ids"
			},
			"parent_id": {
			   "type": "string",
			   "index": "not_analyzed",
			   "copy_to":"parent_and_child_ids"
			},
			"parent_and_child_ids": {
			   "type": "string",
			   "index": "not_analyzed"
			}
		 }
	  }
   }
}
POST org/doc
{  "id":"Company A" }
POST org/doc
{ "id":"Company B", "parent_id": "Company A"}
POST org/doc
{ "id":"Company C","parent_id": "Company B" }
POST org/doc
{"id":"Company D","parent_id": "Company C"}
POST org/doc
{"id":"Company E","parent_id": "Company C"}

Then if you use the Graph UI tool with "certainty" set to 1 and "significant links" turned off you would get this visualization:

Note that there is no notion of "up" - we just know these companies are related and so A is not necessarily positioned at the top. Hierarchies are a special form of graph and one that our existing elastic Graph tools are not specialized for. You're probably better off loading data into a dedicated org-chart tool.

1 Like

I appreciate your help. Thank you!