How to match exact phrase using xapian and python?

578 Views Asked by At

This is my code:

db = xapian.Database(path/to/database)
enquire = xapian.Enquire
stemmer = xapian.Stem(<supported language>)
query_parser = xapian.QueryParser()
query_parser.set_database(db)
query_parser.set_stemmer(stemmer)
query_parser.set_default_op(xapian.query.OP_OR)
xapian_flags = xapian.QueryParser.FLAG_BOOLEAN | xapian.QueryParser.FLAG_SYNONYM | xapian.QueryParser.FLAG_LOVEHATE
query = query_parser.parse_query('"this exact phrase"', xapian_flags)
enquiry.set_query(query)

This isn't matching "this exact phrase" (I am able to achieve pretty much everything but exact matches). Note that I've included the double quotes mentioned in the documentation. Is there a way of achieving this?

1

There are 1 best solutions below

0
On BEST ANSWER

By explicitly setting the flags to the query parser you override the default of FLAG_PHRASE | FLAG_LOVEHATE | FLAG_BOOLEAN. What you've done therefore is to turn on synonym support but turn off phrase searching, which is what the double quotes relies on.

Note that phrase searching isn't strictly the same as exact matching, although without more context it's difficult to advise if this is the wrong approach to take for your situation.