Refactor engine for different types of queries
Add hierarchical query