2016-12-05 65 views
3

我試圖在通過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) 

任何意見將不勝感激。 沒有,似乎工作。

回答

1

請確保年齡字段的類型是整數或字符串。將解決您的問題的關鍵字是must

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

善舉upvote ... – harshil9968

1

用Bool和「must」回答。