Hierarchical relationships..?

Hi All, Does anyone have any experience in storing deep, hierarchical relationships in ES?

We are trying to model something along the lines of:

Food -> Fruit -> Green -> Apple Doc
Food -> Fruit -> Round -> Apple Doc
Food -> Healthy -> Apple Doc
Food -> Apple Doc

I want to be able to find the Apple (by some document attributes) but also know that it lives in 2 places (with the hierarchy returned) - ideally with references /snippets to the ancestor documents?

Each of the ancestors in the list are documents in their own rights and I would like to be able to search within any level and find Apple...

Its also worth noting that there are millions of items at each level... so flattening IDs etc is a last resort.


You can index the categories, like

apple doc -> category : [ "Food/Fruit/Green", "Food/Fruit/Round", "Food/Healthy", "Food" ]

Thanks for the suggestion.

We'd thought about using a path tokenizer...

A little more information... :smile:

  1. It doesn't give me a reference to the "parent" documents eg. Fruit..

  2. It presents an ingest / management issue - that if the hierarchy changes (which it will and quite regularly), we may have to update millions of documents.

  3. Categories are dynamic in that we would like to link them to documents that meet a set of query criteria / list of IDs.

We could manage 2 and 3 with some application logic and Percolator (I think)..?

Why don't you use a separate field for each level when you can use aggregations to generate the hierarchy.

level1 = ["Food"]
level2 = ["Food/Fruit", "Food/Healthy"]
level3 = ["Food/Fruit/Green", "Food/Fruit/Round"]

You still have the update issue, but you can easily link to parent documents by performing the same query and using a filter ie. level2 = "Food/Fruit" for all fruit.