ElasticSearch5.x实践_day05_02_Mapping的写入、查看与修改

首先创建一个索引:

POST http://192.168.20.46:9200/my_index/my_type/1
{"name":"zhangsan"}

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

GET http://192.168.20.46:9200/my_index/_mapping?pretty
{
    "my_index": {
        "mappings": {
            "my_type": {
                "properties": {
                    "name": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            }
        }
    }
}

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

PUT http://192.168.20.46:9200/my_index
{
	"mappings":{
		"product":{
			"properties":{
				"title":{
					"type":"text",
					"store":"yes"
				},
				"description":{
					"type":"keyword",
					"index":"not_analyzed"
				},
				"price":{
					"type":"double"
				},
				"onSale":{
					"type":"boolean"
				},
				"type":{
					"type":"integer"
				},
				"createDate":{
					"type":"date"
				}
			}
		}
	}
}

查看一下索引mapping的内容:

ttp://192.168.20.46:9200/my_index/_mapping?pretty

{
    "my_index": {
        "mappings": {
            "product": {
                "properties": {
                    "createDate": {
                        "type": "date"
                    },
                    "description": {
                        "type": "keyword"
                    },
                    "onSale": {
                        "type": "boolean"
                    },
                    "price": {
                        "type": "double"
                    },
                    "title": {
                        "type": "text",
                        "store": true
                    },
                    "type": {
                        "type": "integer"
                    }
                }
            }
        }
    }
}

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

PUT http://192.168.20.46:9200/my_index/_mapping/product
{
    "product": {
        "properties": {
            "english_title": {
                "type":     "string",
                "analyzer": "english"
            }
        }
    }
}

 

GET http://192.168.20.46:9200/my_index/_mapping?pretty
{
    "my_index": {
        "mappings": {
            "product": {
                "properties": {
                    "createDate": {
                        "type": "date"
                    },
                    "description": {
                        "type": "keyword"
                    },
                    "english_title": {
                        "type": "text",
                        "analyzer": "english"
                    },
                    "onSale": {
                        "type": "boolean"
                    },
                    "price": {
                        "type": "double"
                    },
                    "title": {
                        "type": "text",
                        "store": true
                    },
                    "type": {
                        "type": "integer"
                    }
                }
            }
        }
    }
}

字段的type类型是不能修改的,为什么不能修改一个字段的type?原因是一个字段的类型修改以后,那么该字段的所有数据都需要重新索引。Elasticsearch底层使用的是lucene库,字段类型修改以后索引和搜索要涉及分词方式等操作,不允许修改类型看来是符合lucene机制的。
这里有一篇关于修改mapping字段的博客,叙述的比较清楚:Elasticsearch 的坑爹事——记录一次mapping field修改过程,可以参考.

 

 

 

转载于:https://my.oschina.net/LucasZhu/blog/1491737

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值