typescript elasticsearch helloworld

  1. make project dir

    mkdir sample

  2. open vscode

    code sample

  3. make docker-compose.yml

docker-compose.yml

```
version: "3"
services:
  app:
    image: node
    volumes:
      - .:/app
    working_dir: /app
    depends_on:
      - elasticsearch
    tty: true
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.7.0
    environment:
      - discovery.type=single-node
    ports:
      - 9200:9200
    expose:
      - 9300
  kibana:
    image: docker.elastic.co/kibana/kibana:7.7.0
    ports:
      - 5601:5601

```
  1. npm init

    sudo docker-compose run -u (id -u $USER):(id -g $USER) app npm init -y

  2. npm install

    sudo docker-compose run -u (id -u $USER):(id -g $USER) app npm install -D typescript ts-node

  3. tsc init

    sudo docker-compose run -u (id -u $USER):(id -g $USER) app npx tsc --init --target ES2020

  4. npm install

    sudo docker-compose run -u (id -u $USER):(id -g $USER) app npm install --save @elastic/elasticsearch

  5. create index.ts file

index.ts

```
import { Client } from "@elastic/elasticsearch";

(async () => {
  const client = new Client({ node: "http://elasticsearch:9200" });
  await client.index({
    index: "game-of-thrones",
    body: {
      character: "Ned Stark",
      quote: "hello",
    },
  });
})();
```
  1. edit package.json file

package.json

```
+"start": "ts-node index.ts",
```
  1. docker-compose up

    sudo docker-compose up -d

  2. npm start

    sudo docker-compose exec app npm start

  3. open kibana

    http://localhost:5601/

  4. create index pattern

    Screenshot_2020-05-24 Elastic Kibana.png (87.4 kB)

  5. discover index

    Screenshot_2020-05-24 Discover - Elastic Kibana.png (81.4 kB)