ElasticSearch学习30_Elasticsearch索引mapping的写入、查看与修改

mapping的写入与查看

首先创建一个索引:

curl -XPOST "http://127.0.0.1:9200/productindex"
{"acknowledged":true}  
 
 
  • 1
  • 2
  • 1
  • 2

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

curl -XGET "http://127.0.0.1:9200/productindex/_mapping?pretty" 
{
  "productindex" : {
    "mappings" : { }
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

可以看到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",
                    "index": "not_analyzed"
                },
                "price": {
                    "type": "double"
                },
                "onSale": {
                    "type": "boolean"
                },
                "type": {
                    "type": "integer"
                },
                "createDate": {
                    "type": "date"
                }
            }
        }
  }
'

{
  "acknowledged" : true
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

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

curl -XGET "http://127.0.0.1:9200/productindex/_mapping?pretty"
{
  "productindex" : {
    "mappings" : {
      "product" : {
        "properties" : {
          "createDate" : {
            "type" : "date",
            "format" : "strict_date_optional_time||epoch_millis"
          },
          "description" : {
            "type" : "string",
            "index" : "not_analyzed"
          },
          "onSale" : {
            "type" : "boolean"
          },
          "price" : {
            "type" : "double"
          },
          "title" : {
            "type" : "string",
            "store" : true
          },
          "type" : {
            "type" : "integer"
          }
        }
      }
    }
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

修改mapping

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

curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping?pretty" -d '{
     "product": {
                "properties": {
                     "amount":{
                        "type":"integer"
                   }
                }
            }
    }'
{
  "acknowledged" : true
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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

 curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping?pretty" -d '{
     "product": {
                "properties": {
                 "onSale":{
                    "type":"string" 
               }
            }
        }
}'
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

返回错误:

{
  "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
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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

这里有一篇关于修改mapping字段的博客,叙述的比较清楚:Elasticsearch 的坑爹事——记录一次mapping field修改过程,可以参考.


原文来自:http://blog.csdn.net/napoay/article/details/52012249

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值