Refactor get_query_entities