{"id":8705,"date":"2012-09-25T23:02:39","date_gmt":"2012-09-25T17:32:39","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8705"},"modified":"2022-01-12T19:01:00","modified_gmt":"2022-01-12T13:31:00","slug":"redis-tag-cloud-using-redis","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/redis-tag-cloud-using-redis\/","title":{"rendered":"Redis: Tag Cloud using Redis"},"content":{"rendered":"<p>I would like to share a simple and efficient way of creating tagclouds that I discovered very recently. A Tag Cloud is a pictorial representation of some tags\/text. The size of the tag\/text is drirectly proportional to the weightage of that tag\/text. A sample tag cloud could be seen on our blogs site.<\/p>\n<p>How to do it?<\/p>\n<p>[java]A very simple way of making it possible is just by making a Map having the Tag as key and its occurrences as the value. [\/java]<\/p>\n<p>The presence of redis makes me think that it is not at all taxing for my poor server.<\/p>\n<p>Redis provides a data-structure called Sorted Set. In Sorted sets, every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated.<\/p>\n<p>We can use the Sorted Set to contain the Tags as value and their corresponding weightage as score. This way we have a Sorted Set of all the Tags. We may choose to select top 50 Tags from the Sorted Set. <\/p>\n<p>Prerequisite: You must have Redis installed and running on your machine. Here is a link to Redis site showing how to do it http:\/\/redis.io\/download<\/p>\n<p>Here is an example of showing top 50 tags from Blog domain class.<\/p>\n<p>[java]<br \/>\nclass Blog{<br \/>\n  String content<br \/>\n  String title<br \/>\n  static hasMany = [categories: String]<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>The code for generating the tagCloud is:<\/p>\n<p>[java]<br \/>\n Jedis redis = new Jedis(&quot;localhost&quot;);<br \/>\n    redis.connect();<\/p>\n<p> for (def blog: Blog.list()) {<br \/>\n                        blog.categories.each {catgry -&gt;<br \/>\n                            redis.zincrby(&#8216;tag-cloud-sorted-set&#8217;, 1, &quot;${catgry}&quot;)<br \/>\n                        }<br \/>\n                    }<\/p>\n<p> Map tagCloud = [:]<br \/>\n        Set&lt;String&gt; categories = redis.zrevrange(&#8216;tag-cloud-sorted-set&#8217;, 0, 50)<br \/>\n        int size = 30<br \/>\n        categories.eachWithIndex {catgry, idx -&gt;<br \/>\n            if (idx % 15 == 0) {<br \/>\n                size = size &#8211; 3<br \/>\n            }<br \/>\n            tagCloud.put(catgry, size.toString())<br \/>\n        }<br \/>\n   return tagCloud<\/p>\n<p>[\/java]<\/p>\n<p>Once we get the Map of tagCloud we can render it as follows:<br \/>\n[java]<br \/>\n \t\t&lt;g:each in=&quot;${tagCloud.keySet()}&quot; var=&quot;key&quot; status=&quot;idx&quot;&gt;<br \/>\n                    &lt;g:link action=&quot;search&quot; controller=&quot;feed&quot; params=&quot;[category: key]&quot;<br \/>\n                            style=&quot;font-size: ${tagCloud.get(key)}px;&quot;&gt;${key}&lt;\/g:link&gt;<br \/>\n                &lt;\/g:each&gt;<br \/>\n[\/java]<br \/>\n<!--more--><br \/>\nHope this helps!!!<br \/>\n<!--more--><br \/>\nBest Regards<br \/>\nMohd Farid<br \/>\nfarid@intelligrape.com<br \/>\n@faridiflex<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I would like to share a simple and efficient way of creating tagclouds that I discovered very recently. A Tag Cloud is a pictorial representation of some tags\/text. The size of the tag\/text is drirectly proportional to the weightage of that tag\/text. A sample tag cloud could be seen on our blogs site. How to [&hellip;]<\/p>\n","protected":false},"author":26,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[4840,857,1081,1080],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8705"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8705"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8705\/revisions"}],"predecessor-version":[{"id":54609,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8705\/revisions\/54609"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}