2014-12-18 68 views
9

我遇到了一個問題,即elasticsearch無法通過在嵌套字段上使用聚合條件來返回唯一文檔的計數。如何使用elasticsearch聚合返回唯一文檔的計數

這裏是我們的模型的例子:

{ 
    ..., 
    "location" : [ 
     {"city" : "new york", "state" : "ny"}, 
     {"city" : "woodbury", "state" : "ny"}, 
     ... 
    ], 
    ... 
} 

我想做的狀態領域聚集,但這個文件將在「NY」鬥,因爲「紐約」中出現兩次被計算兩次文件。

所以我想知道是否有辦法抓住不同文件的計數。

映射:

people = { 
    :properties => { 
    :location => { 
     :type => 'nested', 
     :properties => { 
     :city => { 
      :type => 'string', 
      :index => 'not_analyzed', 
     }, 
     :state => { 
      :type => 'string', 
      :index => 'not_analyzed', 
     }, 
     } 
    }, 
    :last_name => { 
     :type => 'string', 
     :index => 'not_analyzed' 
    } 
    } 
} 

查詢是很簡單:

curl -XGET 'http://localhost:9200/people/_search?pretty&search_type=count' -d '{ 
    "query" : { 
    "bool" : { 
     "must" : [ 
     {"term" : {"last_name" : "smith"}} 
     ] 
    } 
    }, 
    "aggs" : { 
    "location" : { 
     "nested" : { 
     "path" : "location" 
     }, 
     "aggs" : { 
     "state" : { 
      "terms" : {"field" : "location.state", "size" : 10} 
     } 
     } 
    } 
    } 
}' 

響應:

{ 
    "took" : 104, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 1248513, 
    "max_score" : 0.0, 
    "hits" : [ ] 
    }, 
    "aggregations" : { 
    "location" : { 
     "doc_count" : 2107012, 
     "state" : { 
     "buckets" : [ { 
      "key" : 6, 
      "key_as_string" : "6", 
      "doc_count" : 214754 
     }, { 
      "key" : 12, 
      "key_as_string" : "12", 
      "doc_count" : 168887 
     }, { 
      "key" : 48, 
      "key_as_string" : "48", 
      "doc_count" : 101333 
     } ] 
     } 
    } 
    } 
} 

的doc_count比命中總大得多。所以必須有重複。

謝謝!

+0

發佈您的索引和您正在使用的查詢的映射,否則我無法幫助您。 –

+0

@AndreiStefan我更新了映射和查詢。謝謝! – milodky

回答

12

我想你需要一個reverse_nested聚集,因爲你要根據嵌套值聚集,但實際上計數的ROOT文件,而不是嵌套的人

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "term": { 
      "last_name": "smith" 
      } 
     } 
     ] 
    } 
    }, 
    "aggs": { 
    "location": { 
     "nested": { 
     "path": "location" 
     }, 
     "aggs": { 
     "state": { 
      "terms": { 
      "field": "location.state", 
      "size": 10 
      }, 
      "aggs": { 
      "top_reverse_nested": { 
       "reverse_nested": {} 
      } 
      } 
     } 
     } 
    } 
    } 
} 

而且,作爲一個結果,你會看到什麼像這樣:

"aggregations": { 
     "location": { 
     "doc_count": 6, 
     "state": { 
      "doc_count_error_upper_bound": 0, 
      "sum_other_doc_count": 0, 
      "buckets": [ 
       { 
        "key": "ny", 
        "doc_count": 4, 
        "top_reverse_nested": { 
        "doc_count": 2 
        } 
       }, 
       { 
        "key": "ca", 
        "doc_count": 2, 
        "top_reverse_nested": { 
        "doc_count": 2 
        } 
       } 
      ] 
     } 
     } 
    } 

你在找什麼是根據top_reverse_nested部分。 這裏有一點:如果我沒有誤會"doc_count": 6是NESTED文檔計數,所以不要對這些數字感到困惑,因爲您認爲您在計算根文檔,計數在嵌套文檔上。因此,對於三個匹配的嵌套文檔,計數將爲3,而不是1.

相關問題