Skip to content

Instantly share code, notes, and snippets.

@roalcantara
Last active May 9, 2023 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save roalcantara/4a807656c1176bb59c9cde0ae369a5ef to your computer and use it in GitHub Desktop.
Save roalcantara/4a807656c1176bb59c9cde0ae369a5ef to your computer and use it in GitHub Desktop.
Elasticsearch: Setup and Quickstart

Setup

  1. Install Docker

    brew install --cask docker-edge

  2. Check if the Docker is up and running

    $ docker --version
    
    Docker version 20.10.0-rc2, build dca98e3
  3. Pull the Elasticsearch Docker image

    $ docker pull docker.elastic.co/elasticsearch/elasticsearch:7.10.2
    
    7.10.2: Pulling from elasticsearch/elasticsearch
    ddf49b9115d7: Pull complete 
    e736878e27ad: Pull complete 
    7487c9dcefbe: Pull complete 
    9ccb7e6e1f0c: Pull complete 
    dcec6dec98db: Pull complete 
    8a10b4854661: Pull complete 
    1e595aee1b7d: Pull complete 
    06cc198dbf22: Pull complete 
    55b9b1b50ed8: Pull complete 
    Digest: sha256:d528cec81720266974fdfe7a0f12fee928dc02e5a2c754b45b9a84c84695bfd9
    Status: Downloaded newer image for docker.elastic.co/elasticsearch/elasticsearch:7.10.2
    docker.elastic.co/elasticsearch/elasticsearch:7.10.2
  4. Run Elasticsearch on a single node cluster

    docker run -d --rm -p 9200:9200 --name elasticsearch_7_10_2 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.10.2
  5. Check if the Elasticsearch container is up and running

    $ curl -X GET 'localhost:9200/_cat/nodes?v=true'
    
    ip         heap.percent ram.percent cpu load_1m load_5m load_15m node.role  master name
    172.17.0.2           22          96   0    0.00    0.01     0.00 cdhilmrstw *      5160ddd7a72b
  6. Shutdown the Elasticsearch container

    docker container stop elasticsearch_7_10_2

References

Setup

Homebrew

  1. Check the latest elasticsearch version

      $ brew info elasticsearch
    
      elasticsearch: stable 7.10.2 (bottled)
      Distributed search & analytics engine
      https://www.elastic.co/products/elasticsearch
      Deprecated because it is switching to an incompatible license!
      Not installed
      From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/elasticsearch.rb
      License: Apache-2.0
      
      ==> Dependencies
      Build: gradle ✘
      Required: openjdk ✘
      
      ==> Caveats
      Data:    /usr/local/var/lib/elasticsearch/
      Logs:    /usr/local/var/log/elasticsearch/elasticsearch_roalcantara.log
      Plugins: /usr/local/var/elasticsearch/plugins/
      Config:  /usr/local/etc/elasticsearch/
    
      To have launchd start elasticsearch now and restart at login:
        brew services start elasticsearch
      Or, if you don't want/need a background service you can just run:
        elasticsearch
  2. Install Elasticsearch

    brew install elasticsearch
  3. Start the Elasticsearch service

    brew services start elasticsearch
  4. Check if Elasticsearch is up and running

    $ curl -X GET 'localhost:9200/_cat/nodes?v=true'
    
    ip         heap.percent ram.percent cpu load_1m load_5m load_15m node.role  master name
    172.17.0.2           22          96   0    0.00    0.01     0.00 cdhilmrstw *      5160ddd7a72b
  5. Stop the Elasticsearch service

    brew services stop elasticsearch

Quickstart

cURL

  1. CREATE a document

    curl -X POST http://localhost:9200/pokemons/_doc/100 \
      --header 'content-type: application/json' \
      --data '{"code": 100,"name": "soundproof","height": 5,"weight": 104,"created_at": "2021-02-04T13:31:42.843787"}'
  2. GET documents

    curl http://localhost:9200/pokemons/_search
  3. GET documents by term

    curl -X POST http://localhost:9200/pokemons/_search \
      --header 'content-type: application/json' \
      --data '{"query": {"term": {"code": 100}}}'
  4. GET document by id

    curl http://localhost:9200/pokemons/_doc/100
  5. DELETE document by id

    curl -X DELETE http://localhost:9200/pokemons/_doc/100
  6. GET all running nodes

    curl 'http://localhost:9200/_cat/nodes?v=true'
  7. GET an index's mapping

    curl http://localhost:9200/_mapping
  8. DELETE an index

    curl -X DELETE http://localhost:9200/pokemons

Quickstart

# elasticsearch.rest

@port = 9200
@hostname = localhost
@host = {{hostname}}:{{port}}
@index = pokemons
@id = 100

### CREATE a document
# https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
POST http://{{host}}/{{index}}/_doc/{{id}}
content-type: application/json

{
  "code": {{id}},
  "name": "soundproof",
  "height": 5,
  "weight": 104,
  "created_at": "2021-02-04T13:31:42.843787"
}

### GET documents
# https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
GET http://{{host}}/{{index}}/_search

### GET documents by term
# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
POST http://{{host}}/{{index}}/_search
content-type: application/json

{
  "query": {
    "term": {
      "code": {{id}}
    }
  }
}

### GET document by id
# https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html
GET http://{{host}}/{{index}}/_doc/{{id}}

### DELETE document by id
# https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html
DELETE http://{{host}}/{{index}}/_doc/{{id}}

#### GET all running nodes
# https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-nodes.html
GET http://{{host}}/_cat/nodes?v=true

### GET an index's mapping
# https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html
GET http://{{host}}/_mapping

### DELETE an index
# https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html
DELETE http://{{host}}/{{index}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment