def write_sample_and_index_files(outpath: str, elems):
    """Given an outpath and a query result (elems), write the texts in the query
    result to outpath, write the indices in the query result to index_filepath.

    Return the index_filepath.
    """
    index_filepath = f'{outpath}.index'
    with open(outpath, 'w') as f, open(index_filepath, 'w') as index:
        for e in elems:
            f.write(f"{e.text}\n")
            index.write(f"{e.id}\n")
    return index_filepath