Optimize fulltext searches by flattening and grouping nodes