search term parsing like Google/Gmail

Robert Kern robert.kern at gmail.com
Thu Sep 10 13:29:53 EDT 2009


On 2009-09-09 16:37 PM, Randy Syring wrote:
> I am looking for a string parser that works kind of like Google's or
> Gmail's advanced search capabilities. So it would turn something like this:
>
>     (subject:"hi there" from:[tim, tom, -fred]) or (subject:foobar from:sam)
>
> into a python structure that could be used. I don't really care so much
> about the search syntax except that easy and intuitive is best for
> users. Does something like this exist?

The Whoosh search library uses pyparsing to parse its queries. It does not 
handle the [] brackets, but it handles the rest. I am sure that Matt Chaput 
would accept a contribution of such an enhancement, though.

   http://whoosh.ca/

In [9]: from whoosh import qparser

In [10]: p = qparser.QueryParser('body')

In [11]: p.parse(u'subject:"hi there" or (subject:foobar from:sam)')
Out[11]: Or([Phrase(u'subject', [u'hi', u'there']), And([Term(u'subject', 
u'foobar', boost=1.0), Term(u'from', u'sam', boost=1.0)])])

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list