2015-04-29 218 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" 
      } 
     } 
     ] 
    } 
}