2015-08-31 64 views
0

所以我有一堆記錄像{A: x, B: y}用elasticsearch匹配多個屬性

我想構建一個匹配AB attrib的搜索查詢。

但是,在match中再添加1個條件,查詢將無法解析。

This Works。

{ 
    "query" : { 
     "match": { 
      "A": "x" 
     } 
    } 
} 

這沒有。

{ 
    "query" : { 
     "match": { 
      "A": "x", 
      "B": "y" 
     } 
    } 
} 

回答

2

它應該是這樣的:

{ 
    "query": { 
    "bool": { 
     "must": [ 
      { 
      "match": {"A": "x"} 
      }, 
      { 
      "match": {"B": "y"} 
      } 
     ] 
    } 
    } 
} 

使用mustAND你所有的馬tch子句,請使用shouldOR全部匹配子句。

0

使用期限的查詢,試試這個:

{ 
    "query": { 
     "bool": { 
      "should": [ 
       { 
       "term": {"A": "x"} 
       }, 
       { 
       "term": {"B": "y"} 
       } 
      ] 
     } 
    } 
} 

或匹配查詢可以這樣做:

{ 
    "query": { 
     "bool": { 
      "should": [ 
       { 
       "match": {"A": "x"} 
       }, 
       { 
       "match": {"B": "y"} 
       } 
      ] 
     } 
    } 
}