什么是數(shù)據(jù)建模锰茉?
-
數(shù)據(jù)建模(Data modeling)州丹, 是創(chuàng)建數(shù)據(jù)模型的過程
-
數(shù)據(jù)模型是對(duì)真實(shí)世界進(jìn)?抽象描述的?種?具和?法低滩,實(shí)現(xiàn)對(duì)現(xiàn)實(shí)世界的映射
- 博客 / 作者 / ?戶評(píng)論
-
三個(gè)過程:概念模型 => 邏輯模型 => 數(shù)據(jù)模型(第三范式)
- 數(shù)據(jù)模型:結(jié)合具體的數(shù)據(jù)庫缤言,在滿?業(yè)務(wù)讀寫性能等需求的前提下采驻,確定最終的定義
-
數(shù)據(jù)建模:功能需求 + 性能需求
如何對(duì)字段進(jìn)?建模
字段類型:Text v.s Keyword
-
Text
?于全?本字段审胚,?本會(huì)被 Analyzer 分詞
默認(rèn)不?持聚合分析及排序。需要設(shè)置 fielddata 為 true
-
Keyword
?于id礼旅,枚舉及不需要分詞的?本膳叨。例如電話號(hào)碼,email地址痘系,?機(jī)號(hào)碼菲嘴,郵政編碼,性別等
適?于 Filter(精確匹配)碎浇,Sorting 和 Aggregations
-
設(shè)置多字段類型
默認(rèn)會(huì)為?本類型設(shè)置成 text临谱,并且設(shè)置?個(gè) keyword 的?字段
在處理?類語?時(shí),通過增加“英?”奴璃,“拼?”和“標(biāo)準(zhǔn)”分詞器悉默,提?搜索結(jié)構(gòu)
字段類型 :結(jié)構(gòu)化數(shù)據(jù)
-
數(shù)值類型
- 盡量選擇貼近的類型。例如可以? byte苟穆,就不要? long
-
枚舉類型
- 設(shè)置為 keyword抄课。即便是數(shù)字,也應(yīng)該設(shè)置成 keyword雳旅,獲取更加好的性能
-
其他
- ?期 / 布爾 / 地理信息
檢索
-
如不需要檢索跟磨,排序和聚合分析
- Enable 設(shè)置成 false
-
如不需要檢索
- Index 設(shè)置成 false
-
對(duì)需要檢索的字段,可以通過如下配置攒盈,設(shè)定存儲(chǔ)粒度
- Index_options / Norms :不需要?dú)w?化數(shù)據(jù)時(shí)抵拘,可以關(guān)閉
聚合及排序
-
如不需要檢索,排序和聚合分析
- Enable 設(shè)置成 false
-
如不需要排序或者聚合分析功能
- Doc_values / fielddata 設(shè)置成 false
-
更新頻繁型豁,聚合查詢頻繁的 keyword 類型的字段
- 推薦將 eager_global_ordinals 設(shè)置為 true
額外的存儲(chǔ)
-
是否需要專?存儲(chǔ)當(dāng)前字段數(shù)據(jù)
Store 設(shè)置成 true僵蛛,可以存儲(chǔ)該字段的原始內(nèi)容
?般結(jié)合 _source 的 enabled 為 false 時(shí)候使?
-
Disable _source:節(jié)約磁盤尚蝌;適?于指標(biāo)型數(shù)據(jù)
?般建議先考慮增加壓縮?
?法看到 _source字段,?法做 ReIndex充尉,?法做 Update
Kibana 中?法做 discovery
?個(gè)數(shù)據(jù)建模的實(shí)例
-
圖書的索引
書名
簡(jiǎn)介
作者
發(fā)??期
圖書封?
優(yōu)化字段設(shè)定
-
圖書的索引
書名:?持全?和精確匹配
簡(jiǎn)介:?持全?
作者:精確值
發(fā)??期:?期類型
圖書封?:精確值
需求變更
新需求:增加圖書內(nèi)容的字段飘言。并要求能被搜索同時(shí) ?持?亮顯示
-
新需求會(huì)導(dǎo)致 _source 的內(nèi)容過?
- Source Filtering 只是傳輸給客戶端時(shí)進(jìn)?過濾, Fetch 數(shù)據(jù)時(shí)驼侠,ES 節(jié)點(diǎn)還是會(huì)傳輸 _source 中的數(shù)據(jù)
-
解決?法
關(guān)閉 _source
然后將每個(gè)字段的 “store” 設(shè)置成 true
#新增 Content字段姿鸿。數(shù)據(jù)量很大。選擇將Source 關(guān)閉
PUT books
{
"mappings" : {
"_source": {"enabled": false},
"properties" : {
"author" : {"type" : "keyword","store": true},
"cover_url" : {"type" : "keyword","index": false,"store": true},
"description" : {"type" : "text","store": true},
"content" : {"type" : "text","store": true},
"public_date" : {"type" : "date","store": true},
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 100
}
},
"store": true
}
}
}
}
查詢圖書:解決字段過?引發(fā)的性能問題
返回結(jié)果不包含 _source 字段
對(duì)于需要顯示的信息倒源,可以在在查詢中指定 “stored_fields"
禁? _source 字段后苛预,還是?持使? highlights API,?亮顯示 content 中匹配的相關(guān)信息
#搜索笋熬,通過store 字段顯示數(shù)據(jù)碟渺,同時(shí)高亮顯示 conent的內(nèi)容
POST books/_search
{
"stored_fields": ["title","author","public_date"],
"query": {
"match": {
"content": "searching"
}
},
"highlight": {
"fields": {
"content":{}
}
}
}
Mapping 字段的相關(guān)設(shè)置
-
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html
Enabled – 設(shè)置成 false,僅做存儲(chǔ)突诬,不?持搜索和聚合分析 (數(shù)據(jù)保存在 _source 中)
Index – 是否構(gòu)倒排索引。設(shè)置成 false芜繁,?法被搜索旺隙,但還是?持 aggregation,并出現(xiàn)在 _source 中
Norms – 如果字段?來過濾和聚合分析骏令,可以關(guān)閉蔬捷,節(jié)約存儲(chǔ)
Doc_values – 是否啟? doc_values,?于排序和聚合分析
Field_data – 如果要對(duì) text 類型啟?排序和聚合分析榔袋, fielddata 需要設(shè)置成true
Store – 默認(rèn)不存儲(chǔ)周拐,數(shù)據(jù)默認(rèn)存儲(chǔ)在 _source。
Coerce – 默認(rèn)開啟凰兑,是否開啟數(shù)據(jù)類型的?動(dòng)轉(zhuǎn)換(例如妥粟,字符串轉(zhuǎn)數(shù)字)
Multifields 多字段特性
Dynamic – true / false / strict 控制 Mapping 的?動(dòng)更新
?些相關(guān)的 API
Index Template & Dynamic Template
根據(jù)索引的名字匹配不同的 Mappings 和 Settings
可以在?個(gè) Mapping 上動(dòng)態(tài)的設(shè)定字段類型
Index Alias
?需停機(jī),?需修改程序吏够,即可進(jìn)?修改
Update By Query & Reindex
本章知識(shí)點(diǎn)
-
數(shù)據(jù)建模對(duì)功能與性能?關(guān)重要
Mapping. & Setting
字段 Mapping 參數(shù)的?些回顧勾给,分?的設(shè)定,會(huì)在后續(xù)講解
-
通過具體的實(shí)例锅知,學(xué)習(xí)了數(shù)據(jù)建模時(shí)需要考慮的點(diǎn)
確定字段類型
是否需要搜索和聚合以及排序
是否需要禁? _source 以及打開 store
課程demo
###### Data Modeling Example
# Index 一本書的信息
PUT books/_doc/1
{
"title":"Mastering ElasticSearch 5.0",
"description":"Master the searching, indexing, and aggregation features in ElasticSearch Improve users’ search experience with Elasticsearch’s functionalities and develop your own Elasticsearch plugins",
"author":"Bharvi Dixit",
"public_date":"2017",
"cover_url":"https://images-na.ssl-images-amazon.com/images/I/51OeaMFxcML.jpg"
}
#查詢自動(dòng)創(chuàng)建的Mapping
GET books/_mapping
DELETE books
#優(yōu)化字段類型
PUT books
{
"mappings" : {
"properties" : {
"author" : {"type" : "keyword"},
"cover_url" : {"type" : "keyword","index": false},
"description" : {"type" : "text"},
"public_date" : {"type" : "date"},
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 100
}
}
}
}
}
}
#Cover URL index 設(shè)置成false播急,無法對(duì)該字段進(jìn)行搜索
POST books/_search
{
"query": {
"term": {
"cover_url": {
"value": "https://images-na.ssl-images-amazon.com/images/I/51OeaMFxcML.jpg"
}
}
}
}
#Cover URL index 設(shè)置成false,依然支持聚合分析
POST books/_search
{
"aggs": {
"cover": {
"terms": {
"field": "cover_url",
"size": 10
}
}
}
}
DELETE books
#新增 Content字段售睹。數(shù)據(jù)量很大桩警。選擇將Source 關(guān)閉
PUT books
{
"mappings" : {
"_source": {"enabled": false},
"properties" : {
"author" : {"type" : "keyword","store": true},
"cover_url" : {"type" : "keyword","index": false,"store": true},
"description" : {"type" : "text","store": true},
"content" : {"type" : "text","store": true},
"public_date" : {"type" : "date","store": true},
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 100
}
},
"store": true
}
}
}
}
# Index 一本書的信息,包含Content
PUT books/_doc/1
{
"title":"Mastering ElasticSearch 5.0",
"description":"Master the searching, indexing, and aggregation features in ElasticSearch Improve users’ search experience with Elasticsearch’s functionalities and develop your own Elasticsearch plugins",
"content":"The content of the book......Indexing data, aggregation, searching. something else. something in the way............",
"author":"Bharvi Dixit",
"public_date":"2017",
"cover_url":"https://images-na.ssl-images-amazon.com/images/I/51OeaMFxcML.jpg"
}
#查詢結(jié)果中,Source不包含數(shù)據(jù)
POST books/_search
{}
#搜索昌妹,通過store 字段顯示數(shù)據(jù)捶枢,同時(shí)高亮顯示 conent的內(nèi)容
POST books/_search
{
"stored_fields": ["title","author","public_date"],
"query": {
"match": {
"content": "searching"
}
},
"highlight": {
"fields": {
"content":{}
}
}
}