es之索引配置

在es中有很多配置參數,有些配置是可以在建好索引後重新配置的,比如索引的副本數量、索引的分詞等
1.更新索引配置
請求:PUT http://127.0.0.1:9200/secisland/_settings
參數:

{
	"index":{"number_of_replicas":4}
}

更新分詞器。添加索引分析器之前必須先關閉索引,添加之後再打開索引

POST http://127.0.0.1:9200/secisland/_close
PUT http://127.0.0.1:9200/secisland/_settings

參數:

{
	"analysis":{
		"analyzer":{
			"content":{"type":"custom","tokenizer":"whitespace"}
		}
	}
}
POST http://127.0.0.1:9200/secisland/_open

2.獲取配置

GET http://127.0.0.1:9200/secisland/_settings

過濾配置參數的返回結果:

GET http://127.0.0.1:9200/secisland/_settings/name=index.number_*

name=index.number_*設置將只返回number_of_replicas,number_of_shards兩個參數詳情
3.索引分析
首先,把一個文本塊分析成一個個單獨的詞,爲後面的倒排索引做準備
一個分析器是由以下三個功能組合而成:

  • 字符過濾器(character filter):字符串經過字符過濾器處理,他們的工作是標記化之前處理字符串。字符過濾器能夠去除HTML標記,或者轉換"&“爲"and”。
  • 分詞器(tokenizer):分詞器被標記化成獨立的詞。一個簡單的分詞器可以根據空格或逗號將單詞分開
  • 標記過濾器(token filters):每個詞都通過所有標記過濾處理,它可以轉小寫,去掉詞,增加詞
    自定義分析器:
    請求POST 127.0.0.1:9200/_analyze
{
	"tokenizer":"keyword",
	"token_filters":["lowercase"],
	"char_filters":["html_strip"],
	"text":"this is a <b>test</b>"
}

如果想獲取分析器分析的更多細節,設置explain屬性爲true(默認是false),將輸出的分詞器的分詞詳情
請求POST http://127.0.0.1:9200/secisland_analyze
參數:

{
	"tokenizer":"standard",
	"token_filters":["snowball"],
	"text":"detailed output",
	"explain":true,
	"attributes":["keyword"]
}

4.索引模板

  1. 創建索引模板
    索引模板就是創建好一個索引參數設置(settings)和映射(mapping)的模板,再創建新索引的時候指定模板名稱就可以使用
    請求:PUT http://127.0.0.1:9200/_template/template_1
{
	"template":"te*",
	"settings":{"number_of_shards":1},
	"mappings":{"type1":{"_source":{"enabled":false}}}
}

可以用te*來適配,分片數量爲1,默認文檔類型是type1,_source的enabled爲false
也可以再模板中定義別名等其他屬性

2.刪除索引模板

DELETE http://127.0.0.1:9200/_template/template_1
  1. 獲取索引模板
GET http://127.0.0.1:9200/_template/template_1

使用通配符或者逗號分隔符可以獲取多個索引模板
獲取所有索引模板:

GET http://127.0.0.1:9200/_template/

判斷索引模板是否存在:

HEAD http://127.0.0.1:9200/_template/template_1
  1. 多個模板匹配存在的問題
    template1、template2兩個模板,使用te*會匹配2個模板,最後合併兩個模板的配置,如果配置重複,這時應該設置order屬性,order是從0開始的數字,先匹配小的,再匹配大的,如果屬性相同,後匹配的會覆蓋之前的配置
  2. 複製配置
    複製配置可以選擇索引數據保存在磁盤上的位置,也可以指如何在副本分片上進行操作
    充分利用index.data_path和index.shadow_replicas設置,可以使相同數據目錄爲多個實例服務,需在elasticsearch.yml中設置node.add_id_to_custom_path爲false
    elasticsearch.yml中的path.shared_data權限管理標明自定義索引的位置
    path.shared_data:/opt/data
    這意味着es可以讀寫path.shared_data設置中所有子目錄中的文檔
    可以在自定義數據路徑中創建索引,每個節點用這個路徑來存儲數據:
PUT http://127.0.0.1:9200/secisland
{
	"index":{"number_of_shards":1,"number_of_replicas":4,
	"data_path':"/opt/data/secisland","shadow_replicas":true
	}
}

index.shadow_replicas設置爲"true"不會在任何副本上重複文檔操作,反而會不停地刷新
index.data_path—字符型索引數據使用的路徑。es默認附加節點序號到路徑中,確保相同機器上的多重實例不會共享數據目錄。
index.shadow_replicas—布爾值,指示索引是否應該使用副本。默認false
index.shared_filesystem—布爾值,指示索引使用共享文件系統.如果index.shadow_replicas設置爲true,默認爲true,其他情況下默認爲false

  1. 重建索引
    重建索引的最基本功能是拷貝一個文件一個索引到另一個索引,例如:
請求:POST http://127.0.0.1:9200/_reindex
{
	"source":{"index":"secisland"},
	"dest":{"index":"new_secisland"}
}

返回值如下:

{
	"took":639,
	"updated":112,
	"batches":130,
	"version_conflicts":0,
	"failures":[],
	"created":12344
}

took:從開始到結束整個操作的毫秒數
updated:已成功更新的文檔數
created:成功創建的文檔數
batches:從創建索引拉回滾動響應的數量
version_conflicts:重建索引中版本衝突的數量
failures:所有索引失敗的數組。如果這是非空的,則請求將被中止
由於_reindex是源索引的快照,而且目標索引是不同的索引,所以基本不可能產生衝突。在接口參數中可以增加dest來進行樂觀併發控制。如果version_type設置爲internal會導致es盲目轉儲文件到目標索引,任何具有相同類型和ID的文檔將被重寫。例如

請求:POST http://127.0.0.1:9200/_reindex
{
	"source":{"index":"secisland"},
	"dest":{"index":"new_secisland","version_type":"internal"}
}

如果設置version_type爲external將會導致elasticsearch 保護源索引的版本,如果在目標索引中有一個比源索引就的版本,則會更新文檔。源文件中丟失的文檔也會在目標中創建
請求:

POST http://127.0.0.1:9200/_reindex
{
	"source":{"index":"secisland"},
	"dest":{"index":"new_secisland","version_type":"external"}
}

設置op_type爲create將導致_reindex在目標索引中僅創建丟失的文件,所有現有的文件將導致版本衝突
請求:

POST http://127.0.0.1:9200/_reindex
{
	"source":{"index":"secisland"},
	"dest":{"index":"new_secisland","op_type":"create"}
}

設置"conflicts":"proceed"可使_reindex發生衝突也可執行下去
請求:

POST http://127.0.0.1:9200/_reindex
{
	"conflicts":"proceed"
	"source":{"index":"secisland"},
	"dest":{"index":"new_secisland","version_type":"external"}
}

在源添加一個類型或者一個查詢可以限制文檔的數量,
例如只複製用戶名爲kimchy的文檔:
請求:

請求:

POST http://127.0.0.1:9200/_reindex
{
	"source":{
		"index":"secisland",
		"type":"secilog",
		"query":{"term":{"user":"kimchy"}
		}
	},
	"dest":{"index":"new_secisland"}
}

在請求接口中可列出多個源索引和類型
請求:
請求:

POST http://127.0.0.1:9200/_reindex
{
	"size":1  //可通過設置數量來限制處理文檔的數量
	"source":{"index":["secisland","blog"],"type":["secilog","post"],"sort":{"date":"desc"} //排序},
	"dest":{"index":"all_together"}
}

_reindex可以使用腳本來修改文檔,可修改的字段包括_id,_type,_index,_version,_routing,_parent,_timestamp,_ttl
請求:

POST http://127.0.0.1:9200/_reindex
{
	"source":{"index":"secisland"},
	"dest":{"index":"new_secisland","version_type":"external"}
	"script":{
		"internal":"if(ctx._source.foo == 'bar') {ctx.version++;ctx._source.remove('foo')}"
	}
}

_reindex可以設置文檔路由來進行路由保存,可以設置dest參數來進行路由設置:
keep:設置在匹配的每一個路由上發送請求的路由。這個是默認設置
discard:爲每一個匹配發送的請求設置爲空
=設置爲每一個匹配文本請求發送路由上的路由
URL後面可跟參數 _pretty之外,還支持_refresh,wait_for_completion,consistency,timeout
consistency 控制每次寫請求必須多少份分片被響應
timeout控制每個寫請求等待可用分片的時間
任務查看:
請求:

GET http://127.0.0.1:9200/_tasks/?pretty&detailed=true&actions=*reindex

可以通過_reindex來修改列的名稱,例如:
請求:

POST http://127.0.0.1:9200/_reindex?pretty
{
	"source":{"index":"secisland"},
	"dest":{"index":"secisland2"},
	"script":{"inline":"ctx._source.tag = ctx._source.remove(\"flag\")"}
}

flag字段名稱被修改成了tag

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章