tags:

views:

19

answers:

1

I'm building a quite thorough searching mechanism for a zope site. There are lots of different ways of searching, and because it might want to search for multiple values on the same index (and match all of them) I need to do it using AdvanceQuery. I've built my queries like this:

if self.text():
    text_query = And()
    for t in self.text():
        text_query.addSubquery(Eq('SearchableText',t))
if self.sector()
    sector_query = And()
    for s in self.sector()
        sector_query.addSubquery(Eq('sector',s))
if self.region():
    region_query = Eq('region',self.region())
if self.role():
    role_query = Eq('role',self.role())

self.text() etc. are defined elsewhere and will return False if the query doesn't exist, and self.text() and self.sector() always produce a list even if there is only a single value so no worries there.

I also know how to do the last bit e.g.

return self.context.portal_catalog.evalAdvancedQuery(query)

What I cannot figure out is how to stitch it together to define 'query'. If I do something like this it breaks if not all of them are present:

query = text_query & sector_query & region_query & role_query

Bear in mind this probably isn't the complete list of variables to search using so where looking at hundreds of possible combinations. How can I define 'query' conditionally so it doesn't break?

A: 

As I said in the comment, using the &= within the if statement seems to do the trick

chrism