原创

ElasticSearc-ES-添加字段-重建索引

原来的mapping,有数据,但是mapping无字段时查不到,单独使用_mapping添加字段后也不行,所以需要重建索引

 GET /safety_law/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "firstClass": "安全法规"
                    }
                }
                ,
                {
                    "term": {
                        "secondClass": "国家法律"
                    }
                }
                 ,
                {
                    "term": {
                        "regulatoryStatus": "现行"
                    }
                }
            ]
        }
    }
}

GET /safety_law-v1/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "firstClass": "安全法规"
                    }
                }
                ,
                {
                    "term": {
                        "secondClass": "国家法律"
                    }
                }
                 ,
                {
                    "term": {
                        "regulatoryStatus": "现行"
                    }
                }
            ]
        }
    }
}

查结构

GET /safety_law

单独增加mapping字段

PUT /safety_law/_mapping
{
  "properties": {
        "regulatoryStatus": {
            "type": "keyword"
        }
    }
}

第0步【感觉非必须】

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "safety_law",
        "alias": "safety_law-alias"
      }
    }
  ]
}

第1步新建索引

#safety_law (单独添加字段后可以执行 GET /safety_law,也可以执行后手动添加新字段 )

删除 "aliases" : { }, 和 上层大{}括号

settings 下保留分片信息

PUT /safety_law-v1
{

    "mappings" : {
      "dynamic" : "false",
      "date_detection" : false,
      "properties" : {
        "assessmentNums" : {
          "type" : "integer"
        },
        "attachment" : {
          "type" : "text"
        },
        "carryOutTime" : {
          "type" : "text",
          "fields" : {
            "raw" : {
              "type" : "keyword"
            }
          },
          "analyzer" : "ik_max_word",
          "search_analyzer" : "ik_smart"
        },
        "content" : {
          "type" : "text",
          "analyzer" : "ik_max_word",
          "search_analyzer" : "ik_smart"
        },
        "createTime" : {
          "type" : "date",
          "format" : "yyyy-MM-dd HH:mm:ss"
        },
        "downloadNums" : {
          "type" : "integer"
        },
        "firstClass" : {
          "type" : "keyword"
        },
        "issueNumber" : {
          "type" : "text",
          "analyzer" : "ik_max_word",
          "search_analyzer" : "ik_smart"
        },
        "issueTime" : {
          "type" : "text",
          "fields" : {
            "raw" : {
              "type" : "keyword"
            }
          },
          "analyzer" : "ik_max_word"
        },
        "publisher" : {
          "type" : "text",
          "analyzer" : "ik_max_word",
          "search_analyzer" : "ik_smart"
        },
        "regulatoryStatus" : {
          "type" : "keyword"
        },
        "secondClass" : {
          "type" : "keyword"
        },
        "thirdClass" : {
          "type" : "keyword"
        },
        "title" : {
          "type" : "text",
          "analyzer" : "ik_max_word",
          "search_analyzer" : "ik_smart"
        },
        "updateTime" : {
          "type" : "text",
          "fields" : {
            "raw" : {
              "type" : "keyword"
            }
          },
          "analyzer" : "ik_max_word"
        }
      }
    },
    "settings" : {
      "index" : {

        "number_of_shards" : "1",
        "number_of_replicas" : "1"

      }
    }

}

第2步查看新的mapping

GET /safety_law-v1/_mapping

第3步重建索引

POST /_reindex
{
   "source": {
      "index": "safety_law"
   },
   "dest": {
      "index": "safety_law-v1"
   }
}

第4步查看进度【必须等待,如果有返回nodes节点,表示还是重建中】

GET _tasks?detailed=true&actions=*reindex&human

第5步删除原索引【务必等待第4步完成】

DELETE /safety_law

第6步给新索引设置原索引的别名(为了不改代码)

或者使用步骤0的修改方式?

POST safety_law-v1/_alias/safety_law

如果改成代码调用,关键步骤 13456, 第4步查进度也很关键。

https://blog.csdn.net/weixin_40126236/article/details/119740415 变更字段需要重建索引 https://blog.csdn.net/dl674756321/article/details/120251127 这个在删除老索引后,为了不影响代码使用,再给老的索引使用别名

正文到此结束
本文目录