Akhil Mordia
Akhil Mordia

Reputation: 142

Elasticsearch Top 10 Most Frequent Values In Array Across All Records

I have an index "test". Document structure is as shown below. Each document has an array of "tags". I am not able to figure out how to query this index to get top 10 most frequently occurring tags?

Also, what are the best practices one should follow if we have more than 2mil docs in this index?

{
    "_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
        }
      }
    }
  },

Upvotes: 7

Views: 3328

Answers (1)

Mohammad Mazraeh
Mohammad Mazraeh

Reputation: 1074

If your tags type in mapping is object (which is by default) you can use an aggregation query like this:

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

Upvotes: 7

Related Questions