Index๋Š” ElasticSearch์—์„œ ํ…Œ์ด๋ธ”์˜ ์—ญํ• ์„ ํ•œ๋‹ค. ๋จผ์ € Index์— ๋Œ€ํ•œ CRUD๋ฅผ ์‚ดํŽด๋ณด๊ณ , ๋ ˆ์ฝ”๋“œ์— ๋Œ€์‘๋˜๋Š” Document์˜ CRUD๋ฅผ ์‚ดํŽด๋ณด์ž.

3 minute read

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>