Elasticsearch

Simba_bยท2022๋…„ 10์›” 11์ผ
0

Elatsticsearch

๋ชฉ๋ก ๋ณด๊ธฐ
1/5
post-thumbnail

Elastic Search

Apache Lucene๊ธฐ๋ฐ˜ Java์˜คํ”ˆ์†Œ์Šค ๋ถ„์‚ฐ ๊ฒ€์ƒ‰ ์—”์ง„์œผ๋กœ ์‹ค์‹œ๊ฐ„ ์ €์žฅ, ๊ฒ€์ƒ‰, ๋ถ„์„ ๊ฐ€๋Šฅ

Elasticsearch๋Š” ํ…์ŠคํŠธ๋ฅผ ํŒŒ์‹ฑํ•ด์„œ ๊ฒ€์ƒ‰์–ด ์‚ฌ์ „์„ ๋งŒ๋“  ๋‹ค์Œ์— inverted index ๋ฐฉ์‹์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ์ €์žฅ

ํ‚ค์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ์ƒค๋“œ๊ฐ€ ๊ตฌ์„ฑ๋˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์‚ฐํ•œ๋‹ค. ์ธ๋ฑ์Šค๋Š” ๊ฐ๊ฐ์˜ ์ƒค๋“œ๋งˆ๋‹ค ๊ตฌ์„ฑ๋œ๋‹ค. ๊ฐ๊ฐ์˜ ์ƒค๋“œ๋Š” 0๊ฐœ ์ด์ƒ์˜ ๋ณต์ œ๋ณธ์„ ๊ฐ€์ง

elasticsearch๋Š” ํด๋Ÿฌ์Šคํ„ฐ๋ง์„ ์ง€์›ํ•˜๋ฉฐ ํด๋Ÿฌ์Šคํ„ฐ๊ฐ€ ๊ฐ€๋™๋  ๋•Œ ์—ฌ๋Ÿฌ ๋…ธ๋“œ ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ๋งˆ์Šคํ„ฐ ๋…ธ๋“œ๋กœ ์„ ์ถœ

๋Œ€๋Ÿ‰์˜ ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰ ๊ฐ€๋Šฅํ•˜๋ฉฐ ์ „๋ฌธ๊ฒ€์ƒ‰, ๊ตฌ์กฐ๊ฒ€์ƒ‰ ๋ชจ๋‘ ์ง€์›

Transaction Rollback์„ ์ง€์›ํ•˜์ง€ ์•ˆํ•จ, ๋ฐ์ดํ„ฐ์˜ ์—…๋ฐ์ดํŠธ๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•ˆํ•จ(์—…๋ฐ์ดํŠธ ๋ช…๋ น์ด ์˜ฌ ๊ฒฝ์šฐ ๊ธฐ์กด ๋ฌธ์„œ ์‚ญ์ œ ํ›„ ์ƒˆ๋กœ์šด ๋ฌธ์„œ ์ƒ์„ฑ)

http ํ”„๋กœํ† ์ฝœ๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ REST API์ง€์› ๊ณ ์œ  URL๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ PUT, POST, GET, DELETE ๋ฅผ ์ด์šฉํ•ด์„œ ์ž์›์„ ์ฒ˜๋ฆฌ

Elasticsearch๋Š” ์ž๋ฐ”๋กœ ๊ฐœ๋ฐœ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฐ” ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•œ ํ™˜๊ฒฝ์ด๋ผ๋ฉด ์–ด๋””์„œ๋“  ๊ตฌ๋™์ด ๊ฐ€๋Šฅ





Elastic Search ์šฉ์–ด

Relational DatabaseElasticsearch
DatabaseIndex
TableType
RowDocument
ColumnField
IndexAnalyze
Primary key_id
SchemaMapping
Physical partitionShard
Logical partitionRoute
RelationalParent/child, Nested
SQLQuery DSL(Domain Specific Language)


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

  • ES์—์„œ ๊ฐ€์žฅ ํฐ ์‹œ์Šคํ…œ ๋‹จ์œ„ ์ตœ์†Œ ํ•˜๋‚˜ ์ด์ƒ์˜ ๋…ธ๋“œ๋กœ ์ด๋ฃจ์–ด์ง
  • ์„œ๋กœ ๋‹ค๋ฅธ ํด๋Ÿฌ์Šคํ„ฐ๋Š” ๋ฐ์ดํ„ฐ ์ ‘๊ทผ, ๊ตํ™˜์„ ํ•  ์ˆ˜ ์—†๋Š” ๋…๋ฆฝ์  ์‹œ์Šคํ…œ
  • ์—ฌ๋Ÿฌ ๋Œ€์˜ ์„œ๋ฒ„๊ฐ€ ํ•˜๋‚˜์˜ ํด๋Ÿฌ์Šคํ„ฐ ๊ตฌ์„ฑ, ํ•œ ์„œ๋ฒ„์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํด๋Ÿฌ์Šคํ„ฐ๊ฐ€ ์กด์žฌ ํ•  ์ˆ˜ ์žˆ์Œ

๋…ธ๋“œ(node)

  • ES๋ฅผ ๊ตฌ์„ฑํ•œ๋Š” ํ•˜๋‚˜์˜ ๋‹จ์œ„ ํ”„๋กœ์„ธ์Šค
  • ํ•˜๋“œ์›จ์–ด ๋ฐ•์Šค๋กœ ์ƒ๊ฐ (ํ•œ ์ปดํ“จํ„ฐ)
  • Master-eligible, Data, Ingest, Tribe ๋…ธ๋“œ๋กœ ๊ตฌ๋ถ„
    • Master-eligible : ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์ œ์–ดํ•˜๋Š” ๋งˆ์Šคํ„ฐ๋กœ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๋…ธ๋“œ
      ์ธ๋ฑ์Šค ์ƒ์„ฑ, ์‚ญ์ œ, ํด๋Ÿฌ์Šคํ„ฐ ๋…ธ๋“œ๋“ค์˜ ์ถ”์  ๋ฐ ๊ด€๋ฆฌ, ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์‹œ ์ƒค๋“œ ํ• ๋‹น
    • Data node : ๋ฐ์ดํ„ฐ์™€ ๊ด€๋ จ๋œ CRUD ์ž‘์—…๊ณผ ๊ด€๋ จ, CPU, ๋ฉ”๋ชจ๋ฆฌ ๋“ฑ ์ž์› ์†Œ๋ชจํ•˜๋ฏ€๋กœ ๋ชจ๋‹ˆํ„ฐ๋ง ํ•„์š”, mater node์™€ ๋ถ„๋ฆฌ๋˜๋Š” ๊ฒƒ์ด ์ข‹์Œ
    • Ingest node : ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜ํ•˜๋Š” ๋“ฑ ์‚ฌ์ „ ์ฒ˜๋ฆฌ ํŒŒ์ดํ”„๋ผ์ธ ์‹คํ–‰
    • Cordination only node : data node, master-eligible node์˜ ์ผ์„ ๋Œ€์‹ ํ•จ, ๋Œ€๊ทœ๋ชจ ํด๋Ÿฌ์Šคํ„ฐ์—์„œ ์ด์ ์ด ์žˆ์Œ

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

  • sharding : ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์‚ฐํ•ด์„œ ์ €์žฅ, ES์—์„œ scale-out์„ ์œ„ํ•ด index๋ฅผ ์—ฌ๋Ÿฌ shard๋กœ ์ชผ๊ฐ ๊ฒƒ์œผ๋กœ ๊ธฐ๋ณธ์ ์œผ๋กœ 1๊ฐœ, ๊ฒ€์ƒ‰์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ์œ„ํ•ด ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ƒค๋“œ ๊ฐฏ์ˆ˜ ์กฐ์ •ํ•˜๋Š” ํŠœ๋‹ ๊ฐ€๋Šฅ
  • replica : ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ shard, ๋…ธ๋“œ ์†์‹คํ–ˆ์„ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ์‹ ๋ขฐ์„ฑ์„ ์œ„ํ•ด ์ƒค๋“œ๋“ค์„ ๋ณต์ œ, replica๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋…ธ๋“œ์— ์กด์žฌํ•  ๊ฒƒ์„ ๊ถŒ์žฅ

Elasticsearch ์•„ํ‚คํ…์ณ



๐Ÿ”Elastic Search ํŠน์ง•

  • Scale out
    • ์ƒค๋“œ(Shard)๋ฅผ ํ†ตํ•ด ๊ทœ๋ชจ ์ˆ˜ํ‰์ ์œผ๋กœ ๋Š˜์–ด๋‚  ์ˆ˜ ์žˆ์Œ
  • ๊ณ ๊ฐ€์šฉ์„ฑ
    • Replica๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ์˜ ์•ˆ์ •์„ฑ ๋ณด์žฅ
  • Schema Free
    • Json ๋ฌธ์„œ๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰ ์ˆ˜ํ–‰ํ•ด ์Šคํ‚ค๋งˆ ๊ฐœ๋…์ด ์—†์Œ
    • ์š”์ฒญ๊ณผ ์‘๋‹ต์„ JSON๋ฌธ์„œ๋กœ ์ฃผ๊ณ  ๋ฐ›๊ณ  ์†Œ์Šค ์ €์žฅ๋„ JSON , JSON๋ฌธ์„œ ๋„˜๊ฒจ์ฃผ๋ฉด ์ž๋™ ์ธ๋ฑ์‹ฑ
  • Multi-tenancy
    • ํ•˜๋‚˜์˜ elasticsearch์„œ๋ฒ„์— ์—ฌ๋Ÿฌ ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅ, ์—ฌ๋Ÿฌ ์ธ๋ฑ์Šค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์˜ ์ฟผ๋ฆฌ๋กœ ๊ฒ€์ƒ‰๊ฐ€๋Šฅ
  • ํ™•์žฅ์„ฑ๊ณผ ์œ ์—ฐ์„ฑ
    • ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์ด์šฉํ•ด ๊ธฐ๋Šฅ ํ™•์žฅ
    • ๋‹ค๋งŒ ์ƒค๋“œ ์ˆ˜๋Š” ์ธ๋ฑ์Šค ๋ณ„๋กœ ๊ณ ์ •(์ˆ˜์ • ๋ถˆ๊ฐ€) , ์ดˆ๊ธฐ์— ๋…ธ๋“œ ์ˆ˜์™€ ํ–ฅ์šฐ ์„œ๋ฒ„ ํ™•์žฅ ๊ณ ๋ คํ•ด ํ• ๋‹น
  • ์—ญ์ƒ‰์ธ(inverted index)
    • ๋ฌธ์„œ ๋‚ด์˜ ๋ฌธ์ž์™€ ๊ฐ™์€ ๋‚ด์šฉ๋ฌผ์˜ ๋งตํ•‘ ์ •๋ณด๋ฅผ ์ƒ‰์ธ(๋‹จ์–ด ๋ณ„ ์ƒ‰์ธ ํŽ˜์ด์ง€)
  • Restful
    • ๋ฐ์ดํ„ฐ CRUD์ž‘์—…์€ HTTP Restful API๋ฅผ ํ†ตํ•ด ์ˆ˜ํ–‰
      Dataย CRUDย Elasticsearch Restful
      ย SELECTGETย 
      ย INSERTย PUT
      ย UPDATEPOST
      ย DELETEDELETE

Not RESTFul

์ž…๋ ฅ : http://user.com/input.jsp?name=kim&age=38&gender=m

์กฐํšŒ : http://user.com/get.jsp?name=kim

์‚ญ์ œ : http://user.com/delete.jsp?name=kim

RESTFul

์ž…๋ ฅ : PUT http://user.com/kim -d {"name":"kim", "age":38, "gender":"m"}

์กฐํšŒ : GET http://user.com/kim

์‚ญ์ œ : DELETE http://user.com/kim


๐Ÿ”ELK == Elastic Search, Logstash, Kibana

ELK Stack์€ย Elastic Search, Logstash, Kibana๋ฅผ ํ†ต์นญํ•˜๋Š” ๋ง์ธ๋ฐ, ๋ฐ์ดํ„ฐ ๋กœ๊ทธ๋“ค์ด logstash๋ฅผ ๊ฑฐ์ณ ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜์— ์ €์žฅ๋˜๊ณ , ์ด๋ฅผ ์‹œ๊ฐํ™”ํ•˜์—ฌ Kibana์—์„œ ๋ณด์—ฌ์ค€๋‹ค.

  • ์žฅ์ 
    • ๊ฐ•๋ ฅํ•œ ์œ ์—ฐ์„ฑ๊ณผ ํ˜ธํ™˜์„ฑ
    • ์ž์œ ์Šคํ‚ค๋งˆ(JSON)
    • ํ™•์žฅ ๊ฐ€๋Šฅ DB
    • ๋ฐ์ดํ„ฐ์ฒ˜๋ฆฌ์ ˆ์ฐจ ๊ฐœ๋ณ„ ์„ค์ •
    • Kibana๋กœ ๋ฐ”๋กœ ์‹œ๊ฐํ™” ๊ฐ€๋Šฅ
    • ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
      (๋ฉ”์„ธ์ง€ ํ์™€ ๊ฒฐํ•ฉ)
  • ๋‹จ์ 
    • ์ดˆ๊ธฐ๋ฐ์ดํ„ฐ ๊ตฌ์„ฑ ๋ฐ ์ด๊ด€๋ฌธ์ œ(ํ˜„์žฌ ๋ฌด๊ด€)
    • ์ปค๋„๋ณ€์ˆ˜ ๋ถˆํ•„์š”ํ•œ ์‚ฌ์šฉ
    • ์‹œ๊ฐ„๋Œ€ ์ผ๊ด€์„ฑ์ด ๊นจ์ง(๋ฐ์ดํ„ฐ ๋„˜๊ธฐ๋Š” ๊ณผ์ •)

๐Ÿ” Logstash

๋ฐ์ดํ„ฐ ์ ์žฌ
elasticsearch MySQL ๋™๊ธฐํ™” ๊ฐ€๋Šฅ

์‹ค์‹œ๊ฐ„ ํŒŒ์ดํ”„๋ผ์ธ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ์—”์ง„ ์˜คํ”ˆ์†Œ์Šค์ด๋‹ค. logstash ์„œ๋กœ ๋‹ค๋ฅธ ์†Œ์Šค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋™์ ์œผ๋กœ ํ†ตํ•ฉํ•˜๊ณ  ์›ํ•˜๋Š” ๋Œ€์ƒ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๊ทœํ™” ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค์–‘ํ•œ ์ž…๋ ฅ๊ณผ ํ•„ํ„ฐ ๋ฐ ์ถœ๋ ฅ ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ํ†ตํ•ด, ๋ชจ๋“  ์œ ํ˜•์˜ ์ด๋ฒคํŠธ๋ฅผ ๋ณด๊ฐ• ๋ณ€ํ™˜ ๊ฐ€๋Šฅ

ES + Kibana ์‹œ๋„ˆ์ง€์™€ ํ•จ๊ป˜ scale-up ๊ฐ€๋Šฅ

Logstash๋Š” ๋‹ค์–‘ํ•œ ์†Œ์Šค์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•ด ์ด๋ฅผ ๋ณ€ํ™˜ํ•˜์—ฌ, ์ €์žฅ์†Œ๋กœ ์ „๋‹ฌํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์†Œ์Šค์˜ ์ข…๋ฅ˜, ์‚ฌ์ด์ฆˆ, ํ˜•ํƒœ์— ๊ด€๊ณ„ ์—†์ด ์ˆ˜์ง‘ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ํŠน์ง•์ ์ด๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ๋กœ๊ทธ ์Šคํƒœ์‹œ๋ฅผ ๊ฑฐ์น  ๋•Œ ํ•„ํ„ฐ๋ง์ด ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ํ•„ํ„ฐ๋ฅผ ํ†ตํ•ด ๊ฐ ์ด๋ฒคํŠธ๋ฅผ ๊ณตํ†ต ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜, ํ†ตํ•ฉํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

logstash์˜ pipeline



ES๊ฒ€์ƒ‰ ์š”์ฒญ ์ผ๋ฐ˜์  ๊ตฌ์กฐ

  1. Query step ์›ํ•˜๋Š” ์กฐ๊ฑด์— ๋งž๋Š” ๋ฌธ์„œ๋งŒ ํ•„ํ„ฐ๋งํ•˜๋Š” ๋‹จ๊ณ„๋กœ, ๊ฐœ๋… ์ƒ ES ์—์„œ๋Š”ย query phaseย ์— ๋Œ€์‘๋œ๋‹ค.
  2. Decision step ํ•„ํ„ฐ๋ง ๋œ ๋ฌธ์„œ๋ฅผ ๋Œ€์ƒ์œผ๋กœ, ์ •๋ ฌ ๋ฐ ํŽ˜์ด์ง•์„ ์ง„ํ–‰ํ•˜์—ฌ ์ตœ์ข… ๋ฌธ์„œ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.
  3. Fetch step ์ตœ์ข… ๋ฌธ์„œ์˜ ํ•„๋“œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ , highlighting ๋“ฑ์„ ์ง„ํ–‰ํ•˜์—ฌ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค.Decision step + fetch step์ด ES ์—์„œ๋Š”ย fetch phaseย ์— ๋Œ€์‘๋œ๋‹ค.
GET [์ธ๋ฑ์Šค์ด๋ฆ„]/_search
{
    "query" : {
    	"match_all" : {}
    },
    "sort": [
    	{
    	"[ํ•„๋“œ๋ช…]": {
        	"order" : "desc" #์˜ค๋ฆ„์ฐจ์ˆœ: asc, ๋‚ด๋ฆผ์ฐจ์ˆœ: desc
            }
        }
    ]    
}

๐Ÿ”REST API ์‚ฌ์šฉํ•˜๊ธฐ_ curl /kibana

๐Ÿ› ๏ธcurl

command line data transfer tool, Linux / Unix ๊ณ„์—ด ๋ฐ Windows๋“ฑ ์ฃผ์š” OS์—์„œ ๊ตฌ๋™

๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ ์—…๋กœ๋“œ ๊ฐ€๋Šฅ, ์ „์†ก ์žฌ๊ฐœ ๋Œ€์—ญํญ ์ œํ•œ, ํ”„๋ก์‹œ ์ง€์› ์‚ฌ์šฉ์ž ์ธ์ฆ ๋“ฑ ์˜ต์…˜ ๋งŽ์Œ

curl๋ช…๋ น์„ ์ด์šฉํ•ด elasticsearch ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ตœ์ƒ์œ„ ๊ฒฝ๋กœ ํ˜ธ์ถœ โ€”> ํด๋Ÿฌ์Šคํ„ฐ ์ •๋ณด jsonํ˜•์‹ return
jdbc ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.
curl [options][URL...] ์ด๋Ÿฐ์‹์œผ๋กœ ์ž‘์„ฑ

$ curl -XGET "http://localhost:9200"
{
  "name" : "DESKTOP",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "________________",
  "version" : {
    "number" : "7.17.7",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "______________________________",
    "build_date" : "2022-10-______________________",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

0๊ฐœ์˜ ๋Œ“๊ธ€