{"id":1085,"date":"2013-01-18T10:19:17","date_gmt":"2013-01-18T09:19:17","guid":{"rendered":"https:\/\/www.unicoda.com\/?p=1085"},"modified":"2013-01-18T10:27:01","modified_gmt":"2013-01-18T09:27:01","slug":"node-elastical-implement-stats-function","status":"publish","type":"post","link":"https:\/\/www.unicoda.com\/?p=1085","title":{"rendered":"Node-Elastical: Implement stats function"},"content":{"rendered":"<p>Recently, I had to find the size of an index in Elasticsearch. I could have just built my query using request like this:<\/p>\n<pre>var request = require('request');\r\nrequest.get('http:\/\/localhost:9200\/' + myIndex + '\/_stats'\r\n  , function (err, res, body) {\r\n    console.log(res._all.primaries.store.size_in_bytes);\r\n});<\/pre>\n<p>Fast and easy. Since it&rsquo;s not available yet in node-elastical, I found it better to add this functionality so that other people can use it. Implementing the stats function wasn&rsquo;t difficult. It just require parsing options to create the right url and then making the request (with request) to Elasticsearch. I used the <a href=\"http:\/\/www.elasticsearch.org\/guide\/reference\/api\/admin-indices-stats.html\">Indices Stats API documentation<\/a> to create the function.<\/p>\n<p>In file <strong>client.js<\/strong><\/p>\n<pre>stats: function (options, callback) {\r\n  var query = [],\r\n  url = '',\r\n  hasOptions;\r\n\r\n  if (typeof options === 'function') {\r\n    callback = options;\r\n    options = {};\r\n  }\r\n  \/\/Create a copy of options so we can modify it.\r\n  options = util.merge(options || {});\r\n\r\n  if (options.index) {\r\n    url = '\/' + encode(Array.isArray(options.index) ?\r\n      options.index.join(',') : options.index);\r\n    delete options.index;\r\n    \/\/Look for types only if there is an index\r\n    if (options.types) {\r\n      query.push(encode('types') + '=' + encode(\r\n        Array.isArray(options.types) ?\r\n        options.types.join(',') : options.types));\r\n    }\r\n    delete options.types;\r\n  }\r\n\r\n  url += '\/_stats';\r\n\r\n  util.each(options, function (value, name) {\r\n    if (value === true || value === false) {\r\n      value = value ? '1' : '0';\r\n    }\r\n\r\n    query.push(encode(name) + '=' + encode(value));\r\n  });\r\n\r\n  if (query.length) {\r\n    url += '?' + query.join('&amp;');\r\n  }\r\n\r\n  this._request(url, {\r\n    method: 'GET'\r\n  }, function (err, res) {\r\n    if(err) { return callback(err, null, res), undefined; }\r\n    callback(null, res);\r\n  });\r\n},<\/pre>\n<div><\/div>\n<div>In file <strong>index.js<\/strong><\/div>\n<pre>stats: function (options, callback) {\r\n  if (typeof options === 'function') {\r\n    callback = options;\r\n    options = {};\r\n  }\r\n  this.client.stats(util.merge(options,{index: this.name}), callback);\r\n},<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I had to find the size of an index in Elasticsearch. I could have just built my query using request like this: var request = require(&lsquo;request&rsquo;); request.get(&lsquo;http:\/\/localhost:9200\/&rsquo; + myIndex + &lsquo;\/_stats&rsquo; , function (err, res, body) { console.log(res._all.primaries.store.size_in_bytes); }); Fast and easy. Since it&rsquo;s not available yet in node-elastical, I found it better to &hellip; <a href=\"https:\/\/www.unicoda.com\/?p=1085\" class=\"more-link\">Continuer la lecture<span class=\"screen-reader-text\"> de &laquo;&nbsp;Node-Elastical: Implement stats function&nbsp;&raquo;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[36,37,34],"class_list":["post-1085","post","type-post","status-publish","format-standard","hentry","category-code","tag-elastical","tag-elasticsearch","tag-pull-request"],"_links":{"self":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1085"}],"version-history":[{"count":5,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1085\/revisions"}],"predecessor-version":[{"id":1090,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1085\/revisions\/1090"}],"wp:attachment":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}