Elasticsearch所有记录中阵列中排名前10的最常用值

时间:2017-06-12 09:39:49

标签: elasticsearch

我有一个索引“test”。文档结构如下所示。每个文档都有一个“标签”数组。我无法弄清楚如何查询此索引以获得前10个最常出现的标签?

此外,如果我们在此索引中拥有超过2mil的文档,应该遵循哪些最佳做法?

{
    "_index" : "test",
    "_type" : "data",
    "_id" : "1412879673545024927_1373991666",
    "_score" : 1.0,
    "_source" : {
      "instagramuserid" : "1373991666",
      "likes_count" : 163,
      "@timestamp" : "2017-06-08T08:52:41.803Z",
      "post" : {
        "created_time" : "1482648403",
        "comments" : {
          "count" : 9
        },
        "user_has_liked" : true,
        "link" : "https://www.instagram.com/p/BObjpPMBWWf/",
        "caption" : {
          "created_time" : "1482648403",
          "from" : {
            "full_name" : "PARAMSahib ™",
            "profile_picture" : "https://scontent.cdninstagram.com/t51.2885-19/s150x150/12750236_1692144537739696_350427084_a.jpg",
            "id" : "1373991666",
            "username" : "parambanana"
          },
          "id" : "17845953787172829",
          "text" : "This feature talks about how to work pastels .\n\nDull gold pullover + saffron khadi kurta + baby pink pants + Deep purple patka and white sneakers - Perfect colours for a Happy sunday christmas morning . \n#paramsahib #men #menswear #mensfashion #mensfashionblog #mensfashionblogger #menswearofficial #menstyle #fashion #fashionfashion #fashionblog #blog #blogger #designer #fashiondesigner #streetstyle #streetfashion #sikh #sikhfashion #singhstreetstyle #sikhdesigner #bearded #indian #indianfashionblog #indiandesigner #international #ootd #lookbook #delhistyleblog #delhifashionblog"
        },
        "type" : "image",
        "tags" : [
          "men",
          "delhifashionblog",
          "menswearofficial",
          "fashiondesigner",
          "singhstreetstyle",
          "fashionblog",
          "mensfashion",
          "fashion",
          "sikhfashion",
          "delhistyleblog",
          "sikhdesigner",
          "indianfashionblog",
          "lookbook",
          "fashionfashion",
          "designer",
          "streetfashion",
          "international",
          "paramsahib",
          "mensfashionblogger",
          "indian",
          "blog",
          "mensfashionblog",
          "menstyle",
          "ootd",
          "indiandesigner",
          "menswear",
          "blogger",
          "sikh",
          "streetstyle",
          "bearded"
        ],
        "filter" : "Normal",
        "attribution" : null,
        "location" : null,
        "id" : "1412879673545024927_1373991666",
        "likes" : {
          "count" : 163
        }
      }
    }
  },

1 个答案:

答案 0 :(得分:3)

如果映射中的标记类型为object(默认情况下),则可以使用如下聚合查询:

{
   "size": 0,
   "aggs": {
      "frequent_tags": {
         "terms": {"field": "post.tags"}
      }
   }
}