1、定制dynamic策略
(1)true:遇到陌生字段丛塌,就進(jìn)行dynamic mapping
(2)遇到陌生字段较解,就忽略
(3)strict: 遇到陌生字段畜疾,就報錯
實(shí)例
PUT /my_index
{
"mappings": {
"my_type": {
"dynamic" : "strict",
"properties": {
"title" : {
"type": "text"
},
"address" : {
"type": "object",
"dynamic" : "true"
}
}
}
}
}
創(chuàng)建了一個只能有title和address字段的type。因?yàn)閐ynamic:strict印衔。而address里面可以有任意字段啡捶,因?yàn)閐ynamic:true
Demo1
PUT /my_index/my_type/1
{
"title" : "my article",
"content" : "this is my article",
"address" : {
"province" : "guangdong",
"city" : "guangzhou"
}
}
結(jié)果
{
"error": {
"root_cause": [
{
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [content] within [my_type] is not allowed"
}
],
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [content] within [my_type] is not allowed"
},
"status": 400
}
報錯了,說type是strict_dynamic奸焙。沒有content瞎暑。
Demo2,刪除content
PUT /my_index/my_type/1
{
"title" : "my article",
"address" : {
"province" : "guangdong",
"city" : "guangzhou",
"area" : "heheda"
}
}
結(jié)果:
{
"_index": "my_index",
"_type": "my_type",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
查看剛才生成的mapping是什么樣的与帆?
GET /my_index/_mapping/my_type/
{
"my_index": {
"mappings": {
"my_type": {
"dynamic": "strict",
"properties": {
"address": {
"dynamic": "true",
"properties": {
"area": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"city": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"province": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"title": {
"type": "text"
}
}
}
}
}
}
2了赌、定制dynamic mapping策略
(1)date_detection
默認(rèn)會按照一定格式識別date,比如yyyy-MM-dd玄糟。但是如果某個field先過來一個2017-01-05的值揍拆,就會被自動dynamic mapping成date類型,后面如果再來一個比如“hello world”這種非date格式的value茶凳,就會報錯嫂拴。
可以手動關(guān)閉某個type的date_detection,如果有需要贮喧,自己手動指定某個field為date類型筒狠。
PUT /my_index/_mapping/my_type
{
"date_detection": false
}
(2)定制自己的dynamic mapping template(type level)
PUT /my_index2
{
"mappings": {
"my_type": {
"dynamic_templates": [
{
"en": {
"match": "*_en",
"match_mapping_type": "string",
"mapping": {
"type": "string",
"analyzer": "english"
}
}
}
]
}
}
}
新增兩條數(shù)據(jù)
PUT /my_index2/my_type/1
{
"title": "this is my first article"
}
PUT /my_index2/my_type/2
{
"title_en": "this is my first article"
}
進(jìn)行查詢
GET /my_index2/my_type/_search
{
"query": {
"match": {
"title": "is"
}
}
}
結(jié)果
{
"took": 98,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2824934,
"hits": [
{
"_index": "my_index2",
"_type": "my_type",
"_id": "1",
"_score": 0.2824934,
"_source": {
"title": "this is my first article"
}
}
]
}
}
可以查出記錄
GET /my_index2/my_type/_search
{
"query": {
"match": {
"titl_en": "is"
}
}
}
結(jié)果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
查詢不出結(jié)果
title沒有匹配到任何的dynamic模板,默認(rèn)就是standard分詞器箱沦,不會過濾停用詞辩恼,is會進(jìn)入倒排索引,用is來搜索是可以搜索到的
title_en匹配到了dynamic模板谓形,就是English分詞器灶伊,會過濾停用詞,is這種停用詞就會被過濾掉寒跳,用is來搜索就搜索不到了聘萨。
(3)定制自己的default mapping template(index level)
PUT /my_index
{
"mappings": {
"_default_": {
"_all": { "enabled": false }
},
"blog": {
"_all": { "enabled": true }
}
}
}
若有興趣,歡迎來加入群童太,【Java初學(xué)者學(xué)習(xí)交流群】:458430385米辐,此群有Java開發(fā)人員、UI設(shè)計(jì)人員和前端工程師书释。有問必答翘贮,共同探討學(xué)習(xí),一起進(jìn)步爆惧!
歡迎關(guān)注我的微信公眾號【Java碼農(nóng)社區(qū)】狸页,會定時推送各種干貨: