1. Matt Chaput
  2. whoosh
  3. Issues
Issue #338 resolved

Date Ranges With matched_terms results in KeyError

created an issue

This seems similar to what I saw and reported in issue #332 which affected only 2.4.1, but this new discovery also affects 2.5.1.

If a date range is searched and hit.matched_terms() is called on a hit, it results in a key error.

Below is code to demonstrate.

from whoosh import analysis, fields, index, qparser, query, searching, scoring
from whoosh.qparser import GtLtPlugin
from whoosh.filedb.filestore import RamStorage
from datetime import datetime
schema = fields.Schema(id=fields.KEYWORD(stored=True), body=fields.TEXT, num=fields.NUMERIC(stored=True,unique=True),created=fields.DATETIME(stored=True))
ix = RamStorage().create_index(schema)

with ix.writer() as w:
    w.add_document(id=u"one", body=u"this and this", num='5', created=datetime.now())
    w.add_document(id=u"three", body=u"that and that", num='7', created=datetime.now())
    w.add_document(id=u"two", body=u"this and that", num='6', created=datetime.now())

with ix.searcher() as s:
    parser = qparser.QueryParser("body", ix.schema)
    query = parser.parse(u"created:>='2013-07-01'")
    r = s.search(query, terms=True)

    for h in r:
        print h
        print h.matched_terms()

Remove the final h.matched_terms() to print all hits without errors.

Affects 2.5.1 and below.

Comments (2)

  1. rholloway reporter

    I also tried to test without using GtLtPlugin to see if it had to do with that, but I couldn't get the search to work at all without it. Query equivalent was

    created:[01 july 2013 to]

    but this results in

    ValueError: invalid literal for int() with base 10: '01ju'

    Personally I am using GtLtPlugin and prefer that format a lot more, but just thought I would point this out as syntax for non-GtLtPlugin query was taken directly from documentation.

  2. Log in to comment