10

I am trying to store a tree in Elasticsearch. My tree can theoretically be infinitely long (although that is unlikely to happen) and can branch relatively often.

What is the best approach for storing this?

I looked into this question but its answer has limited performance when branching. Is there any way I can achieve proper nesting of documents, while still having the flexibility of branching, and the overall performance when reading a tree and its nodes?

Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187
  • You need to tell a little bit more about your use case(s). Will your tree be changing often? What kind of documents do you want to store at each node? Depending on your use case, you might consider using graph databases (Neo4J et al) instead... – Val Oct 04 '16 at 03:46
  • I already explained that I need to branch quite often. Each branch can grow quite often as well. Every node will almost only contain raw text and represents part of a "story tree" that represents a story with many outcomes. – Mathias Lykkegaard Lorenzen Oct 08 '16 at 15:00

1 Answers1

8

I very much recommend storing trees in ES in a similar fashion as Mongodb suggests using Materialized paths

Just store every node in the tree as a document inside an index, if you have different kinds of nodes you can just create a type field and filter by that.

Store the path of the document in the tree as a comma delimited string like this:

path: ',root,books,fiction'

you can later use text search to search in subtrees

See the docs here for more details: https://docs.mongodb.com/manual/tutorial/model-tree-structures-with-materialized-paths/

Jesus Gomez
  • 1,460
  • 13
  • 30
  • What if we needed to do a boost by the type of one of the categories at query time? Do you have any recommendations on that? – TheNastyOne Aug 15 '19 at 22:53
  • @TheNastyOne we had a similar use case, we needed to display the full tree, so we decided to store this : - path : 'root|books|fiction' - level_1 : root - level_2 : books - level_3 : fiction - level_x, etc, etc, ... – misterone Jun 29 '21 at 22:23