elasticsearch系列-什么是Mapping

本篇主要讲解Mapping的一些相关配置与需要注意的地方,说到Mapping大家可能觉得有些不解,其实大体上可以将Elasticsearch理解为一个RDBMS(关系型 数据库 ,比如 MySQL ),那么index 就相当于数据库实例,type可以理解为表,这样mapping可以理解为表的结构和相关设置的信息(当然mapping有更大范围的意思)。
默认情况不需要显式的定义mapping, 当新的type或者field引入时,Elasticsearch会自动创建并且注册有合理的默认值的mapping(毫无性能压力), 只有要覆盖默认值时才必须要提供mapping定义。
什么是Mapping
先看看官方文档中的定义
A mapping defines the fields within a type, the datatype for each field, and how the field should be handled by Elasticsearch. A mapping is also used to configure metadata associated with the type.
Mapping定义了type中的诸多字段的数据类型以及这些字段如何被Elasticsearch处理,比如一个字段是否可以查询以及如何分词等。
自定义Mapping
下面是一个简单的Mapping定义:
curl -XPUT 'http://localhost:9200/megacorp' -d '
{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 1
    }, 
    "mappings": {
        "employee": {
            "properties": {
                "first_name": {
                    "type": "string"
                }, 
                "last_name": {
                    "type": "string"
                }, 
                "age": {
                    "type": "integer"
                }, 
                "about": {
                    "type": "string"
                }, 
                "interests": {
                    "type": "string"
                }, 
                "join_time": {
                    "type": "date", 
                    "format": "dateOptionalTime", 
                    "index": "not_analyzed"
                }
            }
        }
    }
}


其中employee是type(相当于关系数据库中的表),在employee中我们定义了first_name、last_name、age、about、interests、join_time这6个属性。
{ "interests": { "type": "string" }
type表示field的数据类型,上例中interests的type为string表示为普通文本。 
Elasticsearch支持以下数据类型:
  • 文本: string
  • 数字: byte, short, integer, long
  • 浮点数: float, double
  • 布尔值: boolean
  • Date: date
对于type为 string 的字段,最重要的属性是:index and analyzer。 
1、index  
index 属性控制string如何被索引,它有三个可选值:
  • analyzed: First analyze the string, then index it. In other words, 
index this field as full text.
  • not_analyzed:: Index this field, so it is searchable, but index the 
value exactly as specified. Do not analyze it.
  • no: Don’t index this field at all. This field will not be 
searchable.
对于string类型的filed index 默认值是: analyzed.如果我们想对进行精确查找, 那么我们需要将它设置为: not_analyzed。 
例如:
{ "tag": { "type": "string" , "index": "not_analyzed" }
对于 string类型的字段, 我们可以使用 analyzer 属性来指定在搜索阶段和索引阶段使用哪个分词器. 默认, Elasticsearch 使用 standard analyzer, 你也可以指定Elasticsearch内建的其它分词器,比如 whitespace, simple, or english:
例如:
{ "tweet": { "type": "string" , "analyzer": "english" }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值