Elasticsearch: Installation

Run on Docker

Use the following command to run Elasticsearch in a docker container-

docker run -d \
  --name elasticsearch \
  -e discovery.type=single-node \
  -e bootstrap.memory_lock=true \
  -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
  -e xpack.security.enabled=true \
  -e xpack.security.authc.api_key.enabled=true \
  --ulimit memlock=-1:-1 \
  -p 9200:9200 -p 9300:9300 \
  -v es_data:/usr/share/elasticsearch/data \
  docker.elastic.co/elasticsearch/elasticsearch:8.10.1
Bash

Here is the meaning of all the params and flags passed to the command-

Run the following command to set(reset) the password-

docker exec -it elasticsearch bin/elasticsearch-reset-password -u elastic
Bash

You will get some output like below-

This tool will reset the password of the [elastic] user to an autogenerated value.
The password will be printed in the console.
Please confirm that you would like to continue [y/N]y


Password for the [elastic] user successfully reset.
New value: lQhYLyUsgRUFVfZYExG7
Bash

You can see the password.

Check Elasticsearch Installation

Check using Postman

In the Authorization tab, use-

  • Authentication Tab
    • Auth Type: Basic Auth
    • Enter the authentication values-
      • User Name
      • Password
  • Request Method: GET
  • URL: http://localhost:9200
Elasticsearch Check Data

Let’s add some products to the “products” index.

Add product-

Use the request body-

{
  "name": "Elasticsearch Guide",
  "author": "John Doe",
  "category": "Technology",
  "published": "2025-02-01"
}
JSON

Here is the response we get-

{
    "_index": "products",
    "_id": "deBYA5UB807SjwxPX5cK",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}
JSON

Here is a screenshot from postman-

To search the products, use the following info-

Use the following request body-

{
  "query": {
    "match_all": {}
  }
}
JSON

We would get the following output-

{
    "took": 16,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "products",
                "_id": "dOBUA5UB807SjwxP8Zfq",
                "_score": 1.0,
                "_source": {
                    "name": "Elasticsearch Guide",
                    "author": "John Doe",
                    "category": "Technology",
                    "published": "2025-02-01"
                }
            },
            {
                "_index": "products",
                "_id": "deBYA5UB807SjwxPX5cK",
                "_score": 1.0,
                "_source": {
                    "name": "BigBoxCode",
                    "author": "BigBox Auth",
                    "category": "Technology",
                    "published": "2025-02-01"
                }
            }
        ]
    }
}
JSON

Here is the request screenshot-

Elasticsearch Check Data
Elasticsearch Check Data

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.