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.

I used `org.apache.lucene.util.QueryBuilder` as a template for refactoring this.