{"id":2751,"date":"2024-08-25T04:20:25","date_gmt":"2024-08-25T04:20:25","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?p=2751"},"modified":"2024-08-25T17:01:41","modified_gmt":"2024-08-25T17:01:41","slug":"elastic-search","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/database\/elastic-search\/","title":{"rendered":"Elastic Search"},"content":{"rendered":"\n<p>To check health<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET \"localhost:9200\/_cat\/health\"<\/code><\/pre>\n\n\n\n<p>To list all index (tables) <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET \"localhost:9200\/_cat\/indices\"<\/code><\/pre>\n\n\n\n<p>To delete existing index<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X DELETE localhost:9200\/logs<\/code><\/pre>\n\n\n\n<p>To delete multiple indices at a time<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X DELETE localhost:9200\/index1,index2,index3<\/code><\/pre>\n\n\n\n<p>To create new index(table)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X PUT localhost:9200\/index1<\/code><\/pre>\n\n\n\n<p>To insert new document <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X POST localhost:9200\/users\/_doc -H 'Content-Type: application\/json' -d '{\"name\":\"Shailesh\", \"age\": 35, \"city\": \"Nagpur\"}'<\/code><\/pre>\n\n\n\n<p>To list all documents<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET localhost:9200\/users\/_search?pretty<\/code><\/pre>\n\n\n\n<p>To limit \/ size<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET localhost:9200\/users\/_search -H 'Content-Type: application\/json' -d '{\"size\":2}'<\/code><\/pre>\n\n\n\n<p>To add limit with offset<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET localhost:9200\/users\/_search -H 'Content-Type: application\/json' -d '{\"from\":3, \"size\":2}'<\/code><\/pre>\n\n\n\n<p>To list with where clause and equal operator<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET localhost:9200\/users\/_search?pretty -H 'Content-Type: application\/json' -d '{\"query\": {\"match\": {\"name\": \"Shailesh\"}}}'<\/code><\/pre>\n\n\n\n<p>With like query<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET localhost:9200\/users\/_search?pretty -H 'Content-Type: application\/json' -d '{\"query\": {\"wildcard\":{\"name.keyword\":\"Shail*\"}}}'<\/code><\/pre>\n\n\n\n<p>With range<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET localhost:9200\/users\/_search -H 'Content-Type: application\/json' -d '{\"query\": { \"range\" : {\"salary\": {\"gt\": 10000, \"lt\":30000}} } }'<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET localhost:9200\/users\/_search -H 'Content-Type: application\/json' -d '{\"query\": { \"range\" : {\"salary\": {\"gte\": 10000, \"lte\":30000}} } }'<\/code><\/pre>\n\n\n\n<p>Select data from more than one index<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET localhost:9200\/table1,table2\/_search<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET localhost:9200\/table1,table2\/_search -H 'Content-Type: application\/json' -d '{\"query\": {\"range\": {\"salary\": {\"gt\": \"20000\"}}}}'<\/code><\/pre>\n\n\n\n<p>This will search data from both the indices and will show all the documents whose salary is greater than 20000<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Now the most important command, the full text search<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET \"localhost:9200\/users\/_search?pretty\" -H 'Content-Type: application\/json' -d'\n{\n  \"query\": {\n    \"match\": {\n      \"name\": {\n        \"query\": \"Amit Desai\",\n        \"operator\": \"and\"\n      }\n    }\n  }\n}'\n<\/code><\/pre>\n\n\n\n<p>This will search amit and desai matches in any order, you can use <strong>or<\/strong> operator also if wanted to match any of the word from amit and desai<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To check health To list all index (tables) To delete existing index To delete multiple indices at a time To create new index(table) To insert new document To list all documents To limit \/ size To add limit with offset To list with where clause and equal operator With like query With range Select data [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2751"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=2751"}],"version-history":[{"count":7,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2751\/revisions"}],"predecessor-version":[{"id":2763,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2751\/revisions\/2763"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=2751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=2751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=2751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}