ElasticSearch5.x實踐_day05_02_Mapping的寫入、查看與修改

首先建立一個索引:html

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

如今只建立了一個索引,並無設置mapping,查看一下索引mapping的內容:json

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:app

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的內容:code

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信息,再次查看:htm

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修改過程,能夠參考.blog

相關文章
相關標籤/搜索