1. Matt Chaput
  2. whoosh
  3. Issues
Issue #365 new

Prefix as last term for ajax autocomplete queries

Daniel Black
created an issue

I'm using whoosh to as a backend to autocomplete ajax search queries with the full complement of default QueryParser plugins however I wanted the last term to be a PrefixTerm as the user probably hasn't finished typing what they want.

It turns out the plugin is exceptionally easy and as its undoubtedly useful elsewhere here's the code.

If you're interested in merging, I'll include a full documentation and test cases for it.

# This makes the query parser enforce the last keyword to a PrefixTerm
class PrefixLastPlugin(PrefixPlugin):

    expr=r"(?P<text>[^ \t\r\n*]+)(?=$)"
In [50]: parser = QueryParser(None, ix.schema, termclass=query.Variations)
In [51]: parser.add_plugin(MultifieldPlugin(['title', 'content', 'path']))
In [54]: parser.add_plugin(PrefixLastPlugin())
In [55]: parser.parse("dog AND cat")
Out[55]: And([Or([Variations('title', u'dog'), Variations('content', u'dog'), Variations('path', u'dog')]), Or([Prefix('title', u'cat'), Prefix('content', u'cat'), Prefix('path', u'cat')])])

Comments (0)

  1. Log in to comment