61、索引管理_定制化自己的dynamic mapping策略

2020-01-10  本文已影响0人  拉提娜的爸爸

1、定制dynamic策略

true:遇到陌生字段,就进行dynamic mapping
false:遇到陌生字段,就忽略
strict:遇到陌生字段,就报错

示例:创建索引,并且设置my_type类型不可添加陌生字段,但是Object类型的address字段下可以新增字段,并进行dynamic mapping
PUT /my_index
{
  "mappings": {
    "my_type":{
      "dynamic":"strict",
      "properties": {
        "title":{
          "type": "text"
        },
        "address":{
          "type": "object",
          "dynamic":"true"
        }
      }
    }
  }
}
测试1:往my_type里添加document数据,并尝试新增content字段
{
  "title": "my article",
  "content": "this is my article",
  "address": {
    "province": "guangdong",
    "city": "guangzhou"
  }
}
--------------------------------运行结果--------------------------------
{
  "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
}

结果报错,不可添加content字段

测试2:往my_type里添加document数据,保持原有字段
PUT /my_index/my_type/1
{
  "title": "my article",
  "address": {
    "province": "guangdong",
    "city": "guangzhou"
  }
}
--------------------------------运行结果--------------------------------
{
  "_index": "my_index",
  "_type": "my_type",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}
查看my_type的定制mapping
GET /my_index/_mapping/my_type
--------------------------------运行结果--------------------------------
{
  "my_index": {
    "mappings": {
      "my_type": {
        "dynamic": "strict",
        "properties": {
          "address": {
            "dynamic": "true",
            "properties": {
              "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

默认会按照一定格式识别date,比如yyyy-MM-dd。但是如果某个field先过来一个2017-01-01的值,就会被自动dynamic mapping成date,后面如果再来一个"hello world"之类的值,就会报错。可以手动关闭某个type的date_detection,如果有需要,自己手动指定某个field为date类型。

关闭某个type的date_detection
PUT /my_index/_mapping/my_type
{
    "date_detection": false
}

(2)定制自己的dynamic mapping template(type level)

示例:设置以‘_en’结尾的field数据类型为string,并使用english分词器。
PUT /my_index
{
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        { "en": {
          "match": "*_en", 
            "match_mapping_type": "string",
              "mapping": {
                "type": "string",
                  "analyzer": "english"
              }
        }}
     ]
}}}
测试:添加两条document数据,其中一条field字段以_en结尾,并对其进行查询
PUT /my_index/my_type/1
{
  "title": "this is my first article"
}

PUT /my_index/my_type/2
{
  "title_en": "this is my first article"
}
-----------------------------------------查询-----------------------------------------
GET /my_index/my_type/_search
{
  "query": {
    "match": {
      "title_en": "is"
    }
  }
}

GET /my_index/my_type/_search
{
  "query": {
    "match": {
      "title": "is"
    }
  }
}

结果:title没有匹配到任何的dynamic模板,默认就是standard分词器,不会过滤停用词,is会进入倒排索引,用is来搜索是可以搜索到的
title_en匹配到了dynamic模板,就是english分词器,会过滤停用词,is这种停用词就会被过滤掉,用is来搜索就搜索不到了.

(3)定制自己的default mapping template(index level)

示例:默认关闭_all field搜索,type名为blog的_all field搜索除外
PUT /my_index
{
  "mappings": {
    "_default_": {
      "_all": { "enabled":  false }
    },
    "blog": {
      "_all": { "enabled":  true  }
    }
  }
}
上一篇下一篇

猜你喜欢

热点阅读