-
MappingBackEnd/Elasticsearch API 2025. 3. 13. 02:00반응형
Mapping은 데이터의 저장 형태와 검색 방식을 정의하는 명세입니다. 관계형 데이터베이스의 스키마와 비슷한 개념으로, 인덱스에 저장될 문서의 구조와 필드 타입을 지정합니다.
Key Components of Mapping
- Index
- Document
- Field
- Data Type
Data Type
- Text: 색인 시 분석되어 토큰화됩니다. 전체 텍스트 검색에 적합합니다.
- Keyword: 분석되지 않고 그대로 저장되기에 정확한 일치 검색, 정렬, 집계에 적합합니다.
- Numeric: 숫자 데이터를 저장하는 데 사용됩니다. e.g long, integer, short, byte, and etc
- Date: 날짜와 시간 정보를 저장하는 데 사용됩니다.
- Boolean: 참/거짓 값을 저장하는 데 사용됩니다.
- Range: 범위 데이터를 저장하는 데 사용됩니다.
- Geo-Data: 지리적 위치 데이터를 저장하는 데 사용됩니다.
- IP: IP 주소를 저장하는데 사용됩니다.
- Completion: 자동 완성 기능을 제공하는 데 사용됩니다.
- Token Count: 문자열의 토큰 개수를 저장하는 데 사용됩니다.
- Percolator: 쿼리를 저장하고 문서가 해당 쿼리에 매칭되는지 여부를 확인하는 데 사용됩니다.
- Binary: 이진 데이터를 저장하는 데 사용됩니다.
# Mapping PUT /member { "mappings": { "properties": { "name": { "type": "text" }, "email": { "type": "keyword" }, "age": { "type": "integer" }, "joined": { "type": "date" }, "active": { "type": "boolean" }, "location": { "type": "geo_point" }, "ip_address": { "type": "ip" }, "profile_picture": { "type": "binary" }, "preferences": { "type": "object", "properties": { "newsletter": { "type": "boolean" }, "notifications": { "type": "keyword" } } } } } } GET /member/_mapping GET /member/_mapping/field/name # Document POST /member/_doc/1 { "name": "John Doe", "email": "john.doe@example.com", "age": 30, "joined": "2023-05-27T00:00:00Z", "active": true, "location": { "lat": 40.7128, "lon": -74.0060 }, "ip_address": "192.168.1.1", "profile_picture": "aGVsbG8gd29ybGQ=", // "hello world" in Base64 "preferences": { "newsletter": true, "notifications": "daily" } } GET /member/_doc/1
Complex Data Type
Object 타입은 단순한 JSON 구조를 저장하고 검색하는 데 적합하며, Nested 타입은 객체 배열 내의 각 요소를 독립적으로 처리해야 할 때 사용됩니다.
- Object (OR 연산, 독립적인 색인 지원하지 않음)
PUT /my_user { "mappings": { "properties": { "user": { "type": "object", "properties": { "name": { "type": "text" }, "email": { "type": "keyword" } } }, "comments": { "type": "object", "properties": { "user": { "type": "text" }, "message": { "type": "text" }, "date": { "type": "date" } } } } } } POST /my_user/_doc/1 { "user": { "name": "John Doe", "email": "john.doe@example.com" }, "comments": [ { "user": "Alice", "message": "Nice article!", "date": "2023-05-27" }, { "user": "Bob", "message": "Thanks for the information.", "date": "2023-05-28" } ] } # object의 경우, OR연산으로 Alice, Bob 모두 결과에 포함됩니다. GET /my_user/_search { "query": { "bool": { "must": [ { "match": { "comments.user": "Alice" } }, { "match": { "comments.message": "Thanks for the information" } } ] } } }
- Nested (AND 연산, 독립적인 색인 지원)
PUT /my_nested_user { "mappings": { "properties": { "user": { "type": "object", "properties": { "name": { "type": "text" }, "email": { "type": "keyword" } } }, "comments": { "type": "nested", "properties": { "user": { "type": "text" }, "message": { "type": "text" }, "date": { "type": "date" } } } } } } POST /my_nested_user/_doc/1 { "user": { "name": "John Doe", "email": "john.doe@example.com" }, "comments": [ { "user": "Alice", "message": "Nice article!", "date": "2023-05-27" }, { "user": "Bob", "message": "Thanks for the information.", "date": "2023-05-28" } ] } # nested의 경우, AND 연산으로 Alice, Bob 모두 결과에 포함되지 않습니다. GET /my_nested_user/_search { "query": { "nested": { "path": "comments", "query": { "bool": { "must": [ { "match": { "comments.user": "Alice" } }, { "match": { "comments.message": "Thanks for the information." } } ] } } } } }
기존 인덱스에 새로운 필드를 추가하려면 PUT /인덱스명/_mapping API를 사용합니다. 기존 필드의 타입이나 설정을 변경할 수는 없습니다.
PUT /member/_mapping { "properties": { "new_field": { "type": "text" } } }
Multi-Fields Mapping
다중 필드(Multi-Fields)는 하나의 필드에 대해 여러 가지 다른 방식으로 데이터를 저장하고 검색할 수 있게 합니다.
PUT /multi_fields_index { "mappings": { "properties": { "title": { "type": "text", "fields": { "raw": { "type": "keyword" } } } } } } # 데이터 색인 POST /multi_fields_index/_doc/1 { "title": "Elasticsearch: The Definitive Guide" } POST /multi_fields_index/_doc/2 { "title": "Elasticsearch: Search Everything" }
- title: text 타입으로 전체 텍스트 검색
- title.raw: keyword 타입으로 정확한 일치 검색
# 전체 텍스트 검색 GET /multi_fields_index/_search { "query": { "match": { "title": "Elasticsearch" } } } # 정확한 일치 검색 GET /multi_fields_index/_search { "query": { "term": { "title.raw": "Elasticsearch: The Definitive Guide" } } }
Dynamic Mapping
동적 매핑(Dynamic Mapping)은 문서를 색인할 때, 미리 정의된 매핑이 없더라도 자동으로 필드와 데이터 타입을 생성하는 기능입니다. 즉, 사용자가 매핑을 미리 설정하지 않아도 문서의 구조에 따라 Elasticsearch가 자동으로 필드를 생성합니다.
[참고자료]
반응형'BackEnd > Elasticsearch API' 카테고리의 다른 글
Alias & Reindex (0) 2025.03.15 Mapping parameters (0) 2025.03.14 Analysis (0) 2025.03.11 Multiple Documents (0) 2025.03.10 Document (0) 2025.03.09