Elasticsearch-数据建模实例

本文详细介绍了Elasticsearch的数据建模过程,包括功能需求与性能需求的平衡,字段类型的选取如Text与Keyword的区别,以及如何针对搜索、分词、聚合和排序进行字段设置。此外,还讨论了存储优化,如字段存储与_source的使用,并给出了图书索引的实例,展示了如何应对需求变更。最后,提到了Mapping参数的重要设置,以确保高效的数据管理和检索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

什么是数据建模

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

如何对字段进行建模

字段类型

字段类型 Text VS Keyword

字段类型:结构化数据

搜索及分词

聚合及排序

额外的存储 

一个数据建模的实例

优化字段设定

需求变更

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

Mapping字段的相关设置


什么是数据建模

  • 数据建模,是创建数据模型的过程

    • 数据建模是对真实世界进行抽象描述的一种工具和方法,实现对现实世界的映射.

      • 博客/作者/用户评论

    • 三个过程:概念模型=>逻辑模型=>数据模型(第三范式)

      • 数据模型:结合具体的数据库,在满足业务读写性能等需求的前提下,确定最终的定义

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

如何对字段进行建模

  • 确定字段类型--->是否需要搜索及分词--->是否需要聚合及排序--->是否需要额外的存储

字段类型

字段类型 Text VS 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

额外的存储 

  • 是否需要专门存储当前字段数据

    • Store设置成true,可以存储该字段的原始内容

    • 一般结合_source的enabl为false时候使用

  • Disable_source:节约磁盘(source的信息不需要在磁盘上做一个保存);适用于指标型数据(指标型文档不需要做任何的更新操作)

    • 一般先考虑增加压缩比

    • 无法看到_source字段,无法做Reindex,无法Update

一个数据建模的实例

优化字段设定

  • 图书索引
    • 书名:支持全文和精确匹配
    • 简介:支持全文
    • 作者:精确值
    • 发行日期:日期类型
    • 图书封面:精确值(因为没有必要对封面进行搜索所以可以有额外的设置:(1)如果index设置为false,不支持搜索,支持Terms聚合;(2)如果将enabled设为false,则无法进行搜索和聚合分析)

#添加数据,dynamic Mapping
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"
}

#查看Maping
GET books/_mapping

 #手动设置mapping:"index": false则不能通过该字段进行索引,数据还是会出现在_source中
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
      }
    }
  }
}

 

需求变更

  • 新需求:增加图书内容的字段.并要求被搜索同时高亮显示

  • 新需求会导致 _source 的内容过大(造成大量的存储空间的占用,数据在网络间传输的开销)

    • Source Filtering只是传输给客户端时进行过滤,Fetch数据时,ES节点还是会传输 _source中的数据

  • 解决办法

    • 关闭_source

    • 然后将每个字段的"store"设置成true(数据被额外的存储在ES当中)

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

  • 返回结果不包含 _source字段

  • 对于需要显示的信息,可以在查询中指定"stored_fields"

  • 禁止_source字段后,还是支持使用highlightsAPI,高亮显示content中匹配的相关信息

 #重现设定mapping文件,新增 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
      }
    }
  }
}


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":{}
    }
  }
}

Mapping字段的相关设置

  • Mapping parameters | Elasticsearch Guide [8.1] | Elastic

    • 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的自动更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值