簡體   English   中英

如何在elasticsearch中真正重新索引數據

[英]How to really reindex data in elasticsearch

我添加了新的映射(主要是現有字段的 not_analyzed 版本)我現在必須弄清楚如何重新索引現有數據。 我曾嘗試按照彈性搜索網站上的指南進行操作,但這太令人困惑了。 我也嘗試過使用插件(elasticsearch-reindex、allegro/elasticsearch-reindex-tool)。 我看過ElasticSearch - 以零停機時間重新索引您的數據,這是一個類似的問題。 我希望不必依賴外部工具(如果可能)並嘗試使用批量 API(與原始插入一樣)

我可以輕松地重建整個索引,因為它確實是只讀數據,但如果我想在生產中添加更多字段等,那么從長遠來看這不會真正起作用。 我想知道是否有人知道一個易於理解/遵循的解決方案或 ES 相對新手的步驟。 我使用的是版本 2 並使用 Windows。

重新索引意味着讀取數據,刪除elasticsearch中的數據並重新攝取數據。 沒有像“改變現有數據的映射到位”這樣的事情。 您提到的所有重新索引工具都只是圍繞 read->delete->ingest 的包裝。
您始終可以調整新索引的映射並稍后添加字段。 所有新字段都將根據此映射建立索引。 或者,如果您無法控制新字段,請使用動態映射。
查看在 Elasticsearch 中將字符串的默認映射更改為“未分析”以了解如何使用動態映射來獲取字符串的 not_analyzed 字段。

重新索引非常昂貴。 更好的方法是創建一個新索引並刪除舊索引。 要以零停機時間實現這一目標,請為所有客戶使用索引別名。 想想一個名為“data-version1”的索引。 分步驟:

  • 創建索引“data-version1”並給它一個名為“data”的別名
  • 僅在所有客戶端應用程序中使用別名“data”
  • 更新您的映射:創建一個名為“data-version2”的新索引(使用新映射)並將所有數據放入
  • 從 version1 切換到 version2:刪除 version1 上的別名“data”並在 version2 上創建別名“data”(或先創建,然后刪除)。 這兩個步驟之間的時間您的客戶將沒有(或雙倍)數據。 但是刪除和創建別名之間的時間應該很短,您的客戶不應該識別它。

始終使用別名是一種很好的做法。

在 2.3.4 版本中,可以使用新的 api _reindex 來執行它所說的操作。 基本用法是

{
    "source": {
        "index": "currentIndex"
    },
    "dest": {
        "index": "newIndex"
    }
}

Remote主機到Local主機的 Elasticsearch Reindex 示例(2020 年 1 月更新)

# show indices on this host
curl 'localhost:9200/_cat/indices?v'

# edit elasticsearch configuration file to allow remote indexing
sudo vi /etc/elasticsearch/elasticsearch.yml

## copy the line below somewhere in the file
>>>
# --- whitelist for remote indexing ---
reindex.remote.whitelist: my-remote-machine.my-domain.com:9200
<<<

# restart elaticsearch service
sudo systemctl restart elasticsearch

# run reindex from remote machine to copy the index named filebeat-2016.12.01
curl -H 'Content-Type: application/json' -X POST 127.0.0.1:9200/_reindex?pretty -d'{
  "source": {
    "remote": {
      "host": "http://my-remote-machine.my-domain.com:9200"
    },
    "index": "filebeat-2016.12.01"
  },
  "dest": {
    "index": "filebeat-2016.12.01"
  }
}'

# verify index has been copied
curl 'localhost:9200/_cat/indices?v'

我遇到了同樣的問題。 但是我找不到任何資源來更新當前的索引映射和分析器。 我的建議是使用滾動和掃描 api並使用新映射和新字段將數據重新索引到新索引。

如果你想像我一樣直接回答這個普遍的和基本的問題,這個問題一般來說彈性和社區都沒有得到很好的解決,這里是對我有用的代碼。

假設您只是在調試,而不是在生產環境中,添加或刪除字段是絕對合法的,因為您絕對不關心停機時間或延遲:

# First of all: enable blocks write to enable clonage
PUT /my_index/_settings
{
  "settings": {
    "index.blocks.write": true
  }
}

# clone index into a temporary index
POST /my_index/_clone/my_index-000001  

# Copy back all documents in the original index to force their reindexetion
POST /_reindex
{
  "source": {
    "index": "my_index-000001"
  },
  "dest": {
    "index": "my_index"
  }
}

# Disable blocks write
PUT /my_index/_settings
{
  "settings": {
    "index.blocks.write": false
  }
}

# Finaly delete the temporary index
DELETE my_index-000001

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM