Elasticsearch (八) mapping

28 篇文章 0 订阅
14 篇文章 0 订阅

1.elasticsearch中的mapping类似于sql中的建表语句,sql中一个字段声明为int类型,那么以后这个字段只能存储int类型,在elasticsearch中也是一样。

2.除了基本的数据类型定义之外,elasticsearch中mapping中指定的属性会更多,mapping不仅仅会告诉elasticsearch一个文档的某个字段类型是什么,还可以指定字段是否生成索引,索引时时使用何种分词器,搜索时使用何种分词器,字段的权重,路由信息等。

3.对比mysql

create datebase  test;

use test;

CREATE TABLE 'newtest'(
'id'  int(10) unsigned NOT NULL AUTO_INCREMENT,
'title'  varchar(80) NOT NULL DEFAULT '',
'content'  text NOT NULL,
'postdate'  datetime NOT NULL,
 PRIMARY  KEY ('id') 
);

elasticsearch

DELETE  blog

PUT blog

GET blog/_mapping

PUT blog/article/_mapping
{
 "properties": {
   "id":{
     "type": "long"
   },
   "title":{
     "type": "text",
     "analyzer": "ik_max_word",
     "search_analyzer": "ik_max_word"
   },
   "content":{
     "type": "text",
     "analyzer": "ik_max_word",
     "search_analyzer": "ik_max_word"
   },
   "postdate":{
     "type": "date",
     "format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
   }
   
 } 
  
  
}

4.动态mapping

elasticsearch可以根据文档格式自动推测字段的属性并生成mapping,当你往elasticsearch中的索引添加文档的时候,出现一个以前没有出现过的字段,动态mapping机制可以根据字段值的格式来决定它的数据类型并自动添加映射。但是有时候这种行为并不需要,而且自动推测数据类型并不是100%准确

 

dynamic属性可以控制新增字段 有三个取值

true :新发现的字段添加到映射中 (默认)

false :新检测到的字段被忽略,必须显式添加新字段

strict:如果检测到新字段就会引发异常并拒绝

PUT test
PUT test/testa/_mapping
{
  "dynamic":"strict",
  "properties": {
    "id":{
      "type": "long"
    },
    "title":{
      "type": "text"
    }
    
  }
}

测试案例就不贴了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值