2015-04-29 265 views
0

我是新來的彈性搜索。我的users表中有一個字段名clearance,我試圖根據這個過濾我的結果。彈性搜索 - 在單個字段中搜索多個查詢

match: { 
      clearance: { 
      query: 'None', 
      type: 'phrase' 
      } 
     } 

當我給上述匹配查詢我得到3個結果。我試圖得到的是再傳遞一個字符串以及None。例如,我想找到清除用戶NoneFirst Level

我試過了。

multi_match: { 
       clearance: { 
       query: 'None OR First Level', 
       type: 'phrase' 
       } 
      } 

但結束了一些錯誤。請幫忙。如果我的問題不對,請糾正我。

回答

0

一種方法是將清理作爲映射中的not_analyzed字段並使用條件過濾器。

實施例:

PUT test 
{ 
    "mappings": { 
    "e1":{ 
     "properties": { 
     "clearance":{ 
      "type": "string", 
      "index": "not_analyzed" 
     } 
     } 
    } 
    } 
} 

一些測試數據:

PUT test/e1/1 
{ 
    "clearance":"None" 
} 
PUT test/e1/2 
{ 
    "clearance":"First Level" 
} 
PUT test/e1/3 
{ 
    "clearance":"Second Level" 
} 

查閱查詢部分:

GET test/e1/_search 
{ 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": {} 
     }, 
     "filter": { 
     "terms": { 
      "clearance": [ 
      "None", 
      "First Level" 
      ], 
      "execution": "or" 
     } 
     } 
    } 
    } 
} 

結果verfication:

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 2, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "test", 
      "_type": "e1", 
      "_id": "1", 
      "_score": 1, 
      "_source": { 
       "clearance": "None" 
      } 
     }, 
     { 
      "_index": "test", 
      "_type": "e1", 
      "_id": "2", 
      "_score": 1, 
      "_source": { 
       "clearance": "First Level" 
      } 
     } 
     ] 
    } 
}