본 포스팅은 2023년 01월 01일을 기준으로 작성되었습니다.
처음 Elasticsearch가 출시됐을 때, 각 document는 하나의 index와 하나의 mapping type으로 저장되었습니다.
Mapping Type은 document의 타입을 나타내는데 사용되었는데, 예를 들자면 twitter라는 index는 user 타입과 tweet 타입을 가질 수 있습니다.
각각의 mapping type은 고유의 field를 가지고 있습니다. 예를 들어, user 타입은 full_name, user_name 필드를 가지고 있고, tweet 타입은 content, tweeted_at 필드를 가지고 있습니다.
모든 document는 _type 메타 필드를 가지고 있었기 때문에 타입으로 필터링하여 검색을 할 수 있었습니다.
GET twitter/user,tweet/_search
{
"query": {
"match": {
"user_name": "kimchy"
}
}
}
_uid가 _type 필드와 document의 _id가 결합되어 생성되므로, _type만 다르고 동일한 _id를 가진 인덱스가 존재할 수 있습니다.
Elasticsearch는 처음에 index는 SQL database의 '데이터베이스'와 비슷하고, type은 '테이블'과 비슷하다고 유사하였습니다.
이것이 유저들이 잘못된 방향으로 사용하도록 유도하였습니다. SQL database에서 테이블은 서로 독립적입니다. 하지만 elasticsearch에서는 동일한 이름의 index에 다른 _type 내의 동일한 이름의 필드는 lucene field에 속해서, 같은 mapping을 가져가게 됩니다.
즉, 위의 예를 사용하면
- Index: A | type: user | field: user_name
- Index: A | type: tweet | field: user_name
서로 다른 타입의 user_name field 이지만 같은 lucene field에 속하기 때문에, 동일한 매핑(정의)을 가져야 합니다.
위와 같은 문제로 인해, 문서를 압축 등 여러가지 이슈가 있어 Elasticsearch에서 mapping _type의 개념을 제거하기로 결정했습니다.
Index 별 document type정의
인덱스가 서로 완전히 독립적이므로 위에서 언급만 문제가 생기지 않습니다.
2. Custom type field정의
기존 방식:
PUT twitter
{
"mappings": {
"user": {
"properties": {
"name": { "type": "text" },
"user_name": { "type": "keyword" },
"email": { "type": "keyword" }
}
},
"tweet": {
"properties": {
"user_name": { "type": "keyword" },
"tweeted_at": { "type": "date" },
"content": { "type": "text" }
}
}
}
}
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"
}
}
}
변경 후:
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"
}
}
}
}
}