ElasticSearch

woomยท2023๋…„ 8์›” 1์ผ
0

ELK Stack

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

์ถœ์ฒ˜ : ELK ์Šคํƒ์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ถ„์„ - Minsuk Heo


๐ŸŒผ ElasticSearch

  • ์˜คํ”ˆ ์†Œ์Šค ๊ฒ€์ƒ‰ ๋ฐ ๋ถ„์„ ์—”์ง„
  • ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‹ ์†ํ•˜๊ฒŒ ์ €์žฅํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•˜๋ฉฐ, ๋ฐ์ดํ„ฐ์˜ ๋ถ„์„๊ณผ ์‹œ๊ฐํ™”๋ฅผ ์ง€์›ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ


๐Ÿ“• ElasticSearch & ํ‚ค๋ฐ”๋‚˜ ์„ค์น˜

  • ElasticSearch๋Š” JVM์œ„์—์„œ ๋Œ์•„๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— JDK์„ค์น˜ ํ•„์ˆ˜!!

  • ์šด์˜์ฒด์ œ์— ๋งž๊ฒŒ ์„ค์น˜(๋ฒ„์ „์„ ํƒ๊ฐ€๋Šฅ)

  • ์›น์—์„œ localhost:9200์ฃผ์†Œ๋กœ ์ด๋™ํ•ด ElasticSearchํ™•์ธ(์„ค์น˜๋จ)

  • ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜ ์„ค์ • (/elasticsearch-7.15.2/config/elasticsearch.yml)

๐Ÿ“™ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์กฐํšŒ ์‚ญ์ œ

  • index ์กฐํšŒ
    • curl -XGET http://localhost:9200/classes : class๋ผ๋Š” index(db)์žˆ๋Š”์ง€ Read (๋ฆฌ๋ˆ…์Šค์—์„œ๋Š” -X๋ผ๋Š” ์ ‘๋‘์‚ฌ๊ฐ€ ๋ถ™์Œ)
    • 404 error : elasticsearch์•ˆ์— ์กด์žฌํ•˜์ง€ ์•Š์Œ
    • ?pretty: ๊น”๋”ํ•˜๊ฒŒ ์ถœ๋ ฅ๊ฐ€๋Šฅ(JSONํฌ๋งท)
  • index ์ƒ์„ฑ
    • curl -XPUT http://localhost:9200/classes
    • -XGET์œผ๋กœ ํ™•์ธํ•ด๋ณด๋ฉด classes๋ผ๋Š” index์ƒ์„ฑ๋œ ๊ฒƒ ํ™•์ธ ๊ฐ€๋Šฅ
  • index ์‚ญ์ œ
    • curl -XDELETE http://localhost:9200/classes
    • ์‚ญ์ œ ํ›„ GET์œผ๋กœ ๋‹ค์‹œ ํ™•์ธํ•ด๋ณด๋ฉด 404 error
  • document(Row) ์ƒ์„ฑ
    • curl -XPOST http://localhost:9200/classes/class/1/ -d '{"title" : "Algorithm", "professor" : "John"}'
    • index๋ช…/type๋ช…/id ์ˆœ์œผ๋กœ ๊ธฐ์ž…

  • -H "Content-Type: application/json" : HTTP ์š”์ฒญ ํ—ค๋”๋กœ "Content-Type"์„ ์ง€์ •(JSON ํ˜•์‹)

๐Ÿฃ Document๊ฐ€ ํŒŒ์ผ์— ์žˆ์„ ๊ฒฝ์šฐ

curl -XPOST http://localhost:9200/classes/class/1/ -d @fileName.json


๐Ÿ“’ ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ

  • ํ•„๋“œ ์ถ”๊ฐ€
    • curl -XPOST http://localhost:9200/classes/class/1/_update?pretty -d '{ "doc" : {"unit" : 1} }'
    • update ์˜ต์…˜ (doc์ด๋ผ๋Š” property์— unit์ถ”๊ฐ€)
  • unit์„ 2๋กœ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์œ„์™€ ๋™์ผ
    • curl -XPOST http://localhost:9200/classes/class/1/_update?pretty -d '{ "doc" : {"unit" : 2} }'
  • script๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—…๋ฐ์ดํŠธ (์—ฐ์‚ฐ๊ฐ€๋Šฅ)
    • curl -XPOST http://localhost:9200/classes/class/1/_update?pretty -d '{ "script" : "ctx._source.unit += 5" }'
    • _source์˜ unitํ•„๋“œ์— 5๋ฅผ ๋”ํ•ด๋ผ

๐Ÿ“Œ Bulk

์—ฌ๋Ÿฌ๊ฐœ์˜ document๋ฅผ ํ•œ๋ฒˆ์— elasticsearch์— ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•

  • curl -H "Content-Type:application/json" -XPOST http://localhost:9200/_bulk --data-binary @fileName.json

    • _bulk์˜ต์…˜, --data-binary์ถ”๊ฐ€
    • request must be terminated by a newline ์—๋Ÿฌ ๋ฐœ์ƒ
    • json ํŒŒ์ผ ๋งˆ์ง€๋ง‰์— enter ์ถ”๊ฐ€
  • ์‚ฝ์ž…๋œ ๊ฒƒ ํ™•์ธ ๊ฐ€๋Šฅ


๐Ÿ“— ๋งคํ•‘(text, long, date, ...)

  • curl -H "Content-Type:application/json" -XPUT http://localhost:9200/classes/class/_mapping?include_type_name=true -d @classesRating_mapping.json

    • curl ๋ฆฌํ€˜์ŠคํŠธ์—์„œ ํ—ค๋”๋ฅผ ๋ช…ํ™•ํžˆ ์„ค์ •(-H "Content-Type:application/json"), mappign์„ ์ƒ์„ฑํ•  ๋•Œ์—๋Š” include_type_name์„ true๋กœ ์„ค์ •
    • "type" : "mapper_parsing_exception" ์—๋Ÿฌ ๋ฐœ์ƒ โ†’ elasticsearch๊ฐ€ mapping ํƒ€์ž… ์ค‘ string์„ ์‚ญ์ œํ•˜๊ณ  text๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์‚ฌ์šฉ
    • get๋ฐฉ์‹์œผ๋กœ ์กฐํšŒํ•ด๋ณด๋ฉด mapping์— ์ถ”๊ฐ€๋œ ๊ฒƒ ํ™•์ธ ๊ฐ€๋Šฅ

๐Ÿ“Œ ๋ฐ์ดํ„ฐ ์กฐํšŒ(search)

  • search option ์ถ”๊ฐ€

    • curl -XGET localhost:9200/classes/class/_search
    • ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์กฐํšŒ ๊ฐ€๋Šฅ
  • q(์ฟผ๋ฆฌ์ŠคํŠธ๋ง)๋ฅผ ์ด์šฉํ•˜์—ฌ ์กฐ๊ฑด ์ถ”๊ฐ€ ๊ฐ€๋Šฅ

    • curl -XGET localhost:9200/classes/class/_search?q=title:Network&pretty
    • title์ด Network์ธ ๊ฒƒ๋งŒ ์ถœ๋ ฅ๋จ
  • request body๋กœ ๊ฐ€๋Šฅ
    • curl -XGET localhost:9200/classes/class/_search -d ' { "query" : { "hits" : {"title" : "Network"} } } '

๐ŸŒผ Aggregation

ElasticSearch์˜ document์•ˆ์—์„œ ์กฐํ•ฉ์„ ํ†ตํ•ด์„œ ๊ฐ’์„ ๋„์ถœํ•  ๋•Œ ์‚ฌ์šฉ


๐Ÿ“• Metric Aggregation

์‚ฐ์ˆ ํ• ๋•Œ ์‚ฌ์šฉ (avg, max, min ...)

  • ex. curl -H "Content-Type:application/json" -XPOST "localhost:9200/_bulk" --data-binary @simple_basketball.json
  • aggregation ํ˜•์‹
    • field(column)๊ฐ€ points์ธ ๊ฒƒ์˜ ํ‰๊ท ์„ ๊ตฌํ•ด๋ผ(30+20/2)
    • size:0 : ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ •๋ณด๊ฐ€ ๋„์ถœ๋˜๋Š” ๊ฒƒ ๋Œ€์‹ ์— ์šฐ๋ฆฌ๊ฐ€ ๋ณด๊ณ ์‹ถ์€ ๊ฒฐ๊ณผ๊ฐ’๋งŒ ๋ณด๊ธฐ์œ„ํ•ด์„œ
    • aggs: aggregation
  • curl -H "Content-Type:application/json" -XGET "localhost:9200/_search?pretty" --data-binary @avg_points_aggs.json
    • aggrํŒŒ์ผ์„ ์ด์šฉํ•ด์„œ ํ‰๊ท ๊ฐ’ ์ถœ๋ ฅ
    • aggrํŒŒ์ผ์˜ "avg"๊ฐ’์„ "max", "min", "sum"๊ฐ’์œผ๋กœ ์ˆ˜์ •ํ•˜๋ฉด max, min, sum๊ฐ’ ์ถœ๋ ฅ๋จ

"aggr" ํŒŒ์ผ์˜ ๊ฐ’์„ "stats"๋กœ ํ•˜๋ฉด ๋ชจ๋“  ์‚ฐ์ˆ ๊ฐ’ ์ถœ๋ ฅ๋จ


๐Ÿ“™ Bucket Aggregation

group by(ํŒ€๋ณ„๋กœ grouping)

  • ex. basketball์ด๋ผ๋Š” index์— basketball_mapping.json ๋งคํ•‘

  • index: basketball, type: record์— Chicago์™€ LA๋ผ๋Š” ํŒ€์˜ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…

    • curl -H "Content-Type:application/json" -XPOST "localhost:9200/_bulk" --data-binary @twoteam_basketball.json
  • aggregationํ˜•์‹
    • field๊ฐ€ team์ธ ๊ฒƒ ๋ณ„๋กœ group byํ•ด๋ผ
    • "terms" aggregation ์‚ฌ์šฉ
    • "aggs"๋Œ€์‹ ์— "aggregation"์œผ๋กœ ํ•ด๋„ ๋™์ผํ•œ ๊ฒฐ๊ณผ
  • curl -H "Content-Type:application/json" -XGET "localhost:9200/_search?pretty" --data-binary @avg_points_aggs.json
    • aggrsํŒŒ์ผ์„ ์ด์šฉํ•˜์—ฌ buckets๊ฐ’ ์ถœ๋ ฅ
    • team์ด chicago์ธ doc์ˆ˜ : 2, la์ธ doc์ˆ˜ : 2

๐ŸŽ€ ์‘์šฉ(ํŒ€๋ณ„๋กœ ํ†ต๊ณ„)

Bucket Aggregation์„ ์ด์šฉํ•˜์—ฌ ํŒ€๋ณ„๋กœ groupingํ›„ Metric Aggregation์„ ์ด์šฉํ•˜์—ฌ ์‚ฐ์ˆ ๊ฐ’ ์ถœ๋ ฅ

  • curl -H "Content-Type:application/json" -XGET "localhost:9200/_search?pretty" --data-binary @stats_by_team.json

profile
Study Log ๐Ÿ“‚

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