如何建个人网站,sem和网站建设的关系,网络怎样做推广,投票网站开发索引操作 index
创建索引
put 方法创建索引
使用 put 创建索引时必须指明文档id#xff0c;否则报错
# PUT 创建命令
# test1 索引名称
# type1 类型名称#xff0c;默认为_doc#xff0c;已经被废弃
# 1 文档id
PUT /test1/type1/1
{name:zhangsan否则报错
# PUT 创建命令
# test1 索引名称
# type1 类型名称默认为_doc已经被废弃
# 1 文档id
PUT /test1/type1/1
{name:zhangsan,age:18,birth:2000-01-20
}结果
#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{_index : test1, // 索引_type : type1, // 类型_id : 1, // id_version : 1, // 版本result : created, // 操作类型_shards : { // 分片信息total : 2,successful : 1,failed : 0},_seq_no : 0,_primary_term : 1
}post 方法创建索引
post 如果没有指明文档id会随机生成一个
POST /test2/type2
{name:zhangsan,age:18,birth:2000-01-20
}结果
#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{_index : test2,_type : type2,_id : 3D_WQY4BOf0ywiICmI8O,_version : 1,result : created,_shards : {total : 2,successful : 1,failed : 0},_seq_no : 0,_primary_term : 1
}对比 mysql
PUT test1/type1/1 索引test1相当于关系型数据库的库类型type1就相当于表 1 代表数据中的主键 id
这里需要补充的是 在 es5 版本前一个索引下可以创建多个类型但是在之后一个索引只能对应一个类型默认为 _doc而 id 相当于关系型数据库的主键id若果不指定就会默认生成一个20位的uuid属性相当关系型数据库的column列。
而结果中的 result 则是操作类型现在是 created 表示第一次创建。如果再次点击执行该命令那么 result 则会是 updated 我们细心则会发现 _version 开始是1现在你每点击一次就会增加一次。表示第几次更改。
查看索引信息
# get 索引名称
GET test1{test1 : {aliases : { },mappings : {properties : {age : {type : long},birth : {type : date},name : {type : text,fields : {keyword : {type : keyword,ignore_above : 256}}}}},settings : {index : {creation_date : 1710501416895,number_of_shards : 1,number_of_replicas : 1,uuid : hemDp4F3T5ePsAZmaO5Ijg,version : {created : 7080099},provided_name : test1}}}
}可以看到 name、age、birth 字段指定了类型 text、long、date 说明 es 会根据字段的值指定默认的类型
指定字段类型
如果想要自己指定字段的类型使用映射
PUT /test3
{mappings: {properties: {name:{type: text},age:{type: long},birth:{type: date}}}
}结果
{acknowledged : true,shards_acknowledged : true,index : test3
}映射数据是字段名json 数据上面只指定了type数据类型其实可以指定很多属性
type数据类型es 中支持的数据类型非常丰富主要使用以下几个 string 类型又分两种 text可分词keyword不可分词数据会作为完整字段进行匹配 Numerical数值类型分两类 基本数据类型long、integer、short、byte、double、float、half_float浮点数的高精度类型scaled_float Date日期类型Array数组类型Object对象 index是否索引默认为 true所有字段都会被索引被索引则可以用来搜索没有则不可以store是否将数据进行独立存储默认为 false原始的文本会存储在 _source 里面默认情况下其他提取出来的字段都不是独立存储的是从 _source 里面提取出来的。当然你也可以独立的存储某个字段只要设置 “store”: true 即可获取独立存储的字段要比从 _source 中解析快得多但是也会占用更多的空间所以要根据实际业务需求来设置。analyzer分词器这里的 ik_max_word 即使用 ik 分词器
查看索引映射
# get /索引名称/_mapping
GET /test3/_mapping结果
{test3 : {mappings : {properties : {age : {type : long},birth : {type : date},name : {type : text}}}}
}查看索引健康情况
GET _cat/indices?v可以查看我们所有索引的状态健康情况分片数据储存大小等等。 删除索引
# delete 索引名称
DELETE test3结果
{acknowledged : true
}