ElasticSearch์—์„œ ๋ ˆ์ฝ”๋“œ์˜ ์—ญํ• ์„ ํ•˜๋Š” Document์˜ CRUD์— ๋Œ€ํ•ด ์‚ดํŽด๋ณธ๋‹ค. ํ…Œ์ด๋ธ” ์—ญํ• ์„ ํ•˜๋Š” Index์— ๋Œ€ํ•œ CRUD๋Š” ์ด์ „ ํฌ์ŠคํŠธ์—์„œ ๋‹ค๋ค˜๋‹ค.

4 minute read

ElasticSearch์—์„œ ๋ ˆ์ฝ”๋“œ์˜ ์—ญํ• ์„ ํ•˜๋Š” Document์˜ CRUD์— ๋Œ€ํ•ด ์‚ดํŽด๋ณธ๋‹ค. ํ…Œ์ด๋ธ” ์—ญํ• ์„ ํ•˜๋Š” Index์— ๋Œ€ํ•œ CRUD๋Š” ์ด์ „ ํฌ์ŠคํŠธ์—์„œ ๋‹ค๋ค˜๋‹ค.

Create

POST <index-name>/_doc
{
  "id": 11,
  "name": "foo"
}
{
  "_index" : "my-index",
  "_type" : "_doc",
  "_id" : "nuJMKogBDapnWhkqYzBD",
  "_version" : 1,
}

์ƒ์„ฑํ•  Document์˜ ID๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

POST <index-name>/_doc/<id>
{
  "id": 11,
  "name": "foo"
}
{
  "_index" : "my-index",
  "_type" : "_doc",
  "_id" : "123",
  "_version" : 1,
}

Read

Index์— ์ €์žฅ๋œ ์ „์ฒด Document๋ฅผ ์กฐํšŒํ•˜๋Š” API๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

GET <index-name>/_search
{
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      { ... },
      { ... },
      { ... },
      { ... },
    ]
  }
}

๋งŒ์•ฝ Document ํ•˜๋‚˜๋งŒ ๋‹จ๋…์œผ๋กœ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด, ์•„๋ž˜์˜ API๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.

GET <index-name>/_doc/<document-id>

Query DSL

ES๋Š” ์ด๋ฆ„ ๊ทธ๋ž˜๋„ ๊ฒ€์ƒ‰์— ํŠนํ™”๋œ ํ”Œ๋žซํผ์ด๋‹ค. Domain Specific Language, DSL๋ฅผ ํ†ตํ•ด Document๋ฅผ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

DSL์˜ ๋ฌธ๋ฒ•์€ ํฌ๊ฒŒ Filter Context์™€ Query Context๋กœ ๋‚˜๋‰œ๋‹ค. ๋‘˜์˜ ์ฐจ์ด๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • Filter Context
    • ์ •ํ™•ํ•œ ๊ฒ€์ƒ‰์„ ์ œ๊ณตํ•œ๋‹ค.
    • ๋‹จ, ๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ์— ๋Œ€ํ•œ ์œ ์‚ฌ๋„(score)๋Š” ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • Query Context
    • ๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์œ ์‚ฌ๋„ ๋ฐฉ์‹์˜ ๊ฒ€์ƒ‰์„ ์ œ๊ณตํ•œ๋‹ค.
    • ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ๋ถ€์ •ํ™• ํ•  ์ˆ˜ ์žˆ๋‹ค.

Filter Context, Query Context ๋‘˜์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด DSL์„ ๋‚ ๋ฆด ์ˆ˜๋„ ์žˆ๋‹ค!

Query DSL์˜ ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

{
  // ๋ฐ˜ํ™˜ ๊ฒฐ๊ณผ ๊ฐฏ์ˆ˜
  "size": xx

  // ๋ฌธ์„œ์˜ ์‹œ์ž‘ ์‹œ์ . ๊ธฐ๋ณธ๊ฐ’์€ 0
  "from": xx

  // ๊ธฐ๋ณธ๊ฐ’์€ ๋ฌดํ•œ๋Œ€
  "timeout": xx

  // ๊ฒ€์ƒ‰์˜ ์กฐ๊ฑด๋ฌธ: Filter Context, Query Context
  "query": { ... }

  // ๊ฒฐ๊ณผ ์ •๋ ฌ ๋ฐฉ์‹
  "sort": { ... }

  "_source": { ... } // ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์— ๋…ธ์ถœํ•  ํ•„๋“œ
  "aggs": { ... } // ํ†ต๊ณ„/์ง‘๊ณ„ ๋ฐ์ดํ„ฐ
}

์ด๋ฒˆ์—๋Š” Query DSL์˜ ๊ธฐ๋ณธ์ธ Filter Context, Query Context๋งŒ ํ›‘์–ด๋ณด์ž.

POST <index-name>/_search
{
  "query": {
    "bool": { ... } // Filter Context
    "match": { ... } // Query Context
  }
}

Filter Context

POST <index-name>/_search
{
  "query": {
    "bool": {
      "filter": {
        "match": { "name": "foo" }
      }
    }
  }
}

Query Context

POST <index-name>/_search
{
  "query": {
    "match": {
		  "name": "foo"
    }
	}
}

Update

POST <index-name>/_doc/<id>
{
  "id": 11,
  "name": "foo"
}

id๋ฅผ ์ง€์ •ํ•ด์„œ Document๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฑธ ํ•œ๋ฒˆ ๋” ํ•˜๋ฉด, ๊ธฐ์กด Document์˜ ๊ฐ’์ด ๊ฐฑ์‹ ๋œ๋‹ค.

๋‹จ, ์ด๊ฒƒ์€ ๊ธฐ์กด์— ์žˆ๋˜ Document๊ฐ€ ์‚ญ์ œ ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด Document๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ๊ทธ๊ฒƒ์„ ํ•ด๋‹น id์˜ Document์˜ ์ƒˆ๋กœ์šด ๋ฒ„์ „(version)์œผ๋กœ ์˜ฌ๋ฆฌ๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, Overwrite๊ฐ€ ์•„๋‹ˆ๋ผ Replace์ธ ์…ˆ์ด๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฐฉ์‹์€ Document ์ž์ฒด๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. ํ•„๋“œ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ?

POST <index-name>/_doc/<id>/_update
{
  "doc": {
    "id": 11,
    "name": "foo"
  }
}

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด, ๊ธฐ์กด Document์— ํ•ด๋‹น ํ•„๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด, ํ•„๋“œ ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๊ณ , ์—†๋˜ ํ•„๋“œ๋Š” ์ถ”๊ฐ€๋œ๋‹ค. ๋ฌผ๋ก  ์ด ๊ฒฝ์šฐ์—๋„ ๋ฌธ์„œ์˜ version์€ ์˜ฌ๋ผ๊ฐ„๋‹ค.

Delete

์‚ญ์ œ๋Š” ๊ฐ„๋‹จํ•˜๋‹ค.

DELETE <index-name>/_doc/<id>

Bulk API

์—ฌ๋Ÿฌ ๋ช…๋ น์„ ๋ฐฐ์น˜(batch)๋กœ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๊ฐ ๋ช…๋ น์„ ๋”ฐ๋กœ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ๋น ๋ฅด๊ณ , ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ์ ๋‹ค. ๋Œ€์šฉ๋Ÿ‰์˜ indexing, update, delete ์ž‘์—…์ด ํ•„์š”ํ•  ๋•Œ๋Š” Bulk API๋ฅผ ์‚ฌ์šฉํ•˜์ž.

POST _bulk
{"index": {"_index": "bluehorn-test", "_id": "1"}}
{"title": "bulk test", "name": "haha"}

{"update": {"_index": "bluehorn-test", "_id": "2"}}
{"doc": {"title": "bulk test", "name": "haha"}}

{"delete": {"_index": "bluehorn-test", "_id": "3"}}

๋งŒ์•ฝ Bulk ์ž‘์—…ํ•˜๋Š” Index๊ฐ€ ๊ณ ์ •์ด๋ผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ๋‹ค.

POST <index-name>/_doc/_bulk
{"index": {}}
{...}
{"index": {}}
{...}