es--映射管理与分片配置

1、mapping

  • 功能:用于定义索引库中类型表的每个字段的属性
    • 字段名称
    • 字段类型
    • 是否做分词
    • 用什么分词器
    • 是否构建索引
      在这里插入图片描述

数据类型

  • 查看索引库中每个字段的类型
GET /school/_mapping?pretty
  • text:string
    • 会分词的,包含keyword
    • 支持模糊、精确查询
  • keyword:string
    • 不分词
    • 支持模糊、精确查询
    • “I like zhangsan”
  • long:数值类型
  • date:日期类型
  • 如果创建索引库时,没有指定类型,ES会自动判断数据的类型

不指定类型

PUT /document/article/1
{
  "title" : "elasticsearchshi是是什么",
  "author" : "zhangsan",
  "titleScore" : 60
}
​
GET document/_mapping?pretty
​
PUT /document/article/2
{
  "title" : "elasticsearchshi是是什么",
  "author" : "lisi",
  "titleScore" : 88.88
}
​
GET /document/article/_search?pretty
​
如果以后通过Java读取titleScore这一列就会报类型转换异常

指定类型

DELETE  document
​
PUT document
{
  "mappings": {
    "article" : {
      "properties":
      {
        "title" : {"type": "text"} , 
        "author" : {"type": "text"} , 
        "titleScore" : {"type": "double"} 
      }
    }
  }
}
​
GET document/article/_mapping
  • 在实际工作中,如果数据的格式【列】固定一定要指定类型
  • 动态添加字段【field】
--删除索引库
DELETE school
--创建索引库school,type为logs,定义该索引库有一列messages,text类型
PUT school
{
  "mappings": {
    "logs" : {                                     
      "properties": {"messages" : {"type": "text"}}
    }
  }
}
--查看index的结构定义
GET /school/_mapping/logs
​
--动态添加一列
POST /school/_mapping/logs
{
  "properties": {"number" : {"type": "text"}}
}
​
GET /school/_mapping/logs
  • 获取某个字段的信息
GET /{index}/_mapping/{type}/field/{field}
​
GET /school/_mapping/logs/field/number

2、配置管理:setting

  • 功能
    • 管理整个ES中所有属性的配置
    • 修改任意默认的配置
      • 默认ES中每个索引库有5个分片,每个分片有1个副本分片
  • 创建一个普通的索引库
DELETE document
PUT document
{
  "mappings": {
    "article" : {
      "properties":
      {
        "title" : {"type": "text"} , 
        "author" : {"type": "text"} , 
        "titleScore" : {"type": "double"} 
        
      }
    }
  }
}
​
GET /document/_settings
​
​
{
  "document": {
    "settings": {
      "index": {
        "creation_date": "1593829044251",
        "number_of_shards": "5",                    --分片个数
        "number_of_replicas": "1",                  --分片的副本个数
        "uuid": "lHDwtCJ2QVqWorgbF0WnVA",           --唯一id
        "version": {
          "created": "6000099"
        },
        "provided_name": "document"
      }
    }
  }
}
  • 修改副本的个数
PUT /document/_settings
{
  "number_of_replicas": 2
}
  • 修改分片个数:不允许修改
PUT /document/_settings
{
  "number_of_shards": 3
}
  • 如果非要更改:创建一个新的,导入数据,自己手动做数据迁移
--创建第一个索引库
DELETE articles1
PUT articles1
{  
    "settings":{  
         "number_of_shards":3,  
         "number_of_replicas":1  
    },  
    "mappings":{  
         "article":{  
             "dynamic":"strict",  
             "properties":{  
                 "id":{"type": "text", "store": true},  
                 "title":{"type": "text","store": true}, 
                 "readCounts":{"type": "integer","store": true},  
                 "times": {"type": "text", "index": false}
             }  
         }  
    }  
}
​
​
--插入一条数据
PUT articles1/article/1
{
  "id" : "1",
  "title" : "世界1",
  "readCounts" : 2 , 
  "times" : "2018-05-01"
}
​
get articles1/article/1
​
--创建第二个索引库
DELETE articles2
PUT articles2
{  
    "settings":{  
         "number_of_shards":5,  
         "number_of_replicas":1  
    },  
    "mappings":{  
         "article":{  
             "dynamic":"strict",  
             "properties":{  
                 "id":{"type": "text", "store": true},  
                 "title":{"type": "text","store": true}, 
                 "readCounts":{"type": "integer","store": true},  
                 "times": {"type": "date", "index": false}
             }  
         }  
    }  
}  
​
​
GET articles2/article/1
​
--导入数据
POST _reindex
{
  "source": {
    "index": "articles1"
  },
  "dest": {
    "index": "articles2"
  }
}
  • 常见的属性
    • dynamic:定义列是否允许动态添加
      • strict:严格模式,不允许动态添加,如果你动态添加,会报错
      • false:忽略模式,不允许动态添加,如果你动态添加,操作会被忽略
      • true:允许动态添加
    • index:是否对这一列构建索引
      • true:构建索引
      • false:不构建索引
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值