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

额外的存储

● 是否需要专⻔存储当前字段数据

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

● ⼀般结合 _source 的 enabled 为 false 时候使⽤

● Disable _source:节约磁盘;适⽤于指标型数据

● ⼀般建议先考虑增加压缩⽐

● ⽆法看到 _source字段,⽆法做 ReIndex,⽆法做

Update

● Kibana 中⽆法做 discovery

举个例子

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

dynamic mapping 不符合我们的预期

GET books/_mapping

对映射进行优化

#优化字段类型

指定index 则不能通过该字段进行索引,数据还是会出现在_source中

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

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!关于学习Elasticsearch,我可以给你一些指导。Elasticsearch是一个开源的分布式搜索和分析引擎,主要用于快速、实时地存储、搜索和分析大量数据。下面是一些学习Elasticsearch的步骤: 1. 了解基本概念:开始学习Elasticsearch之前,你需要了解一些基本的概念,比如索引(index)、类型(type)、文档(document)、字段(field)等。这将帮助你更好地理解Elasticsearch的工作原理。 2. 安装和配置:根据你的操作系统,你可以从Elasticsearch官方网站下载并安装合适的版本。安装完成后,你需要进行适当的配置,如设置集群名称、分配内存等。 3. 学习REST API:Elasticsearch提供了丰富的REST API,用于与其进行交互。了解如何使用这些API来索引、搜索和删除数据是学习Elasticsearch的重要一步。 4. 索引和搜索数据:学习如何创索引、添加文档以及执行搜索操作是使用Elasticsearch的关键。掌握查询语法、过滤器、聚合操作等功能可以帮助你更有效地使用Elasticsearch。 5. 数据和分析:学习如何设计合适数据型和映射,以及如何使用Elasticsearch进行数据分析和可视化是提高你的技能的重要一步。 6. 扩展和优化:学习如何在生产环境中扩展和优化Elasticsearch集群是非常重要的。了解如何分片、复制、调优性能等将帮助你更好地管理和维护你的数据。 7. 学习资源:除了官方文档,还有很多优秀的学习资源可供参考,如书籍、教程和在线课程等。利用这些资源可以更系统地学习和掌握Elasticsearch。 希望这些步骤能对你学习Elasticsearch有所帮助!如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值