{"id":8874,"date":"2022-04-12T14:44:20","date_gmt":"2022-04-12T19:44:20","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=8874"},"modified":"2022-07-18T14:11:52","modified_gmt":"2022-07-18T19:11:52","slug":"elasticsearch-useful-api-commands","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=8874","title":{"rendered":"ElasticSearch &#8211; Useful API Commands"},"content":{"rendered":"<p>In all of these examples, the copy\/paste text uses localhost and port 9200. Since some of my sandboxes don&#8217;t use the default port, some of the example outputs will use a different port. Obviously, use <em>your<\/em> hostname and port. And, if your ES instance requires authentication, add the &#8220;-u&#8221; option with the user (or user:password &#8230; but that&#8217;s not a good idea outside of sandboxes as the password is then stored to the shell history). If you are using https for the API endpoint, you may also need to add the &#8220;-k&#8221; option to establish an untrusted SSL connection (e.g. the CA isn&#8217;t trusted by your OS).<\/p>\n<p><tt>curl -k -u elastic https:\/\/localhost...<\/tt><\/p>\n<p><strong><em>Listing All Indices<\/em><\/strong><\/p>\n<p>Use the following command to list all of the indices in the ES system:<\/p>\n<p>curl http:\/\/localhost:9200\/_cat\/indices?v<\/p>\n<p><em><strong>Listing All Templates<\/strong><\/em><\/p>\n<p>Use the following command to list all of the templates:<br \/>\n<tt>curl http:\/\/localhost:9200\/_cat\/templates?pretty<\/tt><\/p>\n<p><a href=\"https:\/\/www.rushworth.us\/lisa\/?attachment_id=8875\" rel=\"attachment wp-att-8875\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8875\" src=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2022\/04\/ElasticSearch-ListAllTemplates.png\" alt=\"\" width=\"636\" height=\"341\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2022\/04\/ElasticSearch-ListAllTemplates.png 636w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2022\/04\/ElasticSearch-ListAllTemplates-300x161.png 300w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/a><\/p>\n<p><em><strong>Explain Shard Allocation<\/strong><\/em><\/p>\n<p>I was asked to help get a ELK installation back into working order &#8212; one of the things I noticed is that <em>all<\/em> of the indices were yellow. The log file showed allocation errors. This command reported on the allocation decision that was being made. In the case I was looking at, the problem became immediately obvious &#8212; it was a single node system and 1 replica was defined. The explanation was that the shard could not be stored because it already existed in that place.<\/p>\n<p><tt>curl http:\/\/localhost:9200\/_cluster\/allocation\/explain<\/tt><\/p>\n<p>If the maximum number of allocation retries has been exceeded, you can force ES to re-try allocation (as an example, a disk was full for an extended period of time but space has been cleared and everything <em>should<\/em> work now)<\/p>\n<p><tt>curl http:\/\/localhost:9200\/_cluster\/reroute?retry_failed=true<\/tt><br \/>\n<strong><em>Set the Number of Replicas for a Single Index<\/em><\/strong><\/p>\n<p>Once I identified that the single node ELK instance had indices configured<\/p>\n<pre>curl -X PUT \\\r\nhttp:\/\/127.0.0.1:9200\/logstash-2021.05.08\/_settings \\\r\n-H 'cache-control: no-cache' \\\r\n-H 'content-type: application\/json' \\\r\n-d '{\"index\" : {\"number_of_replicas\" : 0}}\r\n'<\/pre>\n<p><em><strong>Add an Alias to an Index<\/strong><\/em><\/p>\n<p>To add an alias to an existing index, use PUT \/&lt;indexname&gt;\/_alias\/&lt;aliasname&gt; &#8212; e.g.<\/p>\n<p><tt>PUT \/ljr-2022.07.05\/_alias\/ljr<\/tt><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In all of these examples, the copy\/paste text uses localhost and port 9200. Since some of my sandboxes don&#8217;t use the default port, some of the example outputs will use a different port. Obviously, use your hostname and port. And, if your ES instance requires authentication, add the &#8220;-u&#8221; option with the user (or user:password &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1588],"tags":[1590,1589,1591],"class_list":["post-8874","post","type-post","status-publish","format-standard","hentry","category-elk","tag-elasticsearch","tag-elk","tag-kibana"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/8874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8874"}],"version-history":[{"count":4,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/8874\/revisions"}],"predecessor-version":[{"id":9184,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/8874\/revisions\/9184"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}