1、mapping
功能:用于定义索引库中类型表的每个字段的属性
字段名称 字段类型 是否做分词 用什么分词器 是否构建索引
数据类型
GET / school/ _mapping?pretty
text:string
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:是否对这一列构建索引