我將超過 3800 萬個檔案(文本字串)加載到本地機器上的 Elasticsearch 索引中。我想計算每個字串的長度并將該值添加為索引中的元資料。
在將檔案加載到 Elasticsearch 之前,我是否應該將字串長度計算為元資料?或者,我可以在事后使用計算值更新元資料嗎?
我對 Elasticsearch/Kibana 比較陌生,這些問題是由于以下 Python 實驗引起的:
資料作為字串串列
mylist = ['string_1', 'string_2',..., 'string_N'] L = [len(s) for s in mylist] # this computation takes about 1 minute on my machine選項 1 的缺點是我沒有利用 Elasticsearch 并且“mylist”占用了大量記憶體。
資料作為 Elasticsearch 索引,其中“mylist”中的每個字串都加載到“text”欄位中。
from haystack.document_store.elasticsearch import ElasticsearchDocumentStore document_store = ElasticsearchDocumentStore(host='localhost', username='', password='', index='myindex') docs = document_store.get_all_documents_generator() L = [len(d.text) for d in docs] # this computation takes about 6 minutes on my machine選項 2 的缺點是計算時間要長得多。好處是 generator() 釋放了記憶體。長計算時間是為什么我認為將字串長度(和其他分析)作為元資料存盤在 Elasticsearch 中將是一個很好的解決方案。
還有其他我應該考慮的選擇嗎?我錯過了什么?
uj5u.com熱心網友回復:
如果你想存盤整個檔案的大小,我建議安裝mapper-size插件,它將在_size欄位中存盤源檔案的大小。
如果您只想存盤源檔案特定欄位的大小,則需要以不同的方式進行。
我的建議是創建一個攝取管道,在每個檔案被索引之前處理它。然后可以在第一次索引檔案時或加載檔案后使用該攝取管道。我會告訴你怎么做。
首先,使用script處理器創建攝取管道,該處理器將text在另一個名為的欄位中的欄位中存盤字串的大小textLength。
PUT _ingest/pipeline/string-length
{
"description": "My optional pipeline description",
"processors": [
{
"script": {
"source": "ctx.textLength = ctx.text.length()"
}
}
]
}
因此,如果您已經將檔案加載到 Elasticsearch 中并希望使用其中一個欄位的長度來豐富每個檔案,您可以事后使用Update by Query API 來完成,如下所示:
POST myindex/_update_by_query?pipeline=string-length&wait_for_completion=false
當檔案第一次被索引時,也可以在索引時利用該攝取管道,只需在索引查詢中參考管道,如下所示:
PUT myindex/_doc/123?pipeline=string-length
兩種選擇都適用,嘗試一下并選擇最適合您需求的選擇。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/357702.html
