Pyohwan / english-study

0 stars 0 forks source link

[Elasticsearch 7.x] Removal of mapping types #30

Open Pyohwan opened 4 years ago

Pyohwan commented 4 years ago

https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html#_alternatives_to_mapping_types

Removal of mapping types

What are mapping types?

PUT twitter/user/kimchy { "name": "Shay Banon", "user_name": "kimchy", "email": "shay@kimchy.com" }

PUT twitter/tweet/1 { "user_name": "kimchy", "tweeted_at": "2017-10-24T09:00:00Z", "content": "Types are going away" }

GET twitter/tweet/_search { "query": { "match": { "user_name": "kimchy" } } }

* 다음과 같이 커스텀 `type` 필드를 추가하여 동일한 결과를 얻을 수 있다.
```json
PUT twitter
{
  "mappings": {
    "_doc": {
      "properties": {
        "type": { "type": "keyword" }, 
        "name": { "type": "text" },
        "user_name": { "type": "keyword" },
        "email": { "type": "keyword" },
        "content": { "type": "text" },
        "tweeted_at": { "type": "date" }
      }
    }
  }
}

PUT twitter/_doc/user-kimchy
{
  "type": "user", 
  "name": "Shay Banon",
  "user_name": "kimchy",
  "email": "shay@kimchy.com"
}

PUT twitter/_doc/tweet-1
{
  "type": "tweet", 
  "user_name": "kimchy",
  "tweeted_at": "2017-10-24T09:00:00Z",
  "content": "Types are going away"
}

GET twitter/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "user_name": "kimchy"
        }
      },
      "filter": {
        "match": {
          "type": "tweet" 
        }
      }
    }
  }
}

명시적인 type 필드가 암시적인 type 필드를 대신한다.

Parent/Child without mapping types

Schedule for removal of mapping types

Elasticsearch 7.x

Elasticsearch 8.x

Pyohwan commented 4 years ago

https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html#_migrating_multi_type_indices_to_single_type

Migrating multi-type indices to single-type

Index per document type

PUT tweets { "settings": { "index.mapping.single_type": true }, "mappings": { "_doc": { "properties": { "content": { "type": "text" }, "user_name": { "type": "keyword" }, "tweeted_at": { "type": "date" } } } } }

POST _reindex { "source": { "index": "twitter", "type": "user" }, "dest": { "index": "users", "type": "_doc" } }

POST _reindex { "source": { "index": "twitter", "type": "tweet" }, "dest": { "index": "tweets", "type": "_doc" } }

### Custom type field
* 다음 예는 커스텀 `type` 필드를 추가하고 original `_type` 의 값을 입력한다.
* 또한 충돌하는 IDs 를 가진 다른 타입의 문서가 있다면, `_id` 타입에 추가한다.

```json
PUT new_twitter
{
  "mappings": {
    "_doc": {
      "properties": {
        "type": {
          "type": "keyword"
        },
        "name": {
          "type": "text"
        },
        "user_name": {
          "type": "keyword"
        },
        "email": {
          "type": "keyword"
        },
        "content": {
          "type": "text"
        },
        "tweeted_at": {
          "type": "date"
        }
      }
    }
  }
}

POST _reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter"
  },
  "script": {
    "source": """
      ctx._source.type = ctx._type;
      ctx._id = ctx._type + '-' + ctx._id;
      ctx._type = '_doc';
    """
  }
}

Typeless APIs in 7.0

Document APIs

PUT index/_doc/1
{
  "foo": "baz"
}
{
  "_index": "index",
  "_id": "1",
  "_type": "_doc",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

GET /index/_source/1

* 요청 본문에 type 이 더이상 나타나지 않아야 한다. 다음의 bulk indexing 예제에서는, URL 과 개별 bulk 명령어에서 type 이 생략된다.
```json
POST _bulk
{ "index" : { "_index" : "index", "_id" : "3" } }
{ "foo" : "baz" }
{ "index" : { "_index" : "index", "_id" : "4" } }
{ "foo" : "qux" }

Search APIs

Types in responses

GET index/_doc/1

{ "_index" : "index", "_type" : "_doc", "_id" : "1", "_version" : 1, "_seq_no" : 0, "_primary_term" : 1, "found": true, "_source" : { "foo" : "baz" } }


### Index templates
* `include_type_name` 을 `false` 로 설정하여, typeless index 템플릿을 re-adding 으로 만드는것을 추천한다.
  * hood 에서 typeless 템플릿은 index 를 만들때 더미 type `_doc` 를 사용한다.
* typeless 템플릿이 typed index 생성 호출과 함께 사용되거나, typed 템플릿이 typeless index 생성 호출과 함께 사용되면, 템플릿은 계속 적용되지만, idex 생성 호출은 type 이 있는지 없는지 결정해야 한다.
  * 아래 예를 보면, `index-1-01` 는 typeless 템플릿과 일치하지만 type이 있고, `index-2-01` 는 type 정의 템플릿과 일치하지만 typeless 이다.
  * `index-1-01`, `index-2-01` 둘다 일치 하는 템플릿에서 `foo` 필드를 상속한다.
```json
PUT _template/template1
{
  "index_patterns":[ "index-1-*" ],
  "mappings": {
    "properties": {
      "foo": {
        "type": "keyword"
      }
    }
  }
}

PUT _template/template2?include_type_name=true
{
  "index_patterns":[ "index-2-*" ],
  "mappings": {
    "type": {
      "properties": {
        "foo": {
          "type": "keyword"
        }
      }
    }
  }
}

PUT index-1-01?include_type_name=true
{
  "mappings": {
    "type": {
      "properties": {
        "bar": {
          "type": "long"
        }
      }
    }
  }
}

PUT index-2-01
{
  "mappings": {
    "properties": {
      "bar": {
        "type": "long"
      }
    }
  }
}

Mixed-version clusters