Update Mapping

이상민·2021년 5월 10일
0
post-thumbnail

field mappings cannot be changed
--> except for some parameters like ignore_above, etc.

  • mapping is immutable as data could already been indexed

  • changing type requires whole data structure to be rebuild

  • docs need to be reindexed for type change

1. Reindex API

retrieves docs from source index and indexes it to the destination index

1-1. reindexing

PUT /reviews_new
{
    "mappings" : {
        "properties" : {
            "author" : {
                "properties" : {
                    "email" : {
                        "type" : "keyword",
                        "ignore_above" : 256
                    },
                    "first_name" : {
                        "type" : "text"
                    },
                    "last_name" : {
                        "type" : "text"
                    }
                }
            },
            "content" : {
                "type" : "text"
            },
            "created_at" : {
                "type" : "date"
            },
            "product_id" : {
                "type" : "keyword"  // changed from "integer"
            },
            "rating" : {
                "type" : "float"
            }
        }
    }
}
POST /_reindex
{
    "source" : {
        "index" : "reviews"
    },
    "dest" : {
        "index" : reviews_new
    }
}
  • data type doesn't reflect how the values are indexed
    • it is used to represent _source data
    • data types can also be handled at the application level

1-2. reindexing & updating _source

scripts can be supplied on reindex

POST /_reindex
{
    "source" : {
        "index" : "reviews"
    },
    "dest" : {
        "index" : reviews_new
    },
    "script" : {
        "source" : """
            if (ctx._source.product_id != null) {
                ctx._source.product_id = ctx._source.product_id.toString();
            }
           """
    }
}
  • script above changes product_id _source data from int to string

1-3. reindex with condition

  • removing fields
    • field mappings cannot be deleted
    • omitting fields when indexing docs is possible
    • to reclaim disk space used by a field, use source filtering

1-4. reindex only selected fields

  • specify array of field names to be reindexed

1-5. change field name


2. Field Alias

  • For the size of docs, reindexing may be inefficient
  • field aliases doesn't required reindex
    ex) adding alias comment for field content
PUT /reviews/_mapping
{
    "properties" : {
        "comment" : {
            "type" : "alias",
            "path" : "content"
        }
    }
}
  • only the target field of field aliases can be updated
  • similar to field alias, index can also have alias
profile
편하게 읽기 좋은 단위의 포스트를 추구하는 개발자입니다

0개의 댓글