Tree mode is optimized for academic papers and technical documents with deep heading hierarchies. It uses a process of anchor retrieval → tree walk → path aggregation to provide context-aware results.
When using search_mode="tree", the results include an additional paths field that shows the hierarchical relationship between nodes.
from treesearch import TreeSearch
# Use tree mode for hierarchical documents
ts = TreeSearch("papers/", "docs/")
results = ts.search("experimental method", search_mode="tree")
# Standard node results
for doc in results["documents"]:
for node in doc["nodes"]:
print(f"[{node['score']:.2f}] {node['title']}")
# Hierarchical path results
for path in results["paths"]:
chain = " > ".join(p["title"] for p in path["path"])
print(f"[{path['score']:.2f}] {chain}")
print(f" {path['snippet'][:200]}")
Choosing a Search Mode
| Mode | Best For | Description |
|---|
"auto" (Default) | General use | Automatically chooses between tree and flat based on document types and depth. |
"tree" | Papers & Docs | Best for documents with clear heading hierarchies (e.g., Markdown, JSON). |
"flat" | Code Search | Best for keyword-dense, non-hierarchical files (e.g., Python, JS, C++). |
ts = TreeSearch("papers/", "docs/")
results = ts.search("实验方法", search_mode="tree")
for path in results["paths"]:
chain = " > ".join(p["title"] for p in path["path"])
print(f"[{path['score']:.2f}] {chain}")
print(f" {path['snippet'][:200]}")