Index trong Elasticsearch - Mapping

Index trong Elasticsearch gồm có 3 thành phần chính là: aliases, mappings, settings. Ở bài này sẽ tìm hiểu về aliases của Index.

Giới thiệu

Mapping is the process of defining how a document, and the fields it contains, are stored and indexed.

Mapping trong Index chứa các thông tin define kiểu dữ liệu các field trong document, các thao tác với các field của document khi được lưu trữ.

Mapping có 3 phần chính:

Runtime fields

A runtime field is a field that is evaluated at query time

Runtime field là một field được thực thi trong khi query. Runtime field cho phép:

  • Thêm field vào documents mà không phải reindexing lại data(cũng có thể index runtime field nếu muốn). Ví dụ: document có một field createdAt có kiểu là timestamp, khi query có thể trả thêm một field có giá trị là ngày của field createdAt.
  • Ghi đè giá trị của field trong khi query. Ví dụ: khi tạo một runtime field trùng tên với một field của document thì runtime field này sẽ chỉ lấy giá trị của field gốc và tinh toán chứ không thay đổi giá trị của field gốc.

Thực hành thử, đầu tiên tạo 1 một runtime field day_of_week lấy giá trị ngày từ field createdAt của document.

PUT event/ { "mappings": { "runtime": { "day_of_week": { "type": "keyword", "script": { "source": "emit(doc['createdAt'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))" } } }, "properties": { "createdAt": {"type": "date"} } } }

Thêm 1 document vào index

POST event/_bulk {"create": {}} {"createdAt": 1680145918}

Get thử document của index

GET event/_search

Document trả về chỉ có field createdAt. Muốn lấy thêm giá trị của runtime field day_of_week cần define trong field trong query.

GET event/_search { "fields": [ "day_of_week" ] }

Thử search query với runtime field.

GET event/_search { "query": { "match": { "day_of_week": "Tuesday" } }, "fields": [ "day_of_week" ] }

Explicit mapping

Explicit mapping cho phép define cụ thể kiểu của từng field.

PUT /admin { "mappings": { "properties": { "age": { "type": "integer" }, "email": { "type": "keyword" }, "name": { "type": "text" } } } }

API trên định nghĩa Index admin có 3 field với kiểu dữ liệu lần lượt là: age-integer, email-keyword, name-text. Các type của field mà Elasticsearch hỗ trợ có thể xem ở đây.

Dynamic mapping

Khi tạo Index không nhất thiết phải define type của các field luôn. Elasticsearch sẽ tự define type các field theo type mapping sau:

Có thể tạo các Dynamic templates để tạo các custom mapping khác ngoài các mapping default mà Elasticsearch cung cấp.

Thực hành thử: tạo một dynamic template map type long sang type date.

Đầu tiên thử thêm document khi chưa thêm dynamic template:

POST event/_bulk {"create": {}} {"createdAt": 1680151187}

Get mapping của index, field createdAt đang có type là long:

GET event/_mapping

Bây giờ dùng API sau để thêm dynamic template, lúc này các field đầu vào là long sẽ được map sang date

PUT event { "mappings": { "dynamic_templates": [ { "long_as_date": { "match_mapping_type": "long", "mapping": { "type": "date" } } } ] } }

Ref