• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Elasticsearch-27.数据建模he数据建模最佳实践

武飞扬头像
飘然渡沧海
帮助1

Elasticsearch

数据建模实例

什么是数据建模?

  • 数据建模(Data modeling), 是创建数据模型的过程
    • 数据模型是对真实世界进⾏抽象描述的⼀种⼯具和⽅法,实现对现实世界的映射
      • 博客 / 作者 / ⽤户评论
    • 三个过程:概念模型 => 逻辑模型 => 数据模型(第三范式)
      • 数据模型:结合具体的数据库,在满⾜业务读写性能等需求的前提下,确定最终的定义

数据建模:功能需求 性能需求

学新通

如何对字段进⾏建模

学新通

字段类型:Text v.s Keyword

  • Text
    • ⽤于全⽂本字段,⽂本会被 Analyzer 分词
    • 默认不⽀持聚合分析及排序。需要设置 fielddata 为 true
  • Keyword
    • ⽤于 id,枚举及不需要分词的⽂本。例如电话号码,email地址,⼿机号码,邮政编码,性别等
    • 适⽤于 Filter(精确匹配),Sorting 和 Aggregations
  • 设置多字段类型
    • 默认会为⽂本类型设置成 text,并且设置⼀个 keyword 的⼦字段
    • 在处理⼈类语⾔时,通过增加“英⽂”,“拼⾳”和“标准”分词器,提⾼搜索结构

字段类型 :结构化数据

  • 数值类型
    • 尽量选择贴近的类型。例如可以⽤ byte,就不要⽤ long
  • 枚举类型
    • 设置为 keyword。即便是数字,也应该设置成 keyword,获取更加好的性能
  • 其他
    • ⽇期 / 布尔 / 地理信息

检索

  • 如不需要检索,排序和聚合分析
    • Enable 设置成 false
  • 如不需要检索
    • Index 设置成 false
  • 对需要检索的字段,可以通过如下配置,设定存储粒度
    • Index_options / Norms :不需要归⼀化数据时,可以关闭

聚合及排序

  • 如不需要检索,排序和聚合分析
    • Enable 设置成 false
  • 如不需要排序或者聚合分析功能
    • Doc_values / fielddata 设置成 false
  • 更新频繁,聚合查询频繁的 keyword 类型的字段
    • 推荐将 eager_global_ordinals 设置为 true

额外的存储

学新通

⼀个数据建模的实例

学新通

优化字段设定

学新通

需求变更

学新通

查询图书:解决字段过⼤引发的性能问题

学新通

Mapping 字段的相关设置

  • https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html
    • Enabled – 设置成 false,仅做存储,不⽀持搜索和聚合分析 (数据保存在 _source 中)
    • Index – 是否构倒排索引。设置成 false,⽆法被搜索,但还是⽀持 aggregation,并出现在 _source
    • Norms – 如果字段⽤来过滤和聚合分析,可以关闭,节约存储
    • Doc_values – 是否启⽤ doc_values,⽤于排序和聚合分析
    • Field_data – 如果要对 text 类型启⽤排序和聚合分析, fielddata 需要设置成true
    • Store – 默认不存储,数据默认存储在 _source。
    • Coerce – 默认开启,是否开启数据类型的⾃动转换(例如,字符串转数字)
    • Multifields 多字段特性
    • Dynamic – true / false / strict 控制 Mapping 的⾃动更新

⼀些相关的 API

  • Index Template & Dynamic Template
    - 根据索引的名字匹配不同的 Mappings 和 Settings
    - 可以在⼀个 Mapping 上动态的设定字段类型
  • Index Alias
    - ⽆需停机,⽆需修改程序,即可进⾏修改
  • Update By Query & Reindex

本章知识点

  • 数据建模对功能与性能⾄关重要
    • Mapping. & Setting
    • 字段 Mapping 参数的⼀些回顾,分⽚的设定,会在后续讲解
  • 通过具体的实例,学习了数据建模时需要考虑的点
    • 确定字段类型
    • 是否需要搜索和聚合以及排序
    • 是否需要禁⽌ _source 以及打开 store

modeAPI

###### 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"
}



#查询自动创建的Mapping
GET books/_mapping

DELETE books

#优化字段类型
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 设置成false,无法对该字段进行搜索
POST books/_search
{
  "query": {
    "term": {
      "cover_url": {
        "value": "https://images-na.ssl-images-amazon.com/images/I/51OeaMFxcML.jpg"
      }
    }
  }
}

#Cover URL index 设置成false,依然支持聚合分析
POST books/_search
{
  "aggs": {
    "cover": {
      "terms": {
        "field": "cover_url",
        "size": 10
      }
    }
  }
}


DELETE books
#新增 Content字段。数据量很大。选择将Source 关闭
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"
}

#查询结果中,Source不包含数据
POST books/_search
{}

#搜索,通过store 字段显示数据,同时高亮显示 conent的内容
POST books/_search
{
  "stored_fields": ["title","author","public_date"],
  "query": {
    "match": {
      "content": "searching"
    }
  },

  "highlight": {
    "fields": {
      "content":{}
    }
  }

}

学新通

数据建模最佳实践

建模建议(⼀):如何处理关联关系

学新通

Kibana……

  • Kibana ⽬前暂不⽀持 nested 类型和 parent/child 类型 ,在未来有可能会⽀持
  • 如果需要使⽤ Kibana 进⾏数据分析,在数据建模时仍需对嵌套和⽗⼦关联类型作出取舍

建模建议(⼆): 避免过多字段

  • ⼀个⽂档中,最好避免⼤量的字段

    • 过多的字段数不容易维护
    • Mapping 信息保存在 Cluster State 中,数据量过⼤,对集群性能会有影响 (Cluster State 信息需要和所有的节点同步)
    • 删除或者修改数据需要 reindex
  • 默认最⼤字段数是 1000,可以设置 index.mapping.total_fields.limt 限定最⼤字段数。

  • 什么原因会导致⽂档中有成百上千的字段?

Dynamic v.s Strict

  • Dynamic(⽣产环境中,尽量不要打开 Dynamic)
    • true - 未知字段会被⾃动加⼊
    • false - 新字段不会被索引。但是会保存在 _source
    • strict - 新增字段不会被索引,⽂档写⼊失败
  • Strict
    • 可以控制到字段级别

⼀个例⼦:Cookie Service 的数据

学新通

解决⽅案:Nested Object & Key Value

学新通

写⼊ & 查询

学新通

通过 Nested 对象保存 Key/Value 的⼀些不⾜

  • 可以减少字段数量,解决 Cluster State 中保存过多 Meta 信息的问题,但是
    • 导致查询语句复杂度增加
    • Nested 对象,不利于在 Kibana 中实现可视化分析

建模建议(三):避免正则查询

  • 问题:
    • 正则,通配符查询,前缀查询属于 Term 查询,但是性能不够好
    • 特别是将通配符放在开头,会导致性能的灾难
  • 案例:
    • ⽂档中某个字段包含了 Elasticsearch 的版本信息,例如 version: “7.1.0”
    • 搜索所有是 bug fix 的版本?每个主要版本号所关联的⽂档?
解决⽅案:将字符串转换为对象

学新通

搜索过滤

学新通

建模建议(四):避免空值引起的聚合不准

学新通

使⽤ Null_Value 解决空值的问题

学新通

建模建议(五):为索引的 Mapping 加⼊ Meta 信息

学新通

本章知识点

  • 数据建模对于功能与性能⾄关重要。 Mapping ⽂件需要考虑加⼊版本管理
  • 通过 2 个例⼦,了解如何通过建模,提⾼系统的性能
    • 使⽤ Inner Object 避免了低性能的正则匹配
    • 使⽤ Nested Object 和将 Dynamic Mapping 设为 Strict 避免字段数量过多带来的问题
  • 通过 1 个例⼦,了解如何通过建模,提⾼聚合结果的准确度
    • 设置 Null Value

API


###### Cookie Service

##索引数据,dynamic mapping 会不断加入新增字段
PUT cookie_service/_doc/1
{
 "url":"www.谷歌.com",
 "cookies":{
   "username":"tom",
   "age":32
 }
}

PUT cookie_service/_doc/2
{
 "url":"www.amazon.com",
 "cookies":{
   "login":"2019-01-01",
   "email":"xyz@abc.com"
 }
}


DELETE cookie_service
#使用 Nested 对象,增加key/value
PUT cookie_service
{
  "mappings": {
    "properties": {
      "cookies": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "keyword"
          },
          "dateValue": {
            "type": "date"
          },
          "keywordValue": {
            "type": "keyword"
          },
          "IntValue": {
            "type": "integer"
          }
        }
      },
      "url": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}


##写入数据,使用key和合适类型的value字段
PUT cookie_service/_doc/1
{
 "url":"www.谷歌.com",
 "cookies":[
    {
      "name":"username",
      "keywordValue":"tom"
    },
    {
       "name":"age",
      "intValue":32

    }

   ]
 }


PUT cookie_service/_doc/2
{
 "url":"www.amazon.com",
 "cookies":[
    {
      "name":"login",
      "dateValue":"2019-01-01"
    },
    {
       "name":"email",
      "IntValue":32

    }

   ]
 }


# Nested 查询,通过bool查询进行过滤
POST cookie_service/_search
{
  "query": {
    "nested": {
      "path": "cookies",
      "query": {
        "bool": {
          "filter": [
            {
            "term": {
              "cookies.name": "age"
            }},
            {
              "range":{
                "cookies.intValue":{
                  "gte":30
                }
              }
            }
          ]
        }
      }
    }
  }
}



# 在Mapping中加入元信息,便于管理
PUT softwares/
{
  "mappings": {
    "_meta": {
      "software_version_mapping": "1.0"
    }
  }
}

GET softwares/_mapping
PUT softwares/_doc/1
{
  "software_version":"7.1.0"
}

DELETE softwares
# 优化,使用inner object
PUT softwares/
{
  "mappings": {
    "_meta": {
      "software_version_mapping": "1.1"
    },
    "properties": {
      "version": {
        "properties": {
          "display_name": {
            "type": "keyword"
          },
          "hot_fix": {
            "type": "byte"
          },
          "marjor": {
            "type": "byte"
          },
          "minor": {
            "type": "byte"
          }
        }
      }
    }
  }
}


#通过 Inner Object 写入多个文档
PUT softwares/_doc/1
{
  "version":{
  "display_name":"7.1.0",
  "marjor":7,
  "minor":1,
  "hot_fix":0  
  }

}

PUT softwares/_doc/2
{
  "version":{
  "display_name":"7.2.0",
  "marjor":7,
  "minor":2,
  "hot_fix":0  
  }
}

PUT softwares/_doc/3
{
  "version":{
  "display_name":"7.2.1",
  "marjor":7,
  "minor":2,
  "hot_fix":1  
  }
}


# 通过 bool 查询,
POST softwares/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "match":{
            "version.marjor":7
          }
        },
        {
          "match":{
            "version.minor":2
          }
        }

      ]
    }
  }
}




# Not Null 解决聚合的问题
DELETE ratings
PUT ratings
{
  "mappings": {
      "properties": {
        "rating": {
          "type": "float",
          "null_value": 1.0
        }
      }
    }
}


PUT ratings/_doc/1
{
 "rating":5
}
PUT ratings/_doc/2
{
 "rating":null
}


POST ratings/_search
POST ratings/_search
{
  "size": 0,
  "aggs": {
    "avg": {
      "avg": {
        "field": "rating"
      }
    }
  }
}

POST ratings/_search
{
  "query": {
    "term": {
      "rating": {
        "value": 1
      }
    }
  }
}

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggefik
系列文章
更多 icon
同类精品
更多 icon
继续加载