KeyError on highlighting when using search with terms=True

Issue #364 resolved
created an issue

When highlighting results by hit.highlights("content", body), KeyError: 0L is raised. This happens only if the search was invoked with terms=True parameter and search query is simple "*".

The error can be reproduced with following script:

from whoosh.index import create_in
from whoosh.fields import *
from whoosh.qparser import QueryParser
schema = Schema(title=TEXT(stored=True), content=TEXT)
#create index and fill it with sample data
ix = create_in("d:/Projects/tmp/indexdir/", schema)
writer = ix.writer()
writer.add_document(title=u"First document", content=u"Dance")
writer.add_document(title=u"Second document", content=u"Avidly")

#perform search
with ix.searcher() as searcher:
    query = QueryParser("content", ix.schema).parse("*")
    results =, terms=True)
    #if this line is uncommented and used instead, no error is raised
    #results =
    for hit in results:
        print results[0]
        print hit.highlights("content", u"Actual body irrelevant.")

Full traceback:

Traceback (most recent call last):
  File "D:\Projects\tmp\", line 35, in <module>
    print hit.highlights("content", u"Actual body irrelevant.")
  File "D:\Projects\tmp\whoosh\", line 1450, in highlights
  File "D:\Projects\tmp\whoosh\", line 872, in highlight_hit
    bterms = (term for term in hitobj.matched_terms()
  File "D:\Projects\tmp\whoosh\", line 1412, in matched_terms
    return self.results.docterms[self.docnum]
KeyError: 0L

If you do not pass the terms=True, the highlighting proceeds fine.

I'm using Whoosh 2.5.4 along with Python 2.7 x32 on W7 x64.

Comments (2)

  1. Log in to comment