Algorithm to search tree-like structured relational data (use example: elasticsearch)

Viewed 124

Question 1: I have a tree-like data structure to search items, e.g.,

living creatures
      /  \
    bird  fish
    / \    / \
canary Penguin

I am looking for a way to switch on/off the search results. For example, if bird is disabled, when a search is performed against the term "bird", bird and its children should not be appeared in the results. If bird is enabled, when the term "bird" is searched, bird (or bird and its children) should be appeared.

Question 2 Right now, I am putting this tree structure as a flatten list in the Elastic search as a field, what is the best way to make it a tree strucutre (I know I can use a nested fields but considering we have 30 levels depth and 100,000 children and most documents only have a few children.

Any comments are welcome.

1 Answers

You needn't use nesting in this case, you can use your flatten list, make the field at lowest level as your index/primary key and each of its parent as a field, so your entries will become

"canary":{
    "l1":"living creatures",
    "l2":"carnivore",
    "l3":"bird",
}
"dolphin":{
    "l1":"living creatures",
    "l2":"fish",
}
"Penguin":{
    "l1":"living creatures",
    "l2":"fish, bird",
    "##assuming penguin is both
}

Then when u need to disable the entry, you can simply disable any document that has bird in it, of course data is repeated but that's noSQL

Related