解決ElasticSearch5.x中@Field注解之IK分詞不能用的問題


一、概述

環境:ElasticSearch版本5.6.3,SpringBoot 2.0.2.RELEASE,索引myIndex

問題描述:使用@Field注解給實體類指定ik分詞解析器(ik_smart/ik_max_word),測試分詞功能,發現並不能達到預期的效果,查看mapping,並沒有自動生成ik配置。

二、解決方案

由於ElasticSearch索引一旦建立,就無法動態修改其字段的映射類型,為了不影響線上的訪問,需要無縫切換到新的索引上。使用 ElasticSearch 提供的 reindex api 來遷移數據,創建新的索引

1. 創建新的索引

PUT /myIndex_v2

2. 設置新索引的mapping

PUT /myIndex_v2/_mapping/myIndex_v2

{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "ik_smart",
      "search_analyzer": "ik_smart",
      "fields": {
        "keyword": {
        "type": "keyword",
        "ignore_above": 256
        }
      }
    },
    "content": {
      "type": "text",
      "fields": {
        "keyword": {
        "type": "keyword",
        "ignore_above": 256
        }
      }
    },
    "createTime": {
      "type": "long"
    }
  }
}

  

3. 同步數據

使用 reindex 將原來的索引重建到新的索引上

POST /_reindex

{
    "source": {
        "index": "myIndex"
    },
    "dest": {
        "index": "myIndex_v2"
    }
}        

  

4. 查看數據是否已同步到新的索引上

GET /myIndex_v2/_search

5. 使用別名,切換索引(同時刪除原索引myIndex)

POST /_aliases

{
    "actions": [
        {
            "add": {
            "index": "myIndex_v2",
            "alias": "myIndex"
            }
        },
        {
            "remove_index": {
            "index": "myIndex"
            }
        }
    ]
}                    

  大功告成,現在可以同時使用myIndex和myIndex_v2搜索數據

參考:https://javasgl.github.io/elastic-search-reindex/

   https://javasgl.github.io/use-alias-migrate-index/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM