Elasticsearch - Python客户端 - 如何匹配多个字段?

问题描述:

我试图在通过Python API查询ES服务器时在多个字段上进行匹配。但无法弄清楚Python中的语法:Elasticsearch - Python客户端 - 如何匹配多个字段?

我试过了;

res = es.search(index="pyats", doc_type="router_show", body={"query": {"match": {"name": "mark"} AND {"age": "21"}}}, size=1000) 

res = es.search(index="pyats", doc_type="router_show", body={"query": {"match_all": {"name": "mark"} AND {"age": "21"}}}, size=1000) 

res = es.search(index="pyats", doc_type="router_show", body={"query": {"match": {"name": "mark"}}, {"match": {"age": "21"}}}, size=1000) 

任何意见将不胜感激。 没有,似乎工作。

请确保年龄字段的类型是整数或字符串。将解决您的问题的关键字是must

{ 
    "query": { 
     "constant_score" : { 
      "filter" : { 
       "bool" : { 
        "must" : [ 
         { "term" : { "age" : 21 } }, 
         { "term" : { "name" : "mark" } } 
        ] 
       } 
      } 
     } 
    } 
} 
+0

善举upvote ... – harshil9968

用Bool和“must”回答。