Refactor term analysis for simple_query_string prefix queries

Also fixes an issue where the token stream could be non-closed if an
exception occured.