2017-08-07 69 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

明白了。我編輯了我的答案以滿足您的要求。 –