ElasticSearch--mapping元数据

mapping就是index下type的元数据,每一个type都有一个自己的mapping,决定了数据类型,建立倒排索引的行为,以及搜索的行为

  • mapping数据类型

  1. 字符型:string
  2. 整数型:byte,short,integer,long
  3. 浮点型:float,double
  4. 布尔型:boolean
  5. 日期型:date
  • dynamic mapping(动态分配)原则

  1. true或false -->boolean
  2. 123-->long
  3. 123.45-->double
  4. 2017-01-01-->date
  5. "hello world"-->string/text
  • Field内部存储结构

  1. multivalue field,如:{"tags":["tage1","tage2","tage3"]}
  2. enpty field,如:null, [ ], [null]
  3. object field内部存储方式如下:

例1:

{
    "names": {
        "fname": "zeng",
        "mname": "sam",
        "fullname": "sam.zeng"
    }
}

内部会如下存储:

{
    "names.fname": "zeng",
    "names.mname": "sam",
    "names.fullname": "sam.zeng"
}

例2:

{
    "info": [{
        "name": "zeng",
        "age": 16
    }, {
        "name": "sam",
        "age": 20
    }]
}

内部会如下存储:

{
    "info.name": ["zeng", "sam"],
    "info.age": [16, 20]
}

  • 查看mapping元数据语法:

GET index/type/_mapping

或者: GET index/_mapping/type

  • 示例

GET accounts/person/_mapping
返回:
{
  "accounts": {
    "mappings": {
      "person": {
        "properties": {
          "age": {
            "type": "long"
          },
          "desc": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "salary": {
            "type": "long"
          },
          "tag": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            },
            "fielddata": true
          },
          "tags": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "title": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            },
            "fielddata": true
          },
          "user": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}
  • 自定义mapping

PUT newindex
{
  "mappings": {
    "newtype":{
      "properties": {
        "name":{
          "type": "text",
          "analyzer": "english"
        },
        "age":{
          "type": "integer"
        },
        "salary":{
          "type": "double"
        },
        "sex":{
          "type": "boolean"
        },
        "createDate":{
          "type": "date"
        },
        "hobby":{
          "type": "text",
          "index": "not_analyzed",
          "analyzer": "standard"
        }
      }
    }
  }
}
返回:
{
  "acknowledged": true,
  "shards_acknowledged": true
}

查看:
GET newindex/_mapping/newtype
返回:
{
  "newindex": {
    "mappings": {
      "newtype": {
        "properties": {
          "age": {
            "type": "integer"
          },
          "createDate": {
            "type": "date"
          },
          "hobby": {
            "type": "text",
            "analyzer": "standard"
          },
          "name": {
            "type": "text",
            "analyzer": "english"
          },
          "salary": {
            "type": "double"
          },
          "sex": {
            "type": "boolean"
          }
        }
      }
    }
  }
}
  • 不可修改mapping元数据

报如下错误:

PUT newindex
{
  "mappings": {
    "newtype":{
      "properties": {
        "name":{
          "type": "double"
        }
      }
    }
  }
}

返回:

{
  "error": {
    "root_cause": [
      {
        "type": "index_already_exists_exception",
        "reason": "index [newindex/_dBTOyVxRVeUiaUDjx_beg] already exists",
        "index_uuid": "_dBTOyVxRVeUiaUDjx_beg",
        "index": "newindex"
      }
    ],
    "type": "index_already_exists_exception",
    "reason": "index [newindex/_dBTOyVxRVeUiaUDjx_beg] already exists",
    "index_uuid": "_dBTOyVxRVeUiaUDjx_beg",
    "index": "newindex"
  },
  "status": 400
}

  • 增加mapping元数据

PUT newindex/_mapping/newtype
{
  "properties": {
    "addFiled2":{
      "type": "text"
    }
  }
}
返回:
{
  "acknowledged": true
}

查看:
GET newindex/_mapping/newtype
返回:
{
  "newindex": {
    "mappings": {
      "newtype": {
        "properties": {
          "addFiled2": {
            "type": "text"
          },
          "age": {
            "type": "integer"
          },
          "createDate": {
            "type": "date"
          },
          "hobby": {
            "type": "text",
            "analyzer": "standard"
          },
          "name": {
            "type": "text",
            "analyzer": "english"
          },
          "salary": {
            "type": "double"
          },
          "sex": {
            "type": "boolean"
          }
        }
      }
    }
  }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值