Index CRUD
Index๋ ElasticSearch์์ ํ ์ด๋ธ์ ์ญํ ์ ํ๋ค. ๋จผ์ Index์ ๋ํ CRUD๋ฅผ ์ดํด๋ณด๊ณ , ๋ ์ฝ๋์ ๋์๋๋ Document์ CRUD๋ฅผ ์ดํด๋ณด์.
Index ์กฐํ
GET _cat/indices
# include column headings
GET _cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open my-index xxxx 1 1 35961 0 56mb 28mb
...
ES ํด๋ฌ์คํฐ์ ์กด์ฌํ๋ ๋ชจ๋ Index์ ์ ๋ณด๋ฅผ ํ์ธํ๋ค. (1) ์ค๋์ ๋ํ ์ ๋ณด, (2) Document์ ๋ํ ์ ๋ณด, (3) Storage์ ๋ํ ์ ๋ณด๋ฅผ ํ์ธํ ์ ์๋ค.
GET _cat/indicies/<index-name>
๋์ <index-name>๋ฅผ ์ถ๊ฐํด์ ํน์ Index ํ๋๋ง ์กฐํํ ์๋ ์๋ค.
Create
Index ์์ฑํ๊ธฐ ์ํด์ Index ๋ช
์ธ๋ฅผ ํจ๊ป ์ ๋ฌํด์ผ ํ๋ค. Index ๋ช
์ธ์๋ settings, mappings ์ ๋ณด๊ฐ ํฌํจ๋๋ค.
PUT <index-name>
์ฐธ๊ณ ๋ก Create/Update ๋๋ค PUT์ผ๋ก ํด์ผ ํ๋ค!
Index๋ ๋ช ์ธ ์์ด๋ ๋ฐ๋ก ๋ง๋ค ์๋ ์๋ค!
Index Settings
Index๋ฅผ ์ฒ์ ๋ง๋ค ๋์ ์ ๋ณด๊ฐ ๋ด๊ธด๋ค.
PUT <index-name>
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
- ์ค๋ ์:
number_of_shards,number_of_replicas - ์์ฑ์ผ:
creation_date - Analyzer ์ ๋ณด
Index Mappings
Index์ ์ ์ฅ๋๋ Document์ ๊ตฌ์กฐ์ ๋ํ ์ ๋ณด๊ฐ ๋ด๊ธด๋ค.
PUT <index-name>
{
"mappings": {
"properties": {
"id": { "type": "text" },
"name": { "type": "text" },
"count": { "type": "integer" }
}
}
}
Index์ ์
๋ ฅ๋๋ JSON ์ ๋ณด๋ฅผ ๋ํ๋จผํธ๋ก ๋ณํํด ์ ์ฅํ ๋ ์ด mapping ์ ๋ณด๋ฅผ ์ฐธ๊ณ ํด ์ ์ฉํ๋ค.
๋ค์ Document์ CRUD์์ ์ดํด๋ณผ ๊ฒ์ด์ง๋ง, ES๋ โDynamic Mappingโ์ ์ง์ํ๊ธฐ ๋๋ฌธ์, mapping์ ์๋ ํ๋๊ฐ ๋ค์ด์ค๋๋ผ๋ Document๋ก ์ ์ฅ๋๋ค.
์ฆ, ์ฒ๋ฆฌํ๋ ํ๋์ ๋ํ ์ ๋ณด๊ฐ mapping์ ๋ช
์๋์ด ์์ผ๋ฉด ํด๋น ํ์
์ ์ฌ์ฉํ๊ณ , ๋ช
์๋์ด ์์ง ์์ผ๋ฉด, ํด๋น ํ๋๊ฐ ํฌํจ๋๋ ๊ฐ์ฅ ๋์ ๋ฒ์์ ๋ฐ์ดํฐ ํ์
์ ์ ํํ ํ Index์ mapping์ ์ถ๊ฐํ๋ค.
โDynamic Mappingโ์ ์๋ฅผ ์ข๋ ์ดํด๋ณด๋ฉด,
"123"์ด ๋ค์ด์จ๋ค๋ฉด, ํด๋น ํ๋๋longํ์ ์ผ๋ก ์ธํ ๋๊ณ ,"3.14"๋ก ๋ค์ด์จ๋ค๋ฉด, ํ๋๋doubleํ์ ์ผ๋ก,"haha hoho"๊ฐ ๋ค์ด์จ๋ค๋ฉด ํด๋น ํ๋๋textํ์ ์ผ๋ก ์ธํ ๋๋ค.
Read
์์ฑํ Index์ ์์ธ ์ ๋ณด๋ฅผ ๋ณด๋ ๋ฐฉ๋ฒ์ด๋ค.
GET <index-name>
{
"my-index" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1684160351512",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "PIgxLmgdS42BhS2nEoycoQ",
"version" : {
"created" : "7090199"
},
"provided_name" : "my-index"
}
}
}
}
์ํ๋ค๋ฉด settings, mappings ์ ๋ณด๋ง ์กฐํํ ์๋ ์๋ค.
GET <index-name>/_settings
GET <index-name>/_mappings
Update
๋ณ๊ฒฝํ๊ณ ์ ํ๋ Index์ settings, mappings๋ฅผ ๋ค์ ์ ์ด์ฃผ๋ฉด ๋๋ค.
PUT <index-name>/_settings
{
"index": {
"number_of_replicas": 2
}
}
๋จ, number_of_shards๋ ๋ณ๊ฒฝํ ์ ์๋ค.
mapping์ ๊ฒฝ์ฐ ํ๋ ๋ช
์ธ๋ฅผ ์ถ๊ฐํ๋ ๊ฒ๋ง ๊ฐ๋ฅํ๋ค.
Delete
์ญ์ ๋ ๊ฐ๋จํ๋ค!
DELETE <index-name>