2019独角兽企业重金招聘Python工程师标准>>>
业务场景:
现在的系统设计不满足业务需求时就需要升级服务,为了满足业务需求有时需要对ES中 字段进行添加或修改操作,接下来我们查看为 mappings 添加字段的相关操作:
在讲解添加字段的es原生API之前,我看先来说下查看ElasticSearch中索引信息的相关API:
一、查看索引Setting配置信息
GET http://xxx.xxx.xxx.xxx:9200/supplier_index/_settings
{
"supplier_index": {
"settings": {
"index": {
"refresh_interval": "1s",
"number_of_shards": "2",
"provided_name": "supplier_index",
"creation_date": "1528175417715",
"analysis": {
"filter": {
"pinyin_first_letter_and_full_pinyin_filter": {
"keep_joined_full_pinyin": "true",
"keep_none_chinese_in_first_letter": "true",
"lowercase": "true",
"keep_original": "false",
"keep_first_letter": "true",
"trim_whitespace": "true",
"type": "pinyin",
"keep_none_chinese": "false",
"limit_first_letter_length": "16",
"keep_full_pinyin": "false"
},
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": "1",
"max_gram": "15"
},
"full_pinyin_filter": {
"keep_joined_full_pinyin": "true",
"keep_none_chinese_in_first_letter": "true",
"lowercase": "true",
"keep_original": "true",
"keep_first_letter": "true",
"trim_whitespace": "true",
"type": "pinyin",
"keep_none_chinese": "false",
"limit_first_letter_length": "16",
"keep_full_pinyin": "false"
}
},
"analyzer": {
"chinese_analyzer": {
"filter": [
"lowercase",
"autocomplete_filter"
],
"char_filter": [
"html_strip"
],
"type": "custom",
"tokenizer": "keyword"
},
"full_prefix_analyzer": {
"filter": [
"lowercase",
"full_pinyin_filter",
"autocomplete_filter"
],
"char_filter": [
"html_strip"
],
"type": "custom",
"tokenizer": "keyword"
},
"pinyin_analyzer": {
"filter": [
"pinyin_first_letter_and_full_pinyin_filter",
"autocomplete_filter"
],
"char_filter": [
"html_strip"
],
"type": "custom",
"tokenizer": "keyword"
}
}
},
"number_of_replicas": "1",
"uuid": "jGquRxITT1KuhIE-QoZbKw",
"version": {
"created": "5050099"
}
}
}
}
}
二、查看索引相关mappings相关配置信息
GET http://xxx.xxx.xxx.xxx:9200/supplier_index/_mapping
{
"supplier_index": {
"mappings": {
"category_type": {
"properties": {
"advanceCharge": {
"type": "long"
},
"beginMembers": {
"type": "long"
},
"beginMoney": {
"type": "long"
},
"categoryId": {
"type": "long"
},
"categoryJoinId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"categoryJoinName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"cityIds": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"clearRequirment": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"commMode": {
"type": "long"
},
"commRate": {
"type": "long"
},
"createTime": {
"type": "date",
"format": "MMM dd, yyyy hh:mm:ss aa||yyyy-MM-dd HH:mm:ss||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
},
"evaluateRate": {
"type": "long"
},
"id": {
"type": "long"
},
"invoiceTaxPoint": {
"type": "long"
},
"invoiceType": {
"type": "long"
},
"isDel": {
"type": "integer"
},
"isSpecial": {
"type": "long"
},
"isTest": {
"type": "long"
},
"orderNum": {
"type": "long"
},
"orderStatus": {
"type": "long"
},
"otherRequirment": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"platformMode": {
"type": "long"
},
"promiseIds": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"receiveCondition": {
"type": "long"
},
"suppilerType": {
"type": "long"
},
"supplierId": {
"type": "long"
},
"supplierName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"updateTime": {
"type": "date",
"format": "MMM dd, yyyy hh:mm:ss aa||yyyy-MM-dd HH:mm:ss||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
},
"weight": {
"type": "long"
}
}
},
"goods_type": {
"properties": {
"attrIds": {
"type": "text"
},
"auditStatus": {
"type": "integer"
},
"auditorId": {
"type": "long"
},
"brandId": {
"type": "long"
},
"brandName": {
"type": "text"
},
"cateId": {
"type": "long"
},
"cateJoinId": {
"type": "text"
},
"cateJoinName": {
"type": "text"
},
"cateName": {
"type": "text"
},
"cityIds": {
"type": "text"
},
"createTime": {
"type": "date",
"format": "MMM dd, yyyy hh:mm:ss aa||yyyy-MM-dd HH:mm:ss||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
},
"goodsImage": {
"type": "text"
},
"goodsImageId": {
"type": "long"
},
"goodsName": {
"type": "text"
},
"goodsStorage": {
"type": "integer"
},
"id": {
"type": "long"
},
"isDel": {
"type": "integer"
},
"lineStatus": {
"type": "integer"
},
"marketPrice": {
"type": "integer"
},
"saleNum": {
"type": "integer"
},
"shopId": {
"type": "long"
},
"skuId": {
"type": "long"
},
"specOpen": {
"type": "integer"
},
"startTime": {
"type": "date",
"format": "MMM dd, yyyy hh:mm:ss aa||yyyy-MM-dd HH:mm:ss||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
},
"supplierId": {
"type": "long"
},
"supplierName": {
"type": "text"
},
"updateTime": {
"type": "date",
"format": "MMM dd, yyyy hh:mm:ss aa||yyyy-MM-dd HH:mm:ss||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
}
}
},
"supplier_type": {
"properties": {
"address": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"businessLevel": {
"type": "long"
},
"cashDeposit": {
"type": "long"
},
"categoryIds": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"chargeEmail": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"chargeName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"chargePhone": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"cityId": {
"type": "long"
},
"cityIds": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"companyProfile": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"countryId": {
"type": "long"
},
"createTime": {
"type": "date",
"format": "MMM dd, yyyy hh:mm:ss aa||yyyy-MM-dd HH:mm:ss||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
},
"establishYear": {
"type": "long"
},
"goodsNum": {
"type": "long"
},
"id": {
"type": "long"
},
"isDel": {
"type": "integer"
},
"isTest": {
"type": "long"
},
"isUsed": {
"type": "long"
},
"memberRange": {
"type": "long"
},
"orderStatus": {
"type": "long"
},
"orderSum": {
"type": "long"
},
"provinceId": {
"type": "long"
},
"serviceAdvantage": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"serviceCategoryIds": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"serviceDisadvantage": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"simpleName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"status": {
"type": "long"
},
"supplierName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"typicalCustomer": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"updateTime": {
"type": "date",
"format": "MMM dd, yyyy hh:mm:ss aa||yyyy-MM-dd HH:mm:ss||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
},
"userAccount": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
三、添加索引+类型的 字段
为索引self_index_v1的self_type类型添加字段
PUT http://172.16.11.119:9200/self_index_v1/_mapping/self_type
==>
{
"self_type": {
"properties":{
"serviceAdvantage":{
"type": "text",
"index": "not_analyzed"
}
}
}
}
不出以外的话ack提示添加成功
如果要修改一个字段呢,比如onSale字段类型为boolean,现在想要修改为string类型,去尝试一下:
curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping?pretty" -d '{
"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
}
ElasticSearch是不允许修改字段的type类型的,原因是一个字段的类型进行修改之后,ES会重新建立对这个字段的索引信息,ElasticsSearch的底层是Lucene库,字段类型修改至少需要设设计到分词方式,相关度,TF/IDF等倒排的生成 , 不允许修改在我看来应该是Lucene机制导致的。
不允许修改ElasticSearch索引这篇博文讲的很清楚,推荐一下:
Elasticsearch 的坑爹事——记录一次mapping field修改过程