Elasticsearch的dynamic策略

1、定制dynamic策略

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

PUT /my_index
{
  "mappings": {
    "my_type": {
      "dynamic": "strict",            //设置了strict,添加字段报错
      "properties": {
        "title": {
          "type": "text"
        },
        "address": {
          "type": "object",
          "dynamic": "true"           //设置为true可以添加字段
        }
      }
    }
  }
}

PUT /my_index/my_type/1
{
  "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
}


PUT /my_index/my_type/1
{
  "title": "my article",
  "address": {
    "province": "guangdong",
    "city": "guangzhou"        //添加新的字段
  }
}

//不报错
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类型。

PUT /my_index/_mapping/my_type
{
    "date_detection": false
}

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

PUT /my_index
{
    "mappings": {
        "my_type": {
            "dynamic_templates": [
                { "en": {
                      "match":              "*_en", 
                      "match_mapping_type": "string",
                      "mapping": {
                          "type":           "string",
                          "analyzer":       "english"
                      }
                }}
            ]
}}}

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"
}

title没有匹配到任何的dynamic模板,默认就是standard分词器,不会过滤停用词,is会进入倒排索引,用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  }
        }
    }
}

 

Elasticsearch是一个强大的全文搜索引擎,支持对文本进行高效搜索和分析。在处理多语言或需要复杂分析的场景中,可能会遇到需要使用多个分词器(tokenizer)的情况。Elasticsearch中的分词器负责将输入的文本分割成单词,这是索引和搜索的基础。 当您想同时使用多个分词器时,可以采取以下方法: 1. **复合分词器**(Composite Tokenizer):这是一个特殊的分词器,它会将其他分词器的结果连接在一起。您可以配置多个子分词器,并按照指定顺序应用它们,这样每个文档会被用不同的分词器分别处理,最后组合结果。 示例配置: ```json { "tokenizer": { "custom_tokenizer": { "type": "composite", "tokenizers": [ { "type": "standard" }, // 标准分词器 { "type": "ik_max_word" } // 中文分词器 ] } } } ``` 2. **动态分词**(Dynamic Tokenizer):如果不同字段需要不同的分词策略,可以在映射(mapping)中为每个字段指定单独的分词器。 ```json { "mappings": { "properties": { "english_text": { "tokenizer": "standard" }, "chinese_text": { "tokenizer": "ik_max_word" } } } } ``` 3. **管道操作**(Pipeline):虽然默认情况下一个文档只能有一个主分词器,但可以通过管道(pipeline)设置,将多个阶段串联起来执行,包括分词、过滤等操作。 示例配置: ```json { "analysis": { "pipelines": { "my_pipeline": { "description": "A pipeline to apply different tokenizers", "processors": [ {"tokenizer": "standard"}, {"tokenizer": "ik_max_word"} ] } } } } ``` 文档可以通过 `doc_values` 或者 `analyzer` 属性引用这个管道。 记住,同时使用多个分词器可能会增加解析和存储的成本,所以要确保选择的策略能够优化性能并满足实际需求。对于具体应用场景,可能还需要调整参数或优化配置。有任何疑问,你可以继续提问关于Elasticsearch的配置或者分词策略
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值