Site icon EcomPlugins Blog

Elastic Search Basic Curl Commands

Below are some basic curl commands for interacting with Elasticsearch. These commands cover common operations such as creating indices, adding documents, searching, updating, and deleting data. Replace <your-elasticsearch-url> with your Elasticsearch endpoint (e.g., https://localhost:9200).

1. Check Elasticsearch Status

curl -X GET "<your-elasticsearch-url>/_cluster/health?pretty"

2. Get Elasticsearch Version

curl -X GET "<your-elasticsearch-url>"

3. List All Indices

curl -X GET "<your-elasticsearch-url>/_cat/indices?v"

4. Create an Index

curl -X PUT "<your-elasticsearch-url>/<index-name>?pretty"

curl -X PUT "https://localhost:9200/my-index?pretty"

5. Delete an Index

curl -X DELETE "<your-elasticsearch-url>/<index-name>?pretty"

6. Add a Document

curl -X POST "<your-elasticsearch-url>/<index-name>/_doc/<document-id>?pretty" -H "Content-Type: application/json" -d '{
  "field1": "value1",
  "field2": "value2"
}'

curl -X POST "https://localhost:9200/my-index/_doc/1?pretty" -H "Content-Type: application/json" -d '{
  "name": "John Doe",
  "age": 30
}'

7. Retrieve a Document by ID

curl -X GET "<your-elasticsearch-url>/<index-name>/_doc/<document-id>?pretty"

8. Search for Documents

curl -X GET "<your-elasticsearch-url>/<index-name>/_search?pretty" -H "Content-Type: application/json" -d '{
  "query": {
    "match_all": {}
  }
}'

curl -X GET "https://localhost:9200/my-index/_search?pretty" -H "Content-Type: application/json" -d '{
  "query": {
    "match": {
      "name": "John"
    }
  }
}'

9. Update a Document

curl -X POST "<your-elasticsearch-url>/<index-name>/_update/<document-id>?pretty" -H "Content-Type: application/json" -d '{
  "doc": {
    "field1": "new-value1"
  }
}'

curl -X POST "https://localhost:9200/my-index/_update/1?pretty" -H "Content-Type: application/json" -d '{
  "doc": {
    "age": 31
  }
}'

10. Delete a Document

curl -X DELETE "<your-elasticsearch-url>/<index-name>/_doc/<document-id>?pretty"

11. Bulk Operations

curl -X POST "<your-elasticsearch-url>/_bulk?pretty" -H "Content-Type: application/json" -d '
{ "index": { "_index": "my-index", "_id": "1" } }
{ "field1": "value1", "field2": "value2" }
{ "delete": { "_index": "my-index", "_id": "2" } }

12. Delete all indics

curl -X DELETE 'https://localhost:9200/_all'

13. List all indics

curl -X https://localhost:9200/_all?pretty=true

14. Get results from a index

curl -X https://localhost:9200/{indexname}/_search?pretty=true

For example: https://localhost:9200/magento_product_1_v3/_search?pretty=true

 

Exit mobile version