近期,幾個典型 Elasticsearch 8.X 問題及方案探討
問題1:max_expansions 設置大了會報錯,什么原因?
大佬們問個問題,我在使用match_phrase_prefix時候,設置了一個比較大的max_expansions,比如10000。
這個時候會報錯:too_many_clauses: maxClauseCount is set to 1024。我搜了下 maxClauseCount 這是控制搜索條件數量的,但我這只是改了個 max_expansions 就這樣了,這2者有什么聯系呀?
根本原因:
如果你設置了一個很高的值( 10000),Elasticsearch 會嘗試生成所有可能的匹配項,直到達到這個限制。
而進行 match_phrase_prefix 查詢時,每個可能的匹配項都會被視為一個子句。
如果生成的匹配項數量超過 maxClauseCount 的限制,就會出現 too_many_clauses 錯誤。
可行的解決方案:
選擇一個更合理的 max_expansions 值,以保持生成的查詢子句數量在 maxClauseCount 的限制范圍內。
問題2:集群數據遷移能不能直接拷貝文件?
各位大佬,同版本的es集群間數據遷移,假設兩個集群節點數相同,是不是可以通過直接拷貝數據文件來進行啊?
去年年底咱們就討論過:臘月27日凌晨的一個緊急 Elasticsearch 線上問題復盤
一句話,非必要不要直接拷貝文件。
官方文檔在集群備份部分有過強調如下:
you cannot back up an Elasticsearch cluster by making copies of the data directories of its nodes. There are no supported methods to restore any data from a filesystem-level backup. If you try to restore a cluster from such a backup, it may fail with reports of corruption or missing files or other data inconsistencies, or it may appear to have succeeded having silently lost some of your data.
中文釋義:
- 你無法通過復制其節點的數據目錄來備份 Elasticsearch 集群。
- 不支持從文件系統級備份恢復任何數據的方法。
- 如果你嘗試從此類備份恢復集群,則可能會失敗,并報告損壞或丟失文件或其他數據不一致的情況,或者可能看似已成功,但悄無聲息地丟失了一些數據。
https://discuss.elastic.co/t/why-are-we-told-to-copy-the-data-folder-when-upgrading/168951
https://www.elastic.co/guide/en/elasticsearch/reference/current/snapshot-restore.html
問題3:全局超時如何設置?
除了每個search可以配置自己的timeout,集群有沒有參數可以配置一個統一的timeout,針對所有search都生效額?
建議:看看這個參數: search.default_search_timeout。
To set a cluster-wide default timeout for all search requests, configure search.default_search_timeout using the cluster settings API. This global timeout duration is used if no timeout argument is passed in the request. If the global search timeout expires before the search request finishes, the request is cancelled using task cancellation. The search.default_search_timeout setting defaults to -1 (no timeout).
集群層面設置解決方案如下:
圖片
PUT /_cluster/settings
{
"persistent": {
"search.default_search_timeout": "30s"
}
}
要為所有搜索請求設置集群范圍內的默認超時時間,可以使用集群設置 API 配置 search.default_search_timeout。
如果請求中沒有傳遞超時參數,則使用這個全局超時持續時間。
如果全局搜索超時在搜索請求完成之前到期,請求將通過任務取消被取消。
search.default_search_timeout 設置的默認值為 -1(無超時)。
https://www.elastic.co/guide/en/elasticsearch/reference/8.12/search-your-data.html#search-timeout
問題4:自定義ID如何自動設置為 MD5呢?
銘毅老師你好,想請問一下,往es索引里面插入文檔分為指定文檔id和自動生成文檔id,目前有一個需求在插入文檔的時候,將文檔的id值取插入的文檔中的一個字段。
比如我插入的是一個關于文件的相關信息的文檔,字段有md5值,大小,文件類型等等信息,此時我希望插入的這個文檔的ID是這個文件的md5值, 不太明白怎么設置這樣的關系,謝謝!
題目來源:https://t.zsxq.com/16mobA3PV
實踐參考:
Elasticsearch “指紋”去重機制,你實踐中用到了嗎?
方案:
使用 fingerprint 預處理器,借助已有的多個字段構建 MD5值,然后將目標字段設置為 _id 即可。
圖片
PUT _ingest/pipeline/id-fingerprint
{
"processors": [
{
"fingerprint": {
"fields": ["file_name", "file_size"],
"target_field": "_id",
"method": "MD5"
}
}
]
}
DELETE test_01128
PUT test_01128
{
"settings": {
"default_pipeline": "id-fingerprint"
},
"mappings": {
"properties": {
"file_name": {
"type": "keyword"
},
"id-fingerprint": {
"type": "keyword"
}
}
}
}
POST test_01128/_bulk
{"index":{}}
{"file_name":"abc","file_size":"3kb"}
{"index":{}}
{"file_name":"bcd","file_size":"1kb"}
{"index":{}}
{"file_name":"abc","file_size":"3kb"}
小結
以上都是實戰環境遇到的典型問題,如果你也有類似問題,歡迎發出來,咱們一起討論解決!