简体   繁体   中英

Best structure for storing tree in Elasticsearch?

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?

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM