Skip to content

Latest commit

ย 

History

History
584 lines (395 loc) ยท 14.8 KB

ElasticSearch.md

File metadata and controls

584 lines (395 loc) ยท 14.8 KB

Elasticsearch

Intro

Elasticsearch์™€ ๊ด€๊ณ„ํ˜• DB(RDBMS) ๋น„๊ต

๊ด€๊ณ„ํ˜• DB | ElasticSearch

Database | Index

Table | Type

Row | Document

Column | Field

Index | Analyze

Primary key | _id

Schema | Mapping

Physical partition | Shard

Logical partition | Route

Relational | Parent/Child, Nested

SQL | Query DSL

Elasticsearch ์•„ํ‚คํ…์ณ, ์šฉ์–ด

  1. ํด๋Ÿฌ์Šคํ„ฐ(cluster)
  • ํด๋Ÿฌ์Šคํ„ฐ๋ž€ Elasticsearch์—์„œ ๊ฐ€์žฅ ํฐ ์‹œ์Šคํ…œ ๋‹จ์œ„๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ์ตœ์†Œ ํ•˜๋‚˜ ์ด์ƒ์˜ ๋…ธ๋“œ๋กœ ์ด๋ฃจ์–ด์ง„ ๋…ธ๋“œ๋“ค์˜ ์ง‘ํ•ฉ

  • ์„œ๋กœ ๋‹ค๋ฅธ ํด๋Ÿฌ์Šคํ„ฐ๋Š” ๋ฐ์ดํ„ฐ์˜ ์ ‘๊ทผ, ๊ตํ™˜์„ ํ•  ์ˆ˜ ์—†๋Š” ๋…๋ฆฝ์ ์ธ ์‹œ์Šคํ…œ

  • ์—ฌ๋Ÿฌ ๋Œ€์˜ ์„œ๋ฒ„๊ฐ€ ํ•˜๋‚˜์˜ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๊ณ , ํ•œ ์„œ๋ฒ„์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํด๋Ÿฌ์Šคํ„ฐ๊ฐ€ ์กด์žฌํ• ์ˆ˜๋„ ์žˆ์Œ

  1. ๋…ธ๋“œ(node)
  • Elasticsearch๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ํ•˜๋‚˜์˜ ๋‹จ์œ„ ํ”„๋กœ์„ธ์Šค

  • ์—ญํ• ์— ๋”ฐ๋ผ Master-eligible, Data, Ingest, Tribe ๋…ธ๋“œ๋กœ ๊ตฌ๋ถ„ ๊ฐ€๋Šฅ

  1. ์ธ๋ฑ์Šค(index) / ์ƒค๋“œ(Shard) / ๋ณต์ œ(Replica)

์ธ๋ฑ์Šค(index)๋Š” ๊ด€๊ณ„ํ˜• DB์—์„œ Database์— ๋Œ€์‘ํ•˜๋Š” ๊ฐœ๋…

  • ์ƒค๋“œ(Shard): ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์‚ฐํ•ด์„œ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•. ์ธ๋ฑ์Šค๋ฅผ ์—ฌ๋Ÿฌ ์ƒค๋“œ๋กœ ์ชผ๊ฐ  ๊ฒƒ์œผ๋กœ ๊ธฐ๋ณธ์ ์œผ๋กœ 1๊ฐœ๊ฐ€ ์กด์žฌ. ๊ฒ€์ƒ‰ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ์œ„ํ•ด ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ƒค๋“œ ๊ฐฏ์ˆ˜๋ฅผ ์กฐ์ •ํ•˜๊ธฐ๋„ํ•จ(ํŠœ๋‹)

  • ๋ณต์ œ(Replica): ๋˜ ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ ์ƒค๋“œ. ๋…ธ๋“œ๋ฅผ ์†์‹คํ–ˆ์„ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ์‹ ๋ขฐ์„ฑ์„ ์œ„ํ•ด ์ƒค๋“œ๋“ค์„ ๋ณต์ œํ•˜๋Š” ๊ฒƒ. ์„œ๋กœ ๋‹ค๋ฅธ ๋…ธ๋“œ์— ์กด์žฌํ•  ๊ฒƒ์„ ๊ถŒ์žฅ.

Elasticsearch์˜ ํŠน์ง•

  • Scale out ์ƒค๋“œ(Shard)๋ฅผ ํ†ตํ•ด ๊ทœ๋ชจ๊ฐ€ ์ˆ˜ํ‰์ ์œผ๋กœ ๋Š˜์–ด๋‚  ์ˆ˜ ์žˆ์Œ

  • ๊ณ ๊ฐ€์šฉ์„ฑ ๋ณต์ œ(Replica)๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ์˜ ์•ˆ์ •์„ฑ์„ ๋ณด์žฅ

  • Schema Free JSON ๋ฌธ์„œ๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ์Šคํ‚ค๋งˆ ๊ฐœ๋…์ด ์—†์Œ

  • Restful ๋ฐ์ดํ„ฐ CRUD ์ž‘์—…์€ HTTP Restful API๋ฅผ ํ†ตํ•ด ์ˆ˜ํ–‰

Data CRUD | Elasticsearch Restful | SQL CREATE | POST | INSERT READ | GET | SELECT UPDATE | PUT | UPDATE DELETE | DELETE | DELETE

PUT, POST(๋งคํ•‘์ด๋‚˜ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…)๋Š” ์—„๊ฒฉํ•˜๊ฒŒ ๊ตฌ๋ถ„ํ•˜์ง„ ์•Š๋Š”๋Œ€


ํด๋Ÿฌ์Šคํ„ฐ

Elasticsearch๋ฅผ ์„ค์น˜ํ•˜๊ณ  ์‹คํ–‰ํ•ด๋ณด๋ฉด ํด๋Ÿฌ์Šคํ„ฐ๊ฐ€ 1๊ฐœ ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

ํด๋Ÿฌ์Šคํ„ฐ health ์ฒดํฌ - ํด๋Ÿฌ์Šคํ„ฐ ์ •๋ณด

$ curl -XGET localhost:9200/_cluster/health?pretty=true

curl ๋ช…๋ น์–ด๊ฐ€ ์ž‘๋™ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ, sudo apt install curl ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด curl ์„ค์น˜

ํด๋Ÿฌ์Šคํ„ฐ status

Elasticsearch status ์„ค๋ช…


elasticsearch crud

Restful API๋ž€?

  • ๋…ธ๋“œ์™€ ํ†ต์‹ ํ•˜๋Š” ๋ฐฉ๋ฒ•
  • Elasticsearch๋Š” ํด๋Ÿฌ์Šคํ„ฐ์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๋งค์šฐ ํฌ๊ด„์ ์ด๊ณ  ๊ฐ•๋ ฅํ•œ REST API๋ฅผ ์ œ๊ณต
  • ์ˆ˜ํ–‰ ๊ฐ€๋Šฅํ•œ ์ž‘์—…
    • ํด๋Ÿฌ์Šคํ„ฐ, ๋…ธ๋“œ, ์ƒ‰์ธ ์ƒํƒœ, ์ƒํƒœ ๋ฐ ํ†ต๊ณ„ ํ™•์ธ
    • ํด๋Ÿฌ์Šคํ„ฐ, ๋…ธ๋“œ ๋ฐ ์ƒ‰์ธ ๋ฐ์ดํ„ฐ ๋ฐ ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ
    • CRUD(Create, Read, Update, Delete) ๋ฐ ์ธ๋ฑ์Šค์— ๋Œ€ํ•œ ๊ฒ€์ƒ‰ ์ž‘์—… ์ˆ˜ํ–‰
    • ํŽ˜์ด์ง•, ์ •๋ ฌ, ํ•„ํ„ฐ๋ง, ์Šคํฌ๋ฆฝํŒ…, ์ง‘๊ณ„ ๋ฐ ๊ธฐํƒ€ ๊ณ ๊ธ‰ ๊ฒ€์ƒ‰ ์ž‘์—… ์‹คํ–‰
  • ๊ตฌ์„ฑ์š”์†Œ 3๊ฐ€์ง€: ๋ฆฌ์†Œ์Šค, ๋ฉ”์„œ๋“œ, ๋ฉ”์„ธ์ง€ ๋ฉ”์„œ๋“œ์—” put get delete delete ์žˆ์Œ(์›น์—์„  put,get๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)

์„ค๋ช…

REST ์™„ ๋ณ„๊ฐœ๋กœ elasticsearch๊ฐ€ ์ œ๊ณตํ•˜๋Š” api๊ฐ€ ์žˆ๋Š”๋ฐ GET/_cat/nodes?v

_cat :ํด๋Ÿฌ์Šคํ„ฐ ์ƒํƒœ ํ™•์ธ์œ„ํ•ด ์‚ฌ์šฉ _search _update ์ด๋Ÿฐ๊ฑฐ!

v: ์ปฌ๋Ÿผ๋ช… ๋ณด์—ฌ์คŒ pretty: json ํŒŒ์ผ ๋ฐ›์œผ๋ฉด ๋ณดํ†ต ๋‹ค ๋ถ™์–ด์žˆ๋Š”๋ฐ ์ด๊ฑธ ์ด์˜๊ฒŒ ๋ณด์—ฌ์คŒ

ํด๋Ÿฌ์Šคํ„ฐ ์ƒํƒœ ๋…น์ƒ‰: ํด๋Ÿฌ์Šคํ„ฐ๊ฐ€ ์™„์ „ํžˆ ์ž‘๋™ ๋…ธ๋ž€์ƒ‰: ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ผ๋ถ€ ๋ณต์ œ๋ณธ์ด ํ• ๋‹น์ด ์•ˆ๋œ ๊ฒฝ์šฐ(์ƒค๋”ฉ ์•ˆ๋œ๊ฒฝ์šฐ) ๋นจ๊ฐ„์ƒ‰: ๋นจ๊ฐ„์ƒ‰์ด๋ฉด ์‚ฌ์šฉ๋ถˆ๊ฐ€

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ ํ™•์ธ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ชจ๋“  ์ธ๋ฑ์Šค ํ•ญ๋ชฉ ์กฐํšŒ(indices=index+es ๋ผ๊ณ  ๋ณด๋ฉด๋จ)

GET/_cat_indices?v (cat์€ ๋…ธ๋“œ ์กฐํšŒ ์˜๋ฏธ)

  • elasticsearch ์˜ ์ธ๋ฑ์‹ฑ ๋ฐฉ์‹ ๊ด€๊ณ„ํ˜• db์—์„œ์˜ ๊ฒ€์ƒ‰ ๋ฐฉ์‹ + ์ฑ… ๋’ค์— ์žˆ๋Š” ์ƒ‰์ธ ๋ชฉ๋ก๊ณผ ์œ ์‚ฌ(์—ญํŒŒ์ผ ์ƒ‰์ธ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ) ex)๋‹จ์–ด, ๊ทธ ๋‹จ์–ด๊ฐ€ ์žˆ๋Š” ํŽ˜์ด์ง€ ๋ฐ์ดํ„ฐ๊ฐ€ ์ปค์ง„๋‹ค๋Š” ๋‹จ์ ๋„ ์žˆ์ง€๋งŒ ๊ฒ€์ƒ‰์ด ๋น ๋ฆ„

์—˜๋ผ์Šคํ‹ฑ ์„œ์น˜์˜ ์งˆ์˜๋ฐฉ๋ฒ•

  • ์ปค๋งจ๋“œ๋ผ์ธ์˜ curl ๋ช…๋ น์–ด ์‚ฌ์šฉ -> ๋ถˆํŽธ
  • postman ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ์‚ฌ์šฉ(window์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ)
  • kibana์—์„œ devtool ์‚ฌ์šฉ (์‚ฌ์šฉ)
    • kibana์˜ ์—ฐ์žฅ์•„์ด์ฝ˜ ํด๋ฆญ(dev tool์ž„)

๋ฐ์ดํ„ฐ ์ž…๋ ฅ/์กฐํšŒ/์‚ญ์ œ/์—…๋ฐ์ดํŠธ ์š”์•ฝ

  • ์ž…๋ ฅ

PUT ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜์ฃผ์†Œ/์ธ๋ฑ์Šค(๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค)/ํƒ€์ž…(ํ…Œ์ด๋ธ”)/document๋ฒˆํ˜ธ(_id) -d{๋ฉ”์„ธ์ง€}

๋ฉ”์„ธ์ง€๋Š” json ํ˜•ํƒœ๋กœ ๋“ค์–ด๊ฐ€์•ผํ•จ(key value์˜ ์Œ)

  • ์กฐํšŒ

GET ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜์ฃผ์†Œ/์ธ๋ฑ์Šค(๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค)/ํƒ€์ž…(ํ…Œ์ด๋ธ”)/document๋ฒˆํ˜ธ(_id)

  • ์‚ญ์ œ

DELETE ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜์ฃผ์†Œ/์ธ๋ฑ์Šค(๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค)/ํƒ€์ž…(ํ…Œ์ด๋ธ”)/document๋ฒˆํ˜ธ(_id)

  • ์—…๋ฐ์ดํŠธ

POST ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜์ฃผ์†Œ/์ธ๋ฑ์Šค(๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค)/ํƒ€์ž…(ํ…Œ์ด๋ธ”)/document๋ฒˆํ˜ธ(_id)/_update -d{์ˆ˜์ •ํ• ๋‚ด์šฉ}

_update API ์‚ฌ์šฉ

์‹ค์Šต

์ฃผ์†Œ์ฐฝ์— ๋…ธ๋“œ ํ™•์ธ 127.0.0.1:9200/_cat/nodes?v ์ƒํƒœ ํ™•์ธ 127.0.0.1:9200/_cat/health?v 127.0.0.1:5601

GET _cat/indices

POST customer/type1/1
{
  "name": "yurim",
  "age": 24
}

//์œ„ ๋ฐฉ๋ฒ•์œผ๋กœ update๋ฅผ ํ•˜๋ฉด ๋ฉ”์„ธ์ง€์˜ ๋‚ด์šฉ์œผ๋กœ ๋„๊ทœ๋จผํŠธ ์ „์ฒด๊ฐ€ ๊ฐˆ์•„๋ผ์›Œ์ง

POST customer/type1/1/_update
{
  "doc":{
    "age":22
  }  
}

//์œ„ ๋ฐฉ๋ฒ•์œผ๋กœ update๋ฅผ ํ•˜๋ฉด ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์€ ๋ถ€๋ถ„๋งŒ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Œ
"doc" ์€ ๋ฌด์กฐ๊ฑด ๋“ค์–ด๊ฐ€์•ผํ•จ
document์—์„œ ์ด๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•˜๊ฒ ๋‹ค ๋ผ๋Š” ๋œป

POST customer/type1/1/_update
{
  "script":{
    "inline": "if(ctx._source.age==24){ctx._source.age++}"
    }
}

// ์œ„์ฒ˜๋Ÿผ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‚ด์šฉ์„ ์—…๋ฐ์ดํŠธ ํ•  ์ˆ˜๋„ ์žˆ์Œ(์ฝ”๋“œ์ ์ธ๊ฒƒ)
// ํ•ด๋‹น ๋„ํ๋จผํŠธ ์ปจํ…์ŠคํŠธ์˜ _source๋ถ€๋ถ„์— ์žˆ๋Š” age ํ•ญ๋ชฉ์„ ์ˆ˜์ •ํ•œ ๊ฒƒ

๋ฐฐ์น˜ ํ”„๋กœ์„ธ์Šค

http ๋ฐฉ์‹(์š”์ฒญ, ์‘๋‹ต) ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ๋ฒˆ์— ์—ฌ๋Ÿฌ๊ฐœ ๋ณด๋‚ด๊ฒŒ ํ•˜์—ฌ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ค„์—ฌ์คŒ ์ง€๊ธˆ๊นŒ์ง€๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ๋ณด๋ƒˆ๋Š”๋ฐ _bulk API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฌธ์„œ๋ฅผ ๋ณด๋‚ด๋ณด๊ธฐ ์ค‘๊ฐ„์— ์‹คํŒจํ•˜๋”๋ผ๋„ ๋‚˜๋จธ์ง€๋Š” ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋จ

  • bulk ์ด์šฉ, ๋‘๊ฐœ์˜ ๋ฐ์ดํ„ฐ ๋„ฃ๋Š” ์˜ˆ (_id๋Š” ๊ทœ์น™)
POST /customer/type1/_bulk
{"index":{"_id":"1"}}
{"name": "kim"}
{"index":{"_id":"2"}}
{"name": "lee"}
  • bulk ์ด์šฉ ์—ฌ๋Ÿฌ๊ฐœ์˜ update ์‚ฌํ•ญ ๋ณด๋‚ด๊ธฐ(update, delete๋“ฑ)
POST /customer/type1/_bulk
{"update": {"_id":"1"}}
{"doc": {"age": "18"}}
{"delete": {"_id":"2"}}

Search API

  • url ๋ฐฉ๋ฒ•
POST kibana_sample_data_flights/_search?q=*&sort=AvgTicketPrice

#์ž๋™์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์ด๋จ(sort์˜ default)
  • ๋ณธ๋ฌธ์„ ์ด์šฉํ•ด ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•
POST kibana sample_data_flights/_search
{
    "query": {"match_all":{}}, #๋ชจ๋“  ๊ฒฐ๊ณผ ์กฐํšŒ
    "sort": {"AvgTicketPrice": "desc"},
    "_source": ["AvgTicketPrice","FlightNum"]
    #๋ณด์—ฌ์ฃผ๊ณ  ์‹ถ์€ ํ•„๋“œ๋งŒ ๋ณด์—ฌ์ฃผ๊ฒŒํ•จ
}
POST kibana sample_data_flights/_search
{
    "query": {"match_phrase":{"DestCountry": "AU"}}, #์กฐ๊ฑด์— ๋งž๋Š” ๊ฒฐ๊ณผ ์กฐํšŒ
    "sort": {"AvgTicketPrice": "desc"},
    "_source": ["AvgTicketPrice","FlightNum", "DestCountry"]
    #๋ณด์—ฌ์ฃผ๊ณ  ์‹ถ์€ ํ•„๋“œ๋งŒ ๋ณด์—ฌ์ฃผ๊ฒŒํ•จ
}

๊ฒฐ๊ณผ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์กฐ๊ฑด์ด ์—†์œผ๋ฉด 10๊ฐœ ํ•ญ๋ชฉ๋งŒ ๋ณด์—ฌ์คŒ(default)

์•„๋ž˜์ฒ˜๋Ÿผ ๋ฐ˜๋“œ์‹œ ๋งค์น˜ํ•˜๋Š” ๊ฒƒ๊ณผ ๋งค์น˜ํ•˜์ง€ ์•Š์•„์•ผ ํ•˜๋Š” ๊ฒƒ์„ ์ง€์ •ํ•˜์—ฌ ๊ฒ€์ƒ‰ํ•  ์ˆ˜๋„ ์žˆ์Œ

POST kibana_sample_date_flights/_search
{
    "query":{
        "bool":{
            "must": {"match": {"DestCountry": "AU"}}, #์—ฌ๊ธฐ์— []๋กœ ์—ฌ๋Ÿฌ ์˜ต์…˜์„ ์ค„ ์ˆ˜๋„ ์žˆ์Œ
            "must_not": {"match": {"FlightNum": "9HY95MR"}
        }
    }
}
#์‹ค์Šต ๋ชจ์Œ

GET _search
{
  "query": {
    "match_all": {}
  }
}

GET /_cat/health?v

GET /_cat/indices?v

POST /customer/type1/1
{
  "name": "yurim"
}

GET /customer/type1/1



POST /customer/type1/_bulk
{"index":{"_id":"1"}}
{"name": "kim"}
{"index":{"_id":"2"}}
{"name": "lee"}


POST /customer/type1/_bulk
{"update": {"_id":"1"}}
{"doc": {"age": "18"}}
{"delete": {"_id":"2"}}

*** url ๋ฐฉ๋ฒ• ๋‹ค์‹œ

GET kibana_sample_data_flights/_search?q=OriginWeather:Sunny AND DestCountry:AU&_source=OriginWeather,DestCountry,AvgTicketPrice&sort=AvgTicketPrice:desc

GET tourcompany/customerlist/_search?q=*

POST tourcompany/customerlist/1 { "name": "Alfred", "phone": "010-1234-5678", "holyday_dest": "Disneyland", "departure_date": "2017/01/20" }

POST tourcompany/customerlist/2 { "name": "Huey", "phone": "010-2222-4444", "holyday_dest": "Disneyland", "departure_date": "2017/01/20" }

POST tourcompany/customerlist/3 { "name": "Naomi", "phone": "010-3333-5555", "holyday_dest": "Hawaii", "departure_date": "2017/01/10" }

POST tourcompany/customerlist/4 { "name": "Andra", "phone": "010-6666-7777", "holyday_dest": "Bora Bora", "departure_date": "2017/01/11" }

POST tourcompany/customerlist/5 { "name": "Paul", "phone": "010-9999-8888", "holyday_dest": "Hawaii", "departure_date": "2017/01/10" }

POST tourcompany/customerlist/6 { "name": "Colin", "phone": "010-5555-4444", "holyday_dest": "Venice", "departure_date": "2017/01/16" }

#ํ€ด์ฆˆ 1

#์—˜๋ผ์Šคํ‹ฑ์„œ์น˜ CRUD๋ฌธ์ œ์™€ ํ•ด์„ค #BoraBora ์—ฌํ–‰์ž ๋ช…๋‹จ์„ ์‚ญ์ œํ•˜๋ผ DELETE tourcompany/customerlist/4

#Hawaii ๋‹จ์ฒด ๊ด€๋žŒ๊ฐ ์ถœ๋ฐœ์ผ์„ 2017/01/10 ์—์„œ 2017/01/17๋กœ ์ˆ˜์ •ํ•˜๋ผ POST tourcompany/customerlist/3/_update { "doc": { "departure_date": "2017/01/17" } }

POST tourcompany/customerlist/5/_update { "doc": { "departure_date": "2017/01/17" } }

#ํœด์ผ ์—ฌํ–‰์„ ๋””์ฆˆ๋‹ˆ๋žœ๋“œ๋กœ ๋– ๋‚˜๋Š” ์‚ฌ๋žŒ๋“ค์˜ ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ๋ฅผ ์กฐํšŒํ•ด๋ผ

#ํ€ด์ฆˆ 2 ํ€ด์ฆˆ1์—์„œ ํ–ˆ๋˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ค ๋‚ ๋ผ๊ฐ”๋Œ€. ์ด๋ฅผ ๋Œ€๋น„ bulk๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค๊ณ  api๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—…๋กœ๋“œํ•ด๋ณด์ž *์ฃผ์˜ ์ค‘๊ด„ํ˜ธ ์•ˆ์— ๋ถ™์—ฌ์จ์•ผํ•จ POST tourcompany/customerlist/_bulk {"index":{"_id":"1"}} {"name": "Alfred", "phone": "010-1234-5678", "holyday_dest": "Disneyland", "departure_date": "2017/01/20"} {"index":{"_id":"2"}} {"name": "Huey", "phone": "010-2222-4444", "holyday_dest": "Disneyland", "departure_date": "2017/01/20"} {"index":{"_id":"3"}} {"name": "Naomi", "phone": "010-3333-5555", "holyday_dest": "Hawaii", "departure_date": "2017/01/10"} {"index":{"_id":"4"}} {"name": "Andra", "phone": "010-6666-7777", "holyday_dest": "Bora Bora", "departure_date": "2017/01/11"} {"index":{"_id":"5"}} {"name": "Paul", "phone": "010-9999-8888", "holyday_dest": "Hawaii", "departure_date": "2017/01/10"} {"index":{"_id":"6"}} {"name": "Colin", "phone": "010-5555-4444", "holyday_dest": "Venice", departure_date": "2017/01/16"}

tourcompany ์ธ๋ฑ์Šค์—์„œ 010 3333 5555 ๋ฅผ ๊ฒ€์ƒ‰ํ•˜์‹ญ์‹œ์˜ค

GET tourcompany/customerlist/_search?q="010-3333-5555"

ํœด์ผ ์—ฌํ–‰์„ ๋””์ฆˆ๋‹ˆ๋žœ๋“œ๋กœ ๋– ๋‚˜๋Š” ์‚ฌ๋žŒ๋“ค์˜ ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ๋ฅผ ์กฐํšŒํ•˜์‹ญ์‹œ์˜ค (phone ํ•„๋“œ๋งŒ ์ถœ๋ ฅ

GET tourcompany/customerlist/_search?q="Disneyland"&_source=phone,holyday_dest

departure date ๊ฐ€ 2017 01 10 ๊ณผ 2017 01 11 ์ธ ์‚ฌ๋žŒ์„ ์กฐํšŒํ•˜๊ณ  ์ด๋ฆ„ ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜์‹ญ์‹œ์˜ค (name ๊ณผ departure date ํ•„๋“œ๋งŒ ์ถœ๋ ฅ GET tourcompany/customerlist/_search?q="2017/01/10" OR "2017/01/11"&_source=name, departure_date&sort=name.keyword

sort=nameํ•˜๋ฉด ์—๋Ÿฌ๋‚  ๊ฒƒ ์•„๋งˆ nameํ•„๋“œ๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ธ์‹์„ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๊ฐ™์Œ(์—˜๋ผ์Šคํ‹ฑ์„œ์น˜ ๋ฒ„์ „ 6๋ถ€ํ„ฐ ๊ทธ๋Ÿผ) name.keyword๋กœ ์จ์ฃผ๊ธฐ

BoraBora ์—ฌํ–‰์€ ๊ณตํ•ญํ…Œ๋Ÿฌ ์‚ฌํƒœ๋กœ ์ทจ์†Œ๋์Šต๋‹ˆ๋‹ค BoraBora ์—ฌํ–‰์ž์˜ ๋ช…๋‹จ์„ ์‚ญ์ œํ•ด์ฃผ์‹ญ์‹œ์˜ค

Bora Bora์ธ ์•  ํ™•์ธ GET tourcompany/customerlist/_search?q="Bora Bora"

POST tourcompany/customerlist/_delete_by_query?q="Bora Bora"

Hawaii ๋‹จ์ฒด ๊ด€๋žŒ๊ฐ์˜ ์š”์ฒญ์œผ๋กœ ์ถœ๋ฐœ์ผ์ด ์กฐ์ •๋์Šต๋‹ˆ๋‹ค 2017 01 10 ์— ์ถœ๋ฐœํ•˜๋Š” Hawaii ์˜ ์ถœ๋ฐœ์ผ์„ 2017 01 17 ์ผ๋กœ ์ˆ˜์ •ํ•ด์ฃผ์‹ญ์‹œ์˜ค

์Šคํฌ๋ฆฝํŠธ ๋„ฃ๊ธฐ

POST tourcompany/customerlist/_update_by_query { "script":{"inline":"ctx._source.departure_date='2017/01/17'", "lang":"painless"}, "query":{ "bool":{ "must":[ {"match": {"departure_date":"2017/01/10"}}, {"match": {"holyday_dest":"Hawaii"}} ] } } }

์†Œ์Šค ์•ˆ์— departure_date์— ํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ "lang":"painless"๋Š” language๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์ƒ๊ด€ํ•˜์ง€ ์•Š๊ณ  ๋ผ๋Š” ํ˜•์‹ ๊ฐ™์€๊ฒƒ์ž„(์ค‘์š”x)

์‹คํ–‰ํ•ด๋ณด๋ฉด 2๊ฐœ update ๋œ ๊ฒƒ ํ™•์ธ ๊ฐ€๋Šฅ

kibana ์‹œ์ž‘ํ•˜๊ธฐ ์–ด๋–ค index ํƒ์ƒ‰ํ• ์ง€ ์„ค์ •ํ•ด์•ผํ•จ

http://127.0.0.1:9200/_cat/indices

ํ• ๋‹น๋œ๋ฉ”๋ชจ๋ฆฌ, ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š”๋ฉ”๋ชจ๋ฆฌ

http://127.0.0.1:5602/status

Management์—์„œ kibana-index patterns create index pattern ์–ด๋–ค ์ธ๋ฑ์Šค๋ฅผ ํ•  ๊ฒƒ์ธ๊ฐ€ -> Time Filter

time filter๋Š” ์‹œ๊ณ„์—ด (ํƒ€์ž„์‹œ๋ฆฌ์ฆˆ) ๊ด€๋ จ ๋ฐ์ดํ„ฐ ๋ถ„์„์—์„œ ์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ ์žˆ๋Š”๋ฐ ์‹œ๊ฐ„์— ๋”ฐ๋ผ ๋ถ„์„์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ : ์ฃผ์‹, ์‹œ์„ธ, ๋กœ๊ทธ ๋“ฑ

์ด ์ •๋ฆฌ: ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ index pattern์— ๋“ฑ๋กํ•ด์ค˜์•ผ ํ•จ -> ์ž˜ ์กฐํšŒ๋˜๋Š”์ง€ ํ™•์ธ -----07/14 ์ˆ˜๊ฐ•



๊ธฐ๋ณธ API(index, document CRUD)

// ํด๋Ÿฌ์Šคํ„ฐ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ์ธ๋ฑ์Šค(index) ์กฐํšŒ curl -XGET 'localhost:9200/_cat/indices?v'

// ํด๋Ÿฌ์Šคํ„ฐ์— ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ด๋ฅผ ์กฐํšŒํ•ด๋ณด๊ธฐ

curl -XPUT 'localhost:9200/seereal?pretty' curl -XGET 'localhost:9200/_cat/indices?v'

// ์ธ๋ฑ์Šค์— Type์„ ์ƒ์„ฑํ•˜๊ณ  Document(๊ด€๊ณ„ํ˜• DB์—์„œ Record์— ํ•ด๋‹น)๋ฅผ ๋„ฃ์–ด๋ณด๊ธฐ(์ƒ‰์ธํ™” ์ž‘์—…) curl -XPOST 'localhost:9200/seereal/weather/1?pretty' -H 'Content-Type: application/json' -d'{"date": "2020-07-10"}'

seereal ์ธ๋ฑ์Šค์— weather Type์— 1๋ฒˆ _id์— ์ƒ‰์ธํ™”ํ•˜๋Š” ๊ณผ์ • (_id ๊ฐ’์„ ๋ช…์‹œํ•˜์ง€ ์•Š์œผ๋ฉด ์ž„์˜์˜ ๋ฌธ์ž์—ด์„ _id๋กœ ํ• ๋‹นํ•จ)

  • -d ์˜ต์…˜

--data-binary์˜ ์ถ•์•ฝ

์ถ”๊ฐ€ํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋ช…์‹œ

or

// ๋ฐ์ดํ„ฐ๋ฅผ json ํ˜•์‹์˜ ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด์„œ API๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹๋„ ๊ฐ€๋Šฅ

curl -XPOST 'localhost:9200/seereal/weather/2?pretty' -H 'Content-Type: application/json' -d @data.json

// ์ง€๊ธˆ๊นŒ์ง€ ์ถ”๊ฐ€ํ•œ Docuent ์กฐํšŒ curl -XGET 'localhost:9200/seereal/_search?pretty'

// _id๋ฅผ ์ด์šฉํ•ด document๋ฅผ ๊ฒ€์ƒ‰ curl -XGET 'localhost:9200/seereal/weather/1?pretty'

// Document์˜ ์‘๋‹ต ๊ฒฐ๊ณผ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ• curl -XGET 'localhost:9200/seereal/weather/2?pretty&filter_path=_source'

curl -XGET 'localhost:9200/seereal/weather/2?pretty&filter_path=_source.celsius'

// Document ์ˆ˜์ • curl -XPUT 'localhost:9200/seereal/weather/2?pretty' -H 'Content-Type: application/json' -d '{ "date": "2017-04-04" }'

// ์ธ๋ฑ์Šค, Document ์‚ญ์ œ

  • ํŠน์ • _id์˜ Document ์‚ญ์ œ ๋ฐฉ๋ฒ•

curl -XDELETE 'localhost:9200/seereal/weather/1?pretty'

  • ์ธ๋ฑ์Šค ์‚ญ์ œ ๋ฐฉ๋ฒ• curl -XDELETE 'localhost:9200/seereal?pretty'

Q & A


Reference

*Elasticsearch ์ž…๋ฌธํ•˜๊ธฐ(1) ํด๋Ÿฌ์Šคํ„ฐ

*Elasticsearch ์ž…๋ฌธํ•˜๊ธฐ(2)

  • ์ธํ”„๋Ÿฐ ๊ฐ•์˜ 'IT์ธ์„ ์œ„ํ•œ ELK ํ†ตํ•ฉ๋กœ๊ทธ์‹œ์Šคํ…œ ๊ตฌ์ถ•๊ณผ ํ™œ์šฉ'