如何使用TermQuery跨多个术语进行AND查询?

时间:2012-07-31 14:33:57

标签: python elasticsearch pyes

我正在使用pyes Python库进行elasticsearch。

假设我想找到title =“Rainbow”AND artist =“Kermit”的所有文件。以下文件返回title =“Rainbow”OR artist =“Kermit”的文档。如何将其更改为AND?

query = pyes.TermQuery()
query.add("title", "rainbow")
query.add("artist","kermit")
search = pyes.Search(query)
results = conn.search(search, index_name, doc_type)

1 个答案:

答案 0 :(得分:3)

答案是将两个TermQuery包装在BooleanQuery

q1 = pyes.TermQuery("title", "rainbow")
q2 = pyes.TermQuery("artist", "kermit")
query = BooleanQuery(must=[q1, q2])
search = pyes.Search(query)
results = conn.search(search, index_name, doc_type)