2021年1月22日 星期五

[elasticsearch] 新增加的資料 動態增加欄位 但是不索引 Dynamic templates for mapping

[elasticsearch] 新增加的資料 動態增加欄位 但是不索引 Dynamic templates for mapping

elasticsearch 支援動態的欄位是種類 schema on write的search engine,
當你索引新的資料的時候,如果有新的欄位 mapping也會更新。

如果是 string type
就會預設成
{
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"ignore_above": 256
}
}
}

如果在嚴格的使用情境中,讓因爲新增加的資料,所添加的欄位索引呢? 怎麼改變這個設定呢?
這樣可以節省索引的時間跟空間。

有兩種方式

1. dynamic | Elasticsearch Reference [7.10] | Elastic https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic.html
The dynamic setting controls whether new fields can be added dynamically or not. It accepts three settings:

- true Newly detected fields are added to the mapping. (default)
- false Newly detected fields are ignored. These fields will not be indexed so will not be searchable but will still appear in the _source field of returned hits. These fields will not be added to the mapping, new fields must be added explicitly.
- strict If new fields are detected, an exception is thrown and the document is rejected. New fields must be explicitly added to the mapping.

如果使用 "dynamic": false 就會讓不在mapping的欄位只存在_source而不被索引。

2. 但是如果只想對string 這種可能比較長的欄位處理的話可以使用 Dynamic templates
Dynamic templates | Elasticsearch Reference [7.10] | Elastic https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html


PUT my-index-000001
{
"mappings": {
"dynamic_templates": [
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword",
"ignore_above": 8191,
"index": false
}
}
}
}
]
}
}


- ignore_above | Elasticsearch Reference [7.10] | Elastic https://www.elastic.co/guide/en/elasticsearch/reference/current/ignore-above.html
"The value for ignore_above is the character count, but Lucene counts bytes. If you use UTF-8 text with many non-ASCII characters, you may want to set the limit to 32766 / 4 = 8191 since UTF-8 characters may occupy at most 4 bytes."
- index | Elasticsearch Reference [7.10] | Elastic https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-index.html
"The index option controls whether field values are indexed. It accepts true or false and defaults to true. Fields that are not indexed are not queryable."



沒有留言:

張貼留言