2017-08-07 67 views
0

我有一个聚集查询像下面过滤掉elasticserach聚集水桶空键

"subjectArea.untouched" : { 
    "terms" : { 
    "field" : "subjectArea.untouched", 
    "size" : 10, 
    "exclude" : "" //to exclude buckets with empty string key 
    } 
} 

但结果并不如预期

"aggregations" : { 
"subjectArea.untouched" : { 
    "doc_count_error_upper_bound" : 0, 
    "sum_other_doc_count" : 0, 
    "buckets" : [ { 
    "key" : "", //Not expecting this bucket 
    "doc_count" : 13 
    }, { 
    "key" : "subjectArea", 
    "doc_count" : 1 
    }, { 
    "key" : "test1000", 
    "doc_count" : 1 
    } ] 
} 
} 

我不想在结果中第一桶。有人能帮我吗?

回答

1

您需要排除筛选器部分查询中空字符串的文档。 为您的使用情况,查询将看起来是这样的:

{ 
    "query": { 
    "bool": { 
     "must_not": [ 
     { 
      "match": { 
      "subjectArea.untouched": "" 
      } 
     } 
     ] 
    } 
    }, 
    "aggs": { 
    "subjectArea.untouched": { 
     "terms": { 
     "field": "subjectArea.untouched", 
     "size": 10 
     } 
    } 
    }, 
    "size": 0 
} 

基本上查询的第一部分表现为SQL中的WHERE 条款

编辑: 要过滤掉只是桶(没有过滤掉的文件),你应该使用filter aggregations。 查询结果应类似于:

{ 
    "aggs": { 
    "filter_out": { 
     "filter": { 
     "bool": { 
      "must_not": { 
      "match": { 
       "subjectArea.untouched": "" 
      } 
      } 
     } 
     }, 
     "aggs": { 
     "subjectArea.untouched": { 
      "terms": { 
      "field": "subjectArea.untouched", 
      "size": 10 
      } 
     } 
     } 
    } 
    }, 
    "size": 0 
} 
+0

感谢您的回答。我会查的。 – MrG

+0

如果我添加'must_not'过滤器,那么我将无法获得'subjectArea.untouched'为空的那些文档。但我想要那些。我不想要的是在空字符串键的聚合桶。我清楚了吗?你能带些灯吗? – MrG

+0

明白了。我编辑了我的答案以满足您的要求。 –