elasticsearch field add(1)

2022, Mar 19    
elasticsearch

[๊ณตํ†ต]

๊ธฐ์กด ๋ฐ์ดํ„ฐ ๋ฐฑ์—…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์Šค๋ƒ…์ƒท ๋ฐฑ์—… ๋ฐฉ์‹์€ โ€œ์—ฌ๊ธฐโ€ ์ฐธ๊ณ ํ•˜์„ธ์š”.

[๊ธฐ์กด ๋ฐ์ดํ„ฐ์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹]

1. ํ˜„์žฌ ์ ์šฉ๋œ ๋งตํ•‘ ์ •๋ณด๋ฅผ ์กฐํšŒํ•œ๋‹ค.

GET attic_ticket/_mapping

2. ์œ„์—์„œ ์กฐํšŒํ•œ ๋งตํ•‘ ์ •๋ณด๋ฅผ ์ฐธ๊ณ ํ•ด์„œ, depth์— ๋งž๊ฒŒ ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

PUT attic_ticket/_mappings
{
    "properties": {
      "customer" : {
        "type":"nested",
        "properties": {
          "messengerId": {    // customer์— messengerId ์ปฌ๋Ÿผ ์ถ”๊ฐ€
            "type": "text"
          }
        }
      }
  }
}

../img/els2-1.png

3. ์ž˜ ์ถ”๊ฐ€๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค

GET attic_ticket/_mapping

4. ๊ธฐ์กด ๋ฐ์ดํ„ฐ์— ์ถ”๊ฐ€ํ•œ ํ•„๋“œ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.( multiple update )

POST attic_ticket/_update_by_query
{
  "script": "ctx._source.customer.messengerId = ''"
}

** (ํ•„๋“œ๊ฐ€ ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ) **

5-1. ๊ธฐ์กด ๋ฐฐ์—ด ์‚ญ์ œ

POST attic_message/_update_by_query
{
  "script": {
    "source": "ctx._source.remove('metaarray')"
  },
  "query":{
    "bool": {
      "should": [
        {
          "terms": {
            "channelUrl": [
              "92095428-d647-466c-a054-e1e984dd48b1"
            ]
          }
        }
      ]
    }
  }
}

5-2. ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”

POST attic_message/_update_by_query
{
  "script": "ctx._source.metaarray = []",
  "query":{
    "bool": {
      "should": [
        {
          "terms": {
            "channelUrl": [
              "92095428-d647-466c-a054-e1e984dd48b1"
            ]
          }
        }
      ]
    }
  }
}

5-3. ๋ฐฐ์—ด์— ๊ฐ’ ์ถ”๊ฐ€

POST attic_message/_update_by_query
{
  "script": {
    "source":"ctx._source.metaarray.add(params)",
    "params": {
      "key": "templateMessage",
      "value":[
                "{~~~~~~~~~~}"
              ]
    }
  },
  "query":{
    "bool": {
      "should": [
        {
          "terms": {
            "channelUrl": [
              "92095428-d647-466c-a054-e1e984dd48b1"
            ]
          }
        }
      ]
    }
  }
}

5. ๊ฒ€์ฆ

  1. ์ผ๋ถ€ ํ‹ฐ์ผ“์—๋งŒ ์ถ”๊ฐ€ํ•œ ํ•„๋“œ์— ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•ด๋ณธ๋‹ค.
POST attic_ticket/_update_by_query
{
  "script": {
    "source":"ctx._source.customer.messengerId = 'honggildong'"
  },
  "query":{     // ์ผ๋ถ€ ticket์—๋งŒ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•˜๊ณ 
    "term":{
      "ticketId":"63af9a77-845b-4ca5-b692-0d3e5cc7d398"
    }
  }
}
  1. ์ถ”๊ฐ€ํ•œ ๊ฐ’์œผ๋กœ ๊ฒ€์ƒ‰ํ•ด๋ณธ๋‹ค.
GET attic_ticket/_search
{
    "query" : {
      "bool": {
        "should":[{
          "terms":{
            "ticketId":["63af9a77-845b-4ca5-b692-0d3e5cc7d398"],"boost":1.0
          }
        }]
      }
    }
}

์ฐธ๊ณ ) https://esbook.kimjmin.net/04-data/4.3-_bulk