2017-03-31 6 views
0

私は、各投稿に記載されたユーザの配列を持つ一連の文書(投稿)を持っています。集約アレイの出現

{ 
    "title": "Some post title", 
    [ ... ] 
    "mentions": ["johnsmith", "johndoe", "paul"] 
} 

一意の言葉のリストと、それらがすべての投稿にわたって言及された回数を集計するとします。たとえば:Elasticsearchの等価何

"mentions": [{ 
    "$unwind": "$mentions" 
}, { 
    "$group": { 
     "_id": "$mentions", 
     "count": { "$sum": 1 } 
    } 
}] 

:モンゴで

[{ user: "johnsmith", count: 5 }, { user: "benlewis", count: 9 }, { user: "johndoe", count: 1 }] 

、私のような何かをしたいですか?

答えて

0

これにはTermsアグリゲーションを使用できます。小さな(5.xの)例:

PUT test 
{ 
    "mappings": { 
    "test" : { 
     "properties": { 
     "title": { 
      "type": "text" 
     }, 
     "mentions": { 
      "type": "keyword" 
     } 
     } 
    } 
    } 
} 

POST test/test/1 
{ 
    "title": "Some post title", 
    "mentions": [ 
    "johnsmith", 
    "johndoe", 
    "paul" 
    ] 
} 

POST test/test/2 
{ 
    "title": "Some post title 2", 
    "mentions": [ 
    "johnsmith" 
    ] 
} 

GET test/_search 
{ 
    "size": 0, 
    "aggs": { 
    "test": { 
     "terms": { 
     "field": "mentions", 
     "size": 10 
     } 
    } 
    } 
} 

次の応答を提供します:

"aggregations": { 
    "test": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
     { 
      "key": "johnsmith", 
      "doc_count": 2 
     }, 
     { 
      "key": "johndoe", 
      "doc_count": 1 
     }, 
     { 
      "key": "paul", 
      "doc_count": 1 
     } 
     ] 
    } 
    } 
} 

・ホープ、このことができます:)