2013-04-11 42 views
13

我想获得一个查询,它可以返回满足两个过滤条件的json。我试图让已进料中的名为“测试1”一个字段的响应,它也应该缺少一个字段“test2的”,我已经尝试了查询在单个查询中使用缺少和存在的过滤器

{ 
"query": { 
    "filtered": { 
    "query": { 
    "match_all": {} 
    }, 
     "filter": { 
     "missing": { 
     "field": "test2" 
     }, 
     "exists": { 
     "field": "test1" 
     } 
     } 
    } 
    } 
} 

上述查询将返回其拥有的所有字段字段“test1”,并且它还返回缺少字段“test2”的提要,我试图缩小响应范围,因为我只希望提供满足这两个条件的提要。

回答

24

可以使用bool filter将两个或多个过滤器:

{ 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": {} 
     }, 
     "filter": { 
     "bool": { 
      "must": [{ 
      "missing": { 
       "field": "test2" 
      } 
      }, { 
      "exists": { 
       "field": "test1" 
      } 
      }] 
     } 
     } 
    } 
    } 
} 
+0

这作品!非常感谢!!!! :) – sam 2013-04-11 12:29:52