Prefix as last term for ajax autocomplete queries
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 : parser = QueryParser(None, ix.schema, termclass=query.Variations) In : parser.add_plugin(MultifieldPlugin(['title', 'content', 'path'])) In : parser.add_plugin(PrefixLastPlugin()) In : parser.parse("dog AND cat") Out: 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')])])