Elasticsearch with Docker in 5 min

in #programming7 years ago

This is a quick mini guide to start with Elasticsearch with Docker.

In this post I'm going to show you the next things:

  1. How to install Docker and Elasticsearch docker container
  2. How to create a document in Elasticsearch
  3. How to read a document in Elasticsearch
  4. How to update a document in Elasticsearch
  5. How to delete a document in Elasticsearch
  6. How to show all documents in Elasticsearch

1.

For install the last Docker versión:
curl -sSL https://get.docker.com/ubuntu/ | sudo sh
docker --version
*This command must be run as root

To use Docker without root privileges:
gpasswd -a $USER docker
*This command must be run as root

Elasticsearch is an open source Search Server.

For pull image from the official repository of Elasticsearch, run the next command y a terminal:
docker pull docker.elastic.co/elasticsearch/elasticsearch:5.5.2

For list the images:
docker images

For run the Elasticsearch container:
docker run --restart=always -d --name elasticsearch -p 9200:9200 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" docker.elastic.co/elasticsearch/elasticsearch:5.5.2

  • --restart=always : This ensures that the container is always restarted by the Docker daemon if for some reason it stops.
  • -d : For run container in background.
  • --name elasticsearch : the name of the container.
  • -p 9200:9200 : ports of host and container respectively.
    docker.elastic.co/elasticsearch/elasticsearch:5.5.2 : the name of the image and tag version(5.5.2) of the image.

For show running containers by default and filter by string ‘elasticsearch’
docker ps|grep 'elasticsearch'

For show process that is bound to port 9200
netstat -lpn|grep 9200

Elasticsearch user data by default

user: elastic
password: changeme

For test elasticsearch running
curl -u elastic:changeme -XGET 'http://localhost:9200/'

and you will get a result like this:

{
  "name" : "t_QLCEy",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "QMjiT4cNRYOdMADhWw-Wgg",
  "version" : {
    "number" : "5.5.2",
    "build_hash" : "b2f0c09",
    "build_date" : "2017-08-14T12:33:14.154Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.0"
  },
  "tagline" : "You Know, for Search"
}

2.

You can use curl command tool to transfer data from or to a Elasticsearch Server.

An index is like a database in a relational database
An type is like a table in a relational database

In the next example, the index name is: my_index, and the type name is: users

Create document

curl -u elastic:changeme -XPUT http://localhost:9200/my_index/users/1?pretty -d '{
"firstname" : "Rodolfo",
"lastname" : "Guzmán",
"alias" : "El Santo"
}'
1: is the document id

Create another document

curl -u elastic:changeme -XPUT http://localhost:9200/my_index/users/2?pretty -d '{
"firstname" : "Daniel",
"lastname" : "García Arteaga",
"alias" : "Huracán Ramírez"
}'
2: is the document id

3.

Read document

curl -u elastic:changeme -XGET http://localhost:9200/my_index/users/1?pretty
1: is the document id

4.

Update document

curl -u elastic:changeme -XPOST http://localhost:9200/my_index/users/1/_update?pretty -d '{
"doc": { "firstname": "Alejandro", "lastname": "Muñoz Moreno", "alias": "Blue Demon" }
}'

1: is the document id

5.

Delete document
curl -u elastic:changeme -XDELETE http://localhost:9200/my_index/users/1?pretty

1: is the document id

6.

Show documents
curl -u elastic:changeme http://localhost:9200/my_index/users/_search?pretty

In the next post I'll show you how to dynamically load documents in Elasticsearch with a program in golang ;)

References:

https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

https://www.elastic.co/blog/what-is-an-elasticsearch-index

Coin Marketplace

STEEM 0.19
TRX 0.12
JST 0.028
BTC 65566.66
ETH 3559.87
USDT 1.00
SBD 2.48