{"id":20405814,"url":"https://github.com/aryak0512/elasticsearch","last_synced_at":"2025-08-25T23:02:43.317Z","repository":{"id":236721536,"uuid":"793021423","full_name":"aryak0512/elasticsearch","owner":"aryak0512","description":"Elasticsearch handy notes and commands","archived":false,"fork":false,"pushed_at":"2024-04-28T09:12:24.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-25T23:02:13.971Z","etag":null,"topics":["elastic","elasticsearch","elasticsearch-client","elasticstack","kibana"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aryak0512.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-28T07:46:47.000Z","updated_at":"2024-04-28T09:14:09.000Z","dependencies_parsed_at":"2024-04-28T09:24:08.672Z","dependency_job_id":"42ec2510-51c9-4633-b5b0-b984f22aec24","html_url":"https://github.com/aryak0512/elasticsearch","commit_stats":null,"previous_names":["aryak0512/elasticsearch"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/aryak0512/elasticsearch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aryak0512%2Felasticsearch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aryak0512%2Felasticsearch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aryak0512%2Felasticsearch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aryak0512%2Felasticsearch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aryak0512","download_url":"https://codeload.github.com/aryak0512/elasticsearch/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aryak0512%2Felasticsearch/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272144649,"owners_count":24881141,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-25T02:00:12.092Z","response_time":1107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elastic","elasticsearch","elasticsearch-client","elasticstack","kibana"],"created_at":"2024-11-15T05:13:22.962Z","updated_at":"2025-08-25T23:02:43.046Z","avatar_url":"https://github.com/aryak0512.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Elasticsearch\n\n- Disable gatekeeper for embedded JDK in MacOS\n\n```console\naryak@Aryaks-MacBook-Pro Desktop % xattr -d -r com.apple.quarantine /path/to/elastic\n```\n\n## Basic terminologies\n\n### Document\n\nA document is a record in JSON format, document can be compared to a tuple in RDBMS\n\n### Index\n\n- An index is a collection of related documents.\n- Index can be compared to table in RDBMS.\n- In elasticsearch, we always query on indices.\n\n### Node and cluster\n\nEvery node in elasticsearch is a member of a cluster by default.\n\n## REST APIs\n\n- Elasticsearch exposes several APIs along with commands\n- From Elastic version 8 and above, APIs are exposed over HTTPS ( with simple authentication - xpack) and not HTTP.\n- APIs always begin with an underscore [ eg : /_cluster], followed by command [ eg : /health].\n\n```bash\ncurl -X GET https://localhost:9200/_cluster/health\n```\n\n```bash\ncurl --cacert config/certs/http_ca.crt -u elastic:+mqJn+EXfC5hdcqZkB8z -X GET https://localhost:9200/_cluster/health\n```\n\n## Sharding\n\n- Sharding is a concept used for achieving higher availablity and throughput\n- Replica shards are never stored on same node\n- Once can index is created, the no of shards is final, cannot be altered.\n- Max limit of a shard is 2 billion documents.\n- Default no of shards for an index is 1.\n- Default no of replica per shard is 1.\n- ES provides:\n\n  - Split API : for altering number of shards of an index, which under the hood creates a new index with more shards and ES itself does the heavy lifting of migrating the documents to the new index.\n\n  - Shrink API : for reducing the number of shards on an index\n\n## Querying Elasticsearch\n\n### Get all nodes\n\n```bash\nGET /_cat/nodes?v\n```\n\n### Get all indices\n\n```bash\ncurl -X GET http://localhost:9200/_cluster/health\n```\n\n### Create an index\n\n```bash\nPUT /products\n```\n\n### Get structure of an index\n\n```bash\nGET /products\n```\n\n### Get all shards\n\n```bash\nGET /_cat/shards?v\n```\n\n### Adding a document\n\n- Request\n\n```bash\nPOST /products/_doc\n{\n  \"name\": \"Iphone 15 plus\",\n  \"price\": 999,\n  \"in_stock\": 20\n}\n```\n\n- Response\n\n```bash\n{\n  \"_index\": \"products\",\n  \"_id\": \"fZvbI48BTI3iOvwGbjKy\",\n  \"_version\": 1,\n  \"result\": \"created\",\n  \"_shards\": {\n    \"total\": 2,\n    \"successful\": 1,\n    \"failed\": 0\n  },\n  \"_seq_no\": 9,\n  \"_primary_term\": 1\n}\n```\n\n## Adding a document with ID=1000\n\n- Request\n\n```bash\nPUT /products/_doc/1000\n{\n\"name\": \"Iphone 15 pro\",\n\"price\": 899,\n\"in_stock\": 10\n}\n```\n\n- Response\n\n```bash\n{\n  \"_index\": \"products\",\n  \"_id\": \"fJvaI48BTI3iOvwGajL5\",\n  \"_version\": 1,\n  \"result\": \"created\",\n  \"_shards\": {\n    \"total\": 2,\n    \"successful\": 1,\n    \"failed\": 0\n  },\n  \"_seq_no\": 8,\n  \"_primary_term\": 1\n}\n```\n\n## Updating a document\n\n- Documents in elasticsearch are immutable.\n- ES simply simply copies the data to a new doc, with same id which gives a feeling of update.\n\n#### Adding a field to / updating the document with ID = 1000\n\n```bash\nPOST /products/_update/1000\n{\n  \"doc\": {\n    \"in_stock\": 19\n  }\n}\n```\n\n#### Decrement the in_stock field by 1\n\n```bash\nPOST /products/_update/1000\n{\n  \"script\": {\n    \"source\": \"ctx._source.in_stock--\"\n  }\n}\n```\n\n#### Increment the in_stock field by 10\n\n```bash\nPOST /products/_update/1000\n{\n  \"script\": {\n    \"source\": \"ctx._source.in_stock+=10\"\n  }\n}\n```\n\n#### Increment the in_stock by a value passed as parameter\n\n```bash\nPOST /products/_update/1000\nPOST /products/_update/1000\n{\n  \"script\": {\n    \"source\": \"ctx._source.in_stock+=params.quantity\",\n    \"params\": {\n      \"quantity\": 3\n    }\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faryak0512%2Felasticsearch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faryak0512%2Felasticsearch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faryak0512%2Felasticsearch/lists"}