Fix query escaping and filter out empty pattern results.