索引的配置項按是否可以更改分爲static屬性與動態配置,所謂的靜態配置即索引創建後不能修改。
1、索引靜態配置
- index.number_of_shards
索引分片的數量。在ES層面可以通過es.index.max_number_of_shards屬性設置索引最大的分片數,默認爲1024,index.number_of_shards的默認值爲Math.min(es.index.max_number_of_shards,5),故通常默認值爲5。 - index.shard.check_on_startup
分片在打開之前是否應該檢查該分片是否損壞。當檢測到損壞時,它將阻止分片被打開。可選值:false:不檢測;checksum:只檢查物理結構;true:檢查物理和邏輯損壞,相對比較耗CPU;fix:類同與false,7.0版本後將廢棄。默認值:false。 - index.codec
數據存儲的壓縮算法,默認值爲LZ4,可選擇值best_compression ,比LZ4可以獲得更好的壓縮比(即佔據較小的磁盤空間,但存儲性能比LZ4低)。 - index.routing_partition_size
路由分區數,如果設置了該參數,其路由算法爲: (hash(_routing) + hash(_id) % index.routing_parttion_size ) % number_of_shards。如果該值不設置,則路由算法爲 hash(_routing) % number_of_shardings,_routing默認值爲_id。
2、索引動態配置
- index.number_of_replicas
索引複製分片的個數,默認值1,該值必須大於等於0,索引創建後該值可以變更。 - index.auto_expand_replicas
副本數是否自動擴展,可設置(e.g0-5)或(0-all)。 - index.refresh_interval
執行刷新操作的頻率,該操作使對索引的最新更改對搜索可見。默認爲1s。可以設置爲-1以禁用刷新。 - index.max_result_window
控制分頁搜索總記錄數,from + size的大小不能超過該值,默認爲10000。 - index.max_inner_result_window
從from+ size的最大值,用於控制top aggregations,默認爲100。內部命中和頂部命中聚合佔用堆內存,並且時間與 from + size成正比,這限制了內存。 - index.max_rescore_window
在rescore的搜索中,rescore請求的window_size的最大值。 - index.max_docvalue_fields_search
一次查詢最多包含開啓doc_values字段的個數,默認爲100。 - index.max_script_fields
查詢中允許的最大script_fields數量。默認爲32。 - index.max_ngram_diff
NGramTokenizer和NGramTokenFilter的min_gram和max_gram之間允許的最大差異。默認爲1。 - index.max_shingle_diff
對於ShingleTokenFilter, max_shingle_size和min_shingle_size之間允許的最大差異。默認爲3。 - index.blocks.read_only
索引數據、索引元數據是否只讀,如果設置爲true,則不能修改索引數據,也不能修改索引元數據。 - index.blocks.read_only_allow_delete
與index.blocks.read_only基本類似,唯一的區別是允許刪除動作。 - index.blocks.read
設置爲true以禁用對索引數據的讀取操作。 - index.blocks.write
設置爲true以禁用對索引數據的寫操作。(針對索引數據,而不是索引元數據) - index.blocks.metadata
設置爲true,表示不允許對索引元數據進行讀與寫。 - index.max_refresh_listeners
索引的每個分片上當刷新索引時最大的可用監聽器數量。這些偵聽器用於實現refresh=wait_for。 - index.highlight.max_analyzed_offset
高亮顯示請求分析的最大字符數。此設置僅適用於在沒有偏移量或term vectors的文本字段時。默認情況下,該設置在6中未設置。x,默認值爲-1。 - index.max_terms_count
可以在terms查詢中使用的術語的最大數量。默認爲65536。 - index.routing.allocation.enable
Allocation機制,其主要解決的是如何將索引在ES集羣中在哪些節點上分配分片(例如在Node1是創建的主分片,在其他節點上創建複製分片)。
舉個例子,如果集羣中新增加了一個節點,集羣的節點由原來的3個變成了4
可選值:
1. all
所有類型的分片都可以重新分配,默認。
2. primaries
只允許分配主分片。
3. new_primaries
只允許分配新創建的主分片。
4. none
所有的分片都不允許分配。
-
index.routing.rebalance.enable
索引的分片重新平衡機制。可選值如下:- all
默認值,允許對所有分片進行再平衡。 - primaries
只允許對主分片進行再平衡。 - replicas
只允許對複製分片進行再平衡。 - none
不允許對任何分片進行再平衡
- all
- index.gc_deletes
文檔刪除後(刪除後版本號)還可以存活的週期,默認爲60s。 - index.max_regex_length
用於正在表達式查詢(regex query)正在表達式長度,默認爲1000。 - index.default_pipeline
默認的管道聚合器。
3、Analysis
分析模塊相關配置參數,該部分中已在 字段類型映射(mapping中詳細介紹
4、Index Shard Allocation
索引分片分配相關參數。這部分內容將在Cluster(集羣模塊詳細介紹)。
5、Mapping
字段映射相關參數,詳情請參考: Elasticsearch Mapping parameters(主要參數一覽)
6、Merging
後臺分片合併進程相關配置參數。
- index.merge.scheduler.max_thread_count
用於單個分片節點合併的最大線程數量,默認值爲:Math.max(1, Math.min(4, Runtime.getRuntime().availableProcessors() / 2)),如果是非SSD盤,該值建議設置爲1。
7、Similarities
相似性相關配置,這個後續可能會以專題介紹,暫不深究。
8、Show Log
慢查詢日誌相關配置。
8.1 Search Show Log
首先ES提供在查詢階段(query)和數據獲取階段(fetch)設置闊值,超過該闊值則記錄日誌。
支持如下參數:
index.search.slowlog.threshold.query.warn: 10s
index.search.slowlog.threshold.query.info: 5s
index.search.slowlog.threshold.query.debug: 2s
index.search.slowlog.threshold.query.trace: 500ms
上述參數定義查詢階段的闊值,分別表示,如果執行時間超過10s,打出警告日誌,超過5s輸出info級別日誌。
index.search.slowlog.threshold.fetch.warn: 1s
index.search.slowlog.threshold.fetch.info: 800ms
index.search.slowlog.threshold.fetch.debug: 500ms
index.search.slowlog.threshold.fetch.trace: 200ms
上述參數定義查詢獲取數據(fetch)的闊值,分別表示,如果執行時間超過1s,打出警告日誌,超過800ms輸出info級別日誌。
index.search.slowlog.level: info
定義日誌輸出級別爲info,也就是hdebug,trace級別的日誌不輸出。
注意:上述日誌級別爲分片級日誌。
上述參數定義了日誌輸出級別,那接下來還需要在log4j文件中定義日誌輸出器,日誌輸出文件路徑等,其相關配置如下:
appender.index_search_slowlog_rolling.type = RollingFile
appender.index_search_slowlog_rolling.name = index_search_slowlog_rolling
appender.index_search_slowlog_rolling.fileName = ${sys:es.logs}_index_search_slowlog.log
appender.index_search_slowlog_rolling.layout.type = PatternLayout
appender.index_search_slowlog_rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c] [%node_name]%marker %.10000m%n
appender.index_search_slowlog_rolling.filePattern = ${sys:es.logs}_index_search_slowlog-%d{yyyy-MM-dd}.log
appender.index_search_slowlog_rolling.policies.type = Policies // 文件切割方案,屬於log4j的語法
appender.index_search_slowlog_rolling.policies.time.type = TimeBasedTriggeringPolicy // 基於時間切割,log4j還支持按大小切割,其類爲SizeBasedTriggeringPolicy。
appender.index_search_slowlog_rolling.policies.time.interval = 1 // 1小時切割成一個文件
appender.index_search_slowlog_rolling.policies.time.modulate = true // 是否修正時間範圍, 如果設置爲true,則從0時開始計數
logger.index_search_slowlog_rolling.name = index.search.slowlog
logger.index_search_slowlog_rolling.level = trace
logger.index_search_slowlog_rolling.appenderRef.index_search_slowlog_rolling.ref = index_search_slowlog_rolling
logger.index_search_slowlog_rolling.additivity = false
8.2 Index Show Log
索引慢日誌。
index.indexing.slowlog.threshold.index.warn: 10s
index.indexing.slowlog.threshold.index.info: 5s
index.indexing.slowlog.threshold.index.debug: 2s
index.indexing.slowlog.threshold.index.trace: 500ms
index.indexing.slowlog.level: info
index.indexing.slowlog.source: 1000
index.indexing.slowlog.source參數用來控制記錄文檔_souce字段字符的個數,默認爲1000,表示只記錄_souce字段的前1000個字符,可以設置true,表示輸出_souce字段全部內容,設置爲false,表示不記錄_souce字段的內容。
默認情況下,會對_souce字段的輸出進行格式化,通常使用一行輸出,如果想阻止格式化,可以通過index.indexing.slowlog.reformat設置爲false來避免。
同樣通過上述屬性定義好闊值,接下來將在logg4j配置文件中定義日誌的輸出。
appender.index_indexing_slowlog_rolling.type = RollingFile
appender.index_indexing_slowlog_rolling.name = index_indexing_slowlog_rolling
appender.index_indexing_slowlog_rolling.fileName = ${sys:es.logs}_index_indexing_slowlog.log
appender.index_indexing_slowlog_rolling.layout.type = PatternLayout
appender.index_indexing_slowlog_rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c] [%node_name]%marker %.-10000m%n
appender.index_indexing_slowlog_rolling.filePattern = ${sys:es.logs}_index_indexing_slowlog-%d{yyyy-MM-dd}.log
appender.index_indexing_slowlog_rolling.policies.type = Policies
appender.index_indexing_slowlog_rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.index_indexing_slowlog_rolling.policies.time.interval = 1
appender.index_indexing_slowlog_rolling.policies.time.modulate = true
logger.index_indexing_slowlog.name = index.indexing.slowlog.index
logger.index_indexing_slowlog.level = trace
logger.index_indexing_slowlog.appenderRef.index_indexing_slowlog_rolling.ref = index_indexing_slowlog_rolling
logger.index_indexing_slowlog.additivity = false
9、store
存儲模塊,其主要參數爲:index.store.type,表示存儲類型,該參數爲靜態參數,在索引創建時指定,無法更改。
其可選值:
- fs
默認文件系統實現,根據當前操作系統選擇最佳存儲方式。 - simplefs
簡單的FS類型,使用隨機訪問文件實現文件系統存儲(映射到Lucene SimpleFsDirectory)。併發性能很差(多線程會出現瓶頸)。當需要索引持久性時,通常最好使用niofs。 - niofs
基於NIOS實現的文件系統,該類型使用NIO在文件系統上存儲碎片索引(映射到Lucene NIOFSDirectory)。它允許多個線程同時從同一個文件中讀取數據。 - mmapfs
基於文件內存映射機制實現的文件系統實現,該方式將文件映射到內存(MMap)來存儲文件系統上的碎片索引(映射到Lucene MMapDirectory)。內存映射使用進程中與被映射文件大小相同的部分虛擬內存地址空間。
可以通過node.store.allow_mmapfs屬性來禁用基於內存映射機制,如果節點所在的操作系統沒有大量的虛擬內存,則可以使用該屬性明確禁止使用該文件實現。
10、Translog
由於Lucene提交的開銷太大,不能每個單獨變更就提交(刷寫到磁盤),所以每個分片複製都有一個事務日誌,稱爲translog。所有索引(index)和刪除(delete)操作都是在被內部Lucene索引處理之後(但在它們被確認之前[返回客戶端])寫入translog的。在發生崩潰的情況下,當分片恢復時,可以從translog中恢復最近已確認但尚未包含在上一次Lucene提交中的事務。
Translog日誌有點類似於關係型數據庫mysql的redo日誌。
Translog相關配置參數(索引級別):
- index.translog.durability
translog刷盤方式,可選值:request、async。request,即每請求一次刷盤,也就是客戶端發起一個增刪改操作時,會在主分片與複製分片全部刷盤成功後,纔會返回成功,是ES的默認模式。async:異步刷盤模式,此模式刷盤頻率由index.translog.sync_interval設置,其默認值爲5s,該模式會存在數據丟失的可能。 - index.translog.sync_interval
如果index.translog.durability設置爲async,用該值來設置刷盤的頻率,默認爲5s。 - index.translog.flush_threshold_size
es強制刷新的另外一個維度,如果translog的大小達到該值,則強制將未刷盤的數據強制刷新到Lucene中(類比一下關係型數據庫的數據文件),默認512mb。 - index.translog.retention.size
保存跨日誌文件的總大小。也就是一translog日誌文件flush後,並不馬上刪除,而是保留一段時間,但最新的translog文件已存儲的內容與待刪除的文件的間隔不超過該參數設置的值,默認爲512M。 - index.translog.retention.age
保存translog文件的最大持續時間,默認爲12 h。
關於ES的配置屬性就先介紹到這裏,後續還會對Analysis、Index Shard Allocation、Similarities這三個模塊進行更加詳細的說明。
原文發佈時間爲:2019-04-20
本文作者:丁威,《RocketMQ技術內幕》作者。
本文來自中間件興趣圈,瞭解相關信息可以關注中間件興趣圈。