有条件地将查询包含在 zope 的高级查询中
我正在为 zope 网站建立一个相当彻底的搜索机制。有很多不同的搜索方式,并且因为它可能想要在同一索引上搜索多个值(并匹配所有值),所以我需要使用 AdvanceQuery 来执行此操作。我已经构建了这样的查询:
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() 等在其他地方定义,如果查询不存在,则返回 False,并且 self.text() 和 self.sector() 总是生成一个列表,即使存在只是一个值,所以不用担心。
我也知道如何做最后一点,例如
return self.context.portal_catalog.evalAdvancedQuery(query)
我不知道如何将其拼接在一起来定义“查询”。如果我做这样的事情,如果不是所有的都存在,它就会中断:
query = text_query & sector_query & region_query & role_query
请记住,这可能不是要搜索的完整变量列表,因此要查看数百种可能的组合。如何有条件地定义“查询”以免中断?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我在评论中所说,在 if 语句中使用 &= 似乎可以解决问题
As I said in the comment, using the &= within the if statement seems to do the trick