Elasticsearch创建无text类型的索引,只包含keyword

本文详细解析了企业数据映射的结构,包括日期类型、关键字类型及长整型等属性,探讨了如何在Elasticsearch中高效存储和检索公司信息。

PUT /es_company_3
{
    "mappings": {
        "company_3": {
            "properties": {
            "approvedtime" : {
            "type" : "date"
          },
          "base" : {
            "type" : "keyword"
          },
          "crawledtime" : {
            "type" : "date"
          },
          "createtime" : {
            "type" : "long"
          },
          "entname" : {
            "type" : "keyword"
          },
          "enttype" : {
            "type" : "keyword"
          },
          "enttypeno" : {
            "type" : "long"
          },
          "estime" : {
            "type" : "date"
          },
          "flag" : {
            "type" : "long"
          },
          "frid" : {
            "type" : "long"
          },
          "frname" : {
            "type" : "keyword"
          },
          "fromtime" : {
            "type" : "date"
          },
          "frtype" : {
            "type" : "long"
          },
          "id" : {
            "type" : "long"
          },
          "listcode" : {
            "type" : "keyword"
          },
          "namesuffix" : {
            "type" : "keyword"
          },
          "opscope" : {
            "type" : "keyword"
          },
          "orgapprovedinstitute" : {
            "type" : "keyword"
          },
          "orgno" : {
            "type" : "keyword"
          },
          "ownershipstake" : {
            "type" : "keyword"
          },
          "parentid" : {
            "type" : "long"
          },
          "property1" : {
            "type" : "keyword"
          },
          "property2" : {
            "type" : "keyword"
          },
          "property3" : {
            "type" : "keyword"
          },
          "property4" : {
            "type" : "keyword"
          },
          "property5" : {
            "type" : "keyword"
          },
          "regactlcap" : {
            "type" : "keyword"
          },
          "regaddress" : {
            "type" : "keyword"
          },
          "regcap" : {
            "type" : "keyword"
          },
          "regno" : {
            "type" : "keyword"
          },
          "regorg" : {
            "type" : "keyword"
          },
          "regstatus" : {
            "type" : "keyword"
          },
          "sourceflag" : {
            "type" : "keyword"
          },
          "sourceupdatetime" : {
            "type" : "date"
          },
          "totime" : {
            "type" : "date"
          }
            }
        }
    }
}

 

<think> 嗯,用户想了解如何用Postman将Elasticsearch索引中的字段类型keyword改为text。这个问题挺实际的,因为ES的字段类型一旦设定就不能直接修改,需要重建索引。 用户可能正在处理一个ES索引的优化需求,比如发现某个keyword字段现在需要全文搜索功能。ta引用了不少关于索引创建和映射修改的资料,说明已经做过一些尝试。不过从引用内容看,用户似乎混淆了索引创建和映射更新的区别——那些示例都是创建索引或定义初始映射的。 需要特别注意的技术要点是:ES不允许原地修改字段类型,必须走重建索引流程。这个方案虽然步骤多但很稳妥。用户可能需要知道具体操作细节,比如如何用reindex API,以及新旧索引切换的注意事项。 考虑到用户使用Postman操作,应该给出完整的HTTP请求示例。对于"商品名称"这个例子,新索引的映射要明确指定text类型并配置合适的分词器(如ik_max_word),这样比单纯讲理论更有帮助。 用户可能还会关心数据迁移的完整性,所以要在注意事项里强调检查文档数量。另外字段冲突的问题也很常见——比如旧索引有多个字段类型定义不一致,reindex可能报错,这点也要提前说明。 最后生成的相关问题覆盖了用户可能遇到的延伸需求:如何修改其他数据类型(比如textkeyword)、修改多个字段的技巧,以及更深入的类型转换场景。特别是动态模板的自动映射功能,对字段多的索引特别实用。 </think> 在Elasticsearch中,**字段类型一旦创建就无法直接修改**(如从`keyword`改为`text`)。必须通过重建索引的方式实现类型变更。以下是使用Postman操作的完整步骤: --- ### **操作步骤** #### 1. **创建索引(带修正后的映射)** ```http PUT /new_index { "mappings": { "properties": { "your_field": { # 需修改的字段名 "type": "text", # 改为text类型 "analyzer": "ik_max_word", # 中文分词器(可选) "search_analyzer": "ik_smart" }, ... # 其他字段保持不变 } } } ``` [^1] #### 2. **迁移旧索引数据到新索引** ```http POST /_reindex { "source": { "index": "old_index" # 原索引名 }, "dest": { "index": "new_index" # 新索引名 } } ``` [^3] #### 3. **验证数据完整性** ```http GET /new_index/_count # 检查文档数量是否一致 GET /new_index/_search # 抽查字段是否修改成功 ``` #### 4. **切换别名(无缝迁移)** ```http POST /_aliases { "actions": [ { "remove": { # 删除旧索引别名 "index": "old_index", "alias": "current_data" } }, { "add": { # 将别名指向新索引 "index": "new_index", "alias": "current_data" } } ] } ``` #### 5. **删除旧索引(可选)** ```http DELETE /old_index ``` --- ### **关键注意事项** 1. **字段冲突处理** - 若新旧映射存在类型冲突,需在`reindex`前清理冲突数据或使用`script`转换类型: ```json "script": { "source": "ctx._source.your_field = ctx._source.your_field.toString()" } ``` 2. **业务中断规避** - 通过别名切换保证应用无感知(步骤4)。 - 在低峰期执行`_reindex`,避免性能影响。 3. **分词器配置** - 从`keyword`改为`text`后,需显式指定分词器(如中文场景的`ik_max_word`)[^1][^4]。 --- ### **示例场景** **需求**:将索引`products`中的`product_name`字段从`keyword`改为`text`: ```http PUT /products_v2 { "mappings": { "properties": { "product_name": { "type": "text", "analyzer": "ik_max_word" } } } } POST /_reindex { "source": {"index": "products"}, "dest": {"index": "products_v2"} } ``` > ⚠️ **重要**:操作前备份数据并测试!直接修改现有字段类型会导致索引损坏[^2][^4]。 --- ### **相关问题** 1. **如何将`text`类型字段改为`keyword`类型?** > 同样需重建索引,在新映射中定义类型为`keyword`,迁移注意数据格式匹配(如避免长文本)。 2. **能否一次性修改多个字段类型?** > 可以!在新索引的`mappings`中同修改所有目标字段类型,通过`_reindex`批量迁移。 3. **修改嵌套对象(nested)字段类型有何特殊步骤?** > 需在`mappings`中显式声明`"type": "nested"`,并通过`script`处理嵌套结构的数据迁移。 4. **如何动态避免字段类型错误?** > 使用动态模板(dynamic templates)强制指定字段类型,例如将所有字符串字段默认设为`keyword`[^4]: ```json "mappings": { "dynamic_templates": [ { "strings_as_keywords": { "match_mapping_type": "string", "mapping": { "type": "keyword" } } } ] } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值