{"id":882,"date":"2012-11-19T12:23:48","date_gmt":"2012-11-19T11:23:48","guid":{"rendered":"https:\/\/www.unicoda.com\/?p=882"},"modified":"2013-01-18T10:28:50","modified_gmt":"2013-01-18T09:28:50","slug":"elastical-delete-by-query-option","status":"publish","type":"post","link":"https:\/\/www.unicoda.com\/?p=882","title":{"rendered":"Node-Elastical: Delete by query option"},"content":{"rendered":"<p>What is <a href=\"https:\/\/github.com\/ramv\/node-elastical\">Elastical<\/a>?<\/p>\n<blockquote><p>Elastical is a Node.js client library for the <a href=\"http:\/\/www.elasticsearch.org\">ElasticSearch<\/a> REST API.<\/p><\/blockquote>\n<p>That&rsquo;s it for the presentation.<\/p>\n<p>Until a few weeks ago, it wasn&rsquo;t possible to delete data by query without using a hack. The hack was the following:<\/p>\n<pre>client.delete('twitter', 'tweet', '', {q: 'user:Shay'} );<\/pre>\n<p>Here, we set id to an empty string and we use the options parameter to perform a search on <em>user<\/em> with value <em>Shay<\/em>. Not so practical at all.<\/p>\n<p>Using curl to interact with ElasticSearch, it is possible to delete by query:<\/p>\n<pre>curl -XDELETE 'http:\/\/localhost:9200\/twitter\/tweet\/_query' -d ' \r\n  { \"term\" : { \"user\" : \"kimchy\" } \r\n}\r\n'<\/pre>\n<p>This handy way of deleting things in ElasticSearch was not possible with Elastical (unless, you managed to use the hack above).<\/p>\n<p>So I&rsquo;ve added a new parameter <em>options.query<\/em> which allow us to perform such a query more easily.<\/p>\n<pre>client.delete('twitter', 'tweet', '', {query:\r\n  { \"term\" : { \"user\" : \"kimchy\" } } \r\n});<\/pre>\n<p>When using the <em>query<\/em> parameter, <em>id<\/em> and all other options except ignoreMissing will be ignored.<\/p>\n<p>Let&rsquo;s take a quick look at the code involved:<\/p>\n<pre>if(params.query) {\r\n  url = '\/' + encode(this.name) + '\/' + encode(type) + '\/_query';\r\n\r\n\u00a0 this.client._request(url, {method: 'DELETE', json: params.query}\r\n  , function (err, res) {\r\n\u00a0   if (err) {\r\n\u00a0     if (ignoreMissing &amp;&amp; res &amp;&amp; res.found === false) {\r\n\u00a0\u00a0\u00a0     return callback(null, res), undefined;\r\n\u00a0\u00a0\u00a0   } else {\r\n\u00a0\u00a0\u00a0     return callback(err, res), undefined;\r\n\u00a0\u00a0\u00a0   }\r\n\u00a0   }\r\n\u00a0   callback(null, res);\r\n  });\r\n} else {<\/pre>\n<p>It&rsquo;s pretty simple. First, we need to build the url, then we&rsquo;re requesting ElasticSearch on the given url, passing the query in the <em>json<\/em> parameter. If options.query doesn&rsquo;t exist, then the original code is used.<\/p>\n<p>As a conclusion, delete by query is now available in Elastical and you should use it!<\/p>\n<p>&nbsp;<\/p>\n<p>ElasticSearch: <a href=\"http:\/\/www.elasticsearch.org\/guide\/reference\/api\/delete-by-query.html\">Delete By Query API<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Elastical? Elastical is a Node.js client library for the ElasticSearch REST API. That&rsquo;s it for the presentation. Until a few weeks ago, it wasn&rsquo;t possible to delete data by query without using a hack. The hack was the following: client.delete(&lsquo;twitter&rsquo;, &lsquo;tweet&rsquo;, \u00a0\u00bb, {q: &lsquo;user:Shay&rsquo;} ); Here, we set id to an empty string &hellip; <a href=\"https:\/\/www.unicoda.com\/?p=882\" class=\"more-link\">Continuer la lecture<span class=\"screen-reader-text\"> de &laquo;&nbsp;Node-Elastical: Delete by query option&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,14,34],"class_list":["post-882","post","type-post","status-publish","format-standard","hentry","category-code","tag-elastical","tag-elasticsearch","tag-github","tag-pull-request"],"_links":{"self":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/882","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=882"}],"version-history":[{"count":9,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/882\/revisions"}],"predecessor-version":[{"id":890,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/882\/revisions\/890"}],"wp:attachment":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}