es查询index生成时间_【Elasticsearch】Elasticsearch索引的创建、查看及修改

本文介绍了如何在Elasticsearch中创建、查看和修改索引映射(mapping),包括设置不同类型的字段如string、date、boolean等。通过curl命令演示了创建索引、添加属性、查看mapping结果以及尝试修改mapping的过程,并指出字段类型一旦设定,无法直接修改,因为这涉及到数据的重新索引和分词方式变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转:

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.youkuaiyun.com/liuxiao723846/article/details/78444472

mapping的写入与查看

使用elasticsearch保存数据之前创建索引非常关键,一个好的索引使后续业务的查询更加方便快捷,我们创建索引时如果不指定相关信息,会按照默认设置创建,如果我们想要更加强大的功能,比如中文检索、拼音检索、首拼检索,就需要我们自己规划索引的创建,一般索引创建后不能更改,所以创建索引时要特别注意。下面是创建索引的最基础的步骤,供新手们参考。

以下POST命令如果执行不成功可以换成PUT尝试,本人在kibana软件中亲测有效。

1、首先创建一个索引:

curl -XPOST "http://127.0.0.1:9200/productindex"

命令执行正常则返回:{"acknowledged":true}

现在只创建了一个索引,并没有设置mapping,查看一下索引mapping的内容:

curl -XGET "http://127.0.0.1:9200/productindex/_mapping?pretty"

{

"productindex" : {

"mappings" : { }

}

}

2、给索引添加属性

可以看到mapping为空,我们只创建了一个索引,并没有进行mapping配置,mapping自然为空。

下面给productindex这个索引加一个type,type name为product,并设置mapping:

curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping?pretty" -d '

{

"product": {

"properties": {

"title": {

"type": "string",

"store": "yes"

},

"description": {

"type": "string",

"analyzer": "standard"

},

"price": {

"type": "double"

},

"onSale": {

"type": "boolean"

},

"type": {

"type": "integer"

},

"createDate": {

"type": "date"

}

}

}

}

命令执行正常则返回:{"acknowledged" : true}

3、查看mapping结果

上面的操作中,我们给productindex加了一个type,并写入了product的mapping信息,再次查看:

curl -XGET "http://127.0.0.1:9200/productindex/_mapping"

{

"productindex" : {

"mappings" : {

"product" : {

"properties" : {

"createDate" : {

"type" : "date",

"format" : "strict_date_optional_time||epoch_millis"

},

"description" : {

"type" : "string",

"analyzer": "standard"

},

"onSale" : {

"type" : "boolean"

},

"price" : {

"type" : "double"

},

"title" : {

"type" : "string",

"store" : true

},

"type" : {

"type" : "integer"

}

}

}

}

}

}

4、修改mapping(新增一个新字段)

如果想给product新增一个字段,那么需要修改mapping,尝试一下:

curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping

{

"product": {

"properties": {

"amount":{

"type":"integer"

}

}

}

}'

{

5、修改mapping(修改原有字段)

如果要修改一个字段的类型呢,比如onSale字段的类型为boolean,现在想要修改为string类型,尝试一下:

curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping

{

"product": {

"properties": {

"onSale":{

"type":"string"

}

}

}

}

返回错误:

{

"error" : {

"root_cause" : [ {

"type" : "illegal_argument_exception",

"reason" : "mapper [onSale] of different type, current_type [boolean], merged_type [string]"

} ],

"type" : "illegal_argument_exception",

"reason" : "mapper [onSale] of different type, current_type [boolean], merged_type [string]"

},

"status" : 400

}

为什么不能修改一个字段的type?原因是一个字段的类型修改以后,那么该字段的所有数据都需要重新索引。Elasticsearch底层使用的是lucene库,字段类型修改以后索引和搜索要涉及分词方式等操作,不允许修改类型在我看来是符合lucene机制的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值