2016-08-24 55 views
1

在elasticsearch中,我想獲得獨特的條款和計數條款。獲取獨特條款的計數

 
index: 
doc1: filetype(multivalued): pdf, zip, zip, txt 

expected result: 
pdf: 1 
zip: 2 
txt: 1 

我提出了這個查詢,但我得到了一個錯誤。

"aggs": { 
    "uniqueTerms": { 
     "terms": { 
      "field": "filetype" 
     } 
    }, 
    "aggs": { 
     "count": { 
      "cardinality": { 
       "field": "filetype" 
      } 
     } 
    } 
} 

我該如何進行查詢以獲得結果?

指數映射更新:

"file" : { 
    "properties" : { 
     "filetype" : { 
      "type" : "string", 
      "norms" : { 
       "enabled" : false 
      }, 
      "fields" : { 
        "raw" : { 
         "type" : "string", 
         "index" : "not_analyzed", 
         "ignore_above" : 256 
        } 
       } 
      } 
     } 
} 

回答

0

良好的開端,你幾乎沒有。您只需將cardinality聚合嵌套更深一層。

{ 
    "aggs": { 
    "uniqueTerms": { 
     "terms": { 
     "field": "filetype" 
     }, 
     "aggs": { 
     "count": { 
      "cardinality": { 
      "field": "filetype" 
      } 
     } 
     } 
    } 
    } 
} 
+0

謝謝,但它看起來得到文件count.I需要計數每個獨特的條款。 – user3413814

+0

查詢使這個結果,pdf:1,zip:1,txt:1 – user3413814

+0

對不起,我已更新我的回答 – Val

0

我改變了我的索引映射這樣的:

... 
"file": { 
    "type" : "nested" 
} 
... 

「文件」 是一個數組,包含文件信息。

,然後,我做了這個查詢:

"aggs": { 
    "1": { 
     "nested": { 
      "path": "file" 
     }, 
     "aggs": { 
      "2": { 
       "terms": { 
        "field": "file.file_type.raw" 
       } 
      } 
     } 
    } 
} 

查詢工作正常。