Create Indexes in elasticsearch and frequently used commands


Create Indexes in elasticsearch and frequently used commands

Create Indexes in elasticsearch and frequently used commands

Preface

This post assumes that you have some basic understanding of Docker, Docker Compose, and the key components used in the docker ecosystem. Get up to speed, with the Prepare Your Docker Environment section of Docker docs.

  1. Install Docker
  2. install docker-compose
  3. Install Elasticsearch or Run Elasticsearch container

Frequently used commands

  • Check the status of the Elasticsearch

  • Check the health status of the Elasticsearch

curl -X GET "localhost:9200/_cat/health"
  • Get the number of nodes of the Elasticsearch Cluster
curl -X GET "localhost:9200/_cat/nodes"
  • Check with the shards with
curl -X GET "localhost:9200/_cat/shards"
  • Get list of indices
curl -X GET "localhost:9200/_cat/indices?v"
  • Get list of indices with specific column, we want to the column index, which will list the index names
curl -X GET "localhost:9200/_cat/indices?v&h=index"
  • Get the list of indices sort by column
curl -X GET "localhost:9200/_cat/indices?v&s=docs.count:desc"

curl -X GET "localhost:9200/_cat/indices?v&s=docs.count:asc"

curl -X GET "localhost:9200/_cat/indices?v&s=index"

curl -X GET "localhost:9200/_cat/indices?v&s=docs.count:desc"

curl -X GET "localhost:9200/_cat/indices?v&s=docs.count:desc"
  • Why the health status of the Elasticsearch is red

Number of nodes in the cluster was three so there was no extra node to create the replica, and restore the unassigned indexes, So the health was turning to red. Created the index with settings property and set the number_of_replicas as 0.

curl -XPUT 'localhost:9200/_settings' -H 'Content-Type: application/json' -d '
{
    "index" : {
        "number_of_replicas" : 0
    }
}'

Create the Indice

  • First, let’s create a twitter user, and add some tweets
curl -XPUT 'http://127.0.0.1:9200/twitter/user/kimchy' -d '{ "name" : "Shay Banon" }'

curl -XPUT 'http://127.0.0.1:9200/twitter/tweet/1' -d '
{
    "user": "kimchy",
    "postDate": "2009-11-15T13:12:00",
    "message": "Trying out Elasticsearch multinode, so far so good?"
}'

curl -XPUT 'http://127.0.0.1:9200/twitter/tweet/2' -d '
{
    "user": "kimchy",
    "postDate": "2009-11-15T14:12:12",
    "message": "Balu tweet, will it be indexed?"
}'
  • Now, let’s see if the information was added by GETting it:
curl -XGET 'http://127.0.0.1:9200/twitter/user/kimchy?pretty=true'
curl -XGET 'http://127.0.0.1:9200/twitter/tweet/1?pretty=true'
curl -XGET 'http://127.0.0.1:9200/twitter/tweet/2?pretty=true'
  • Create customer entity with 1000 records
curl -sXPUT 'http://localhost:9200/customer/?pretty' -d '{
  "settings" : {
      "index" : {
          "number_of_shards" : 5,
          "number_of_replicas" : 0
      }
  }
}'

while ! curl -s "localhost:9200/_cat/indices?v" | grep green; do
  sleep 0.1
done

for i in `seq 1 500`; do
  curl -sXPUT "localhost:9200/customer/external/$i?pretty" -d "
  {
    \"number\": $i,
    \"name\": \"John Doe - $i\"
  }"
done
  • Create student entity with 1000 records
curl -sXPUT 'http://localhost:9200/student/?pretty' -d '{
  "settings" : {
      "index" : {
          "number_of_shards" : 5,
          "number_of_replicas" : 0
      }
  }
}'

while ! curl -s "localhost:9200/_cat/indices?v" | grep green; do
  sleep 0.1
done

for i in `seq 1 20`; do
  curl -sXPUT "localhost:9200/student/external/$i?pretty" -d "
  {
    \"number\": $i,
    \"name\": \"Ram - $i\"
  }"
done
  • Somemore examples
curl -X PUT "localhost:9200/twitter?pretty"

curl -X PUT "localhost:9200/school?pretty" -H 'Content-Type: application/json' -d'
{
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 2
    },
    "mappings" : {
        "properties" : {
            "name" : { "type" : "text" }
        }
    }
}
'

Delete index

curl -X DELETE "localhost:9200/school?pretty"

Get Index

  • Get the mappings and setting with the following
curl -X GET "localhost:9200/school?pretty"

curl -X GET "localhost:9200/school/_mapping?pretty"

curl -X GET "localhost:9200/school/_settings?pretty"

  • Checks if an index exists
curl -I "localhost:9200/twitter?pretty"
  • Get the count of Index
curl -I "localhost:9200/twitter?pretty"
  • Update index settings API
curl -X PUT "localhost:9200/school/_settings?pretty" -H 'Content-Type: application/json' -d'
{
    "index" : {
        "number_of_replicas" : 2
    }
}
'
  • Get the Statistics of the index
curl -X GET "localhost:9200/school/_stats?pretty"

curl -X GET "localhost:9200/_stats?pretty"

curl -X GET "localhost:9200/index1,index2/_stats?pretty"

comments powered by Disqus