2016-09-15 180 views
3

我對彈性搜索非常陌生,想編寫一個關注兩個字段的查詢。我的意思是這些字段的內容包含指定的子字符串。我有一個包含字段的文檔,像這樣:用多個字段匹配查詢

name: n 
tag: t 

我嘗試這樣做:

/_search -d ' 
{ 
    "query": { 
     "match": { 
      "name": "n", 
      "tag": "t" 
     } 
    } 
} 

但查詢結果中出現以下錯誤:

[match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its 'options' form, with 'query' element?

有沒有辦法做到這在elasticsearch?

回答

7

您需要封閉在一個bool/must查詢的兩個match查詢,像這樣:

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "match": { 
      "name": "n" 
      } 
     }, 
     { 
      "match": { 
      "tag": "t" 
      } 
     } 
     ] 
    } 
    } 
}