2016-02-20 22 views
2

我正在嘗試做一些聚合查詢和encouter一些問題。elasticsearch aggs返回錯誤的計數編號

GET /my_index/_search 
{ 
"size" : 0, 
"aggs":{ 
    "group_by":{ 
     "terms": { 
      "field" : "category" 
     } 
    } 
    } 
    } 

這回我:

"hits": { 
    "total": 180, 
    "max_score": 0, 
    "hits": [] 
}, 
"aggregations": { 
    "group_by": { 
    "doc_count_error_upper_bound": 0, 
    "sum_other_doc_count": 1, 
    "buckets": [ 
     { 
      "key": "pf_rd_m", 
      "doc_count": 139 
     }, 
     { 
      "key": "other", 
      "doc_count": 13 
     }, 
     { 
      "key": "_encoding", 
      "doc_count": 12 
     }, 
     { 
      "key": "ie", 
      "doc_count": 10 
     }, 
     { 
      "key": "cadeaux", 
      "doc_count": 2 
     }, 
     { 
      "key": "cartes", 
      "doc_count": 2 
     }, 
     { 
      "key": "cheques", 
      "doc_count": 2 
     }, 
     { 
      "key": "home", 
      "doc_count": 2 
     }, 
     { 
      "key": "nav_logo", 
      "doc_count": 1 
     }, 
     { 
      "key": "ref", 
      "doc_count": 1 
     } 
    ] 
    } 

}

,你可以看到,這告訴我,有180個文件,但如果我做的每一個關鍵的doc_count的總和在我的水桶,我覺得更多的元素......

這肯定做elasticsearch標記化mecanism(https://www.elastic.co/guide/en/elasticsearch/guide/current/aggregations-and-analysis.html

所以我tryed解決方案在這個ES職位,但仍然不工作。這裏是我的地圖

"properties":{ 
          "status":{ 
           "type":"integer", 
           "index":"analyzed" 
          }, 
          "category":{ 
           "type":"string", 
           "fields": { 
           "raw" : { 
            "type": "string", 
            "index": "not_analyzed" 
           } 
           } 
          }, 
          "dynamic_templates": [ 
           { "notanalyzed": { 
             "match":    "*", 
             "match_mapping_type": "string", 
             "mapping": { 
              "type":  "string", 
              "index":  "not_analyzed" 
             } 
            } 
           } 
           ] 
          } 

正如你所看到的,我有一個名爲「category」的字段。並將「raw」添加爲not_analyze字符串,但仍然返回錯誤的數字。

當我試試這個:

GET /my_index/_search 
{ 
"size" : 0, 
"aggs":{ 
    "group_by":{ 
     "terms": { 
      "field" : "category.raw" 
     } 
     } 
    } 
    } 

這將返回:

"hits": { 
    "total": 180, 
    "max_score": 0, 
    "hits": [] 
}, 
"aggregations": { 
    "group_by": { 
    "doc_count_error_upper_bound": 0, 
    "sum_other_doc_count": 0, 
    "buckets": [] 
    } 
} 

這是非常奇怪的。任何幫助?

回答

1

documentation描述,

文檔計數(和任意子聚合的結果)中的條款聚合不總是準確的。這是因爲每個碎片提供了自己的術語的排序列表應該是什麼看法和這些被合併以給出最終意見

爲了克服資源爲代價的這個問題,可以用碎片的尺寸參數。
再次,從文檔:
碎片大小

越高所請求的大小是,更準確的結果將是,還可以,更昂貴的這將是計算最終結果(這兩者都是由於在碎片級別上管理的較大優先級隊列以及節點和客戶端之間的較大數據傳輸)。 可以使用參數shard_size最大限度地減少請求尺寸更大的額外工作。定義後,它將確定協調節點將從每個分片請求多少個項。一旦所有分片都響應,協調節點會將它們減少到基於size參數的最終結果 - 這樣,可以提高返回項的準確性並避免將大量分組流回給客戶。如果設置爲0,則shard_size將設置爲Integer.MAX_VALUE

如果碎片尺寸參數添加到查詢:

GET /my_index/_search 
{ 
"size" : 0, 
"aggs":{ 
    "group_by":{ 
     "terms": { 
      "field" : "category.raw", 
      "shard_size" : 0 
     } 
     } 
    } 
    } 
+0

謝謝您的答覆。我試了你的答案,但查詢返回給我一個arror。 解析失敗[無元素[shard_size]的解析器]]; }]「, 」status「:400 –

+0

所以對不起,應該在聚合定義中。 – Hkntn

+0

我怎麼能定義這個聚合?在我的映射或者其他地方?謝謝 –