์ถ์ฒ : ELK ์คํ์ผ๋ก ๋ฐ์ดํฐ ๋ถ์ - Minsuk Heo
ElasticSearch๋ JVM์์์ ๋์๊ฐ๊ธฐ ๋๋ฌธ์ JDK์ค์น ํ์!!
์ด์์ฒด์ ์ ๋ง๊ฒ ์ค์น(๋ฒ์ ์ ํ๊ฐ๋ฅ)
์น์์ localhost:9200์ฃผ์๋ก ์ด๋ํด ElasticSearchํ์ธ(์ค์น๋จ)
- 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 ์์ผ๋ก ๊ธฐ์
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์ถ๊ฐ)
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๋ฅผ ๋ํด๋ผ
์ฌ๋ฌ๊ฐ์ document๋ฅผ ํ๋ฒ์ elasticsearch์ ์ฝ์ ํ๋ ๋ฐฉ๋ฒ
curl -H "Content-Type:application/json" -XPOST http://localhost:9200/_bulk --data-binary @fileName.json
์ฝ์
๋ ๊ฒ ํ์ธ ๊ฐ๋ฅ
curl -H "Content-Type:application/json" -XPUT http://localhost:9200/classes/class/_mapping?include_type_name=true -d @classesRating_mapping.json
search option ์ถ๊ฐ
curl -XGET localhost:9200/classes/class/_search
q(์ฟผ๋ฆฌ์คํธ๋ง)๋ฅผ ์ด์ฉํ์ฌ ์กฐ๊ฑด ์ถ๊ฐ ๊ฐ๋ฅ
curl -XGET localhost:9200/classes/class/_search?q=title:Network&pretty
curl -XGET localhost:9200/classes/class/_search -d ' { "query" : { "hits" : {"title" : "Network"} } } '
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" ํ์ผ์ ๊ฐ์ "stats"๋ก ํ๋ฉด ๋ชจ๋ ์ฐ์ ๊ฐ ์ถ๋ ฅ๋จ
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
curl -H "Content-Type:application/json" -XGET "localhost:9200/_search?pretty" --data-binary @stats_by_team.json