Elasticsearch筆記——文件管理
一、新建文件
1、put: http://localhost:9200/<index>/<Type>/<Id>,傳的JSON是一條記錄Document
2、post:http://localhost:9200/<index>/<Type>,傳的JSON是一條記錄Document
二、獲取文件
get http://localhost:9200/<index>/<Type>/<Id>?pretty=true
pretty=表示返回的易讀的格式資料
如果Id不正確,就查不到資料,found欄位就是false,found欄位就是true表示查到了資料
根據id一次獲取多個文件 get http://localhost:9200/_mget { "docs": [ { "_index": "<index>", "_type": "<type>", "_id": "<id>" }, { "_index": "<index1>", "_type": "<type2>", "_id": "<id2>" } ] } 如果index是同一個: get http://localhost:9200/<index>/_mget { "docs": [ { "_type": "<type>", "_id": "<id>" }, { "_type": "<type2>", "_id": "<id2>" } ] } 如果index和type都是同一個: get http://localhost:9200/<index>/<type>/_mget { "docs": [ { "_id": "<id>" }, { "_id": "<id2>" } ] } 或 get http://localhost:9200/<index>/<type>/_mget { "ids": ["id1", "id2"] }
三、更新文件
put: http://localhost:9200/<index>/<Type>/<Id>,傳的JSON是一條記錄Document
查詢更新(對 title 中包含 git 關鍵字的文件增加一個 category 欄位): post http://localhost:9200/<index>/_update_by_query { "script": { "inline": "ctx._source.category = params.category", "lang": "painless", "params": {"category" : "git"} }, "query": { "term": {"title": "git"} } } 給指定的文件新增一個欄位: post http://localhost:9200/<index>/<type>/<id>/_update { "script": { "inline": "ctx._source.new_field = \"value_of_new_field\"" } } 給指定的文件移除一個欄位: post http://localhost:9200/<index>/<type>/<id>/_update { "script": { "inline": "ctx._source.remove(\"new_field\") } }
四、刪除文件
delete http://localhost:9200/<index>/<Type>/<Id>
如果在索引文件時指定了路由,刪除時也可以新增路由引數,delete http://localhost:9200/<index>/<Type>/<Id>?routing=user123
注意:如果執行刪除操作時路由值不正確,會導致文件刪除失敗;當對映的_routing被設定為required且沒有指定路由值時,執行刪除操作會丟擲路由缺失異常並拒絕該請求。
查詢刪除
刪除文件的 title 欄位中包含 hibernate 的所有文件:
post http://localhost:9200/<index>/_delete_by_query
{
"query": {
"term": {
"title": "hibernate"
}
}
}
刪除一個type下所有文件:
post http://localhost:9200/<index>/<type>/_delete_by_query
{
"query": {
"match_all": {}
}
}
五、批量操作
1、建立一個JSON檔案
2、檔案中寫入多個請求操作,請求格式如下:
action_and_meta_data\n
optional_source\n
......
action_and_meta_data\n
optional_source\n
3、執行操作
post http://localhost:9200/<index>/<type>/_bulk
說明:
請求資料的每一行的結尾處都必須有換行字元“\n”,最後一行也必須有。
action_and_meta_data 行指定將要在哪個文件中執行什麼操作,其中 action 必須是 index、create、update、或 delete, mete_data 需要指明被操作文件的 _index、_type 和 _id。
建立文件:
{ "index" : { "_index" : "<index>", "_type" : "<type>", "_id" : "<id>"}}
{ }
或
{ "create" : { "_index" : "<index>", "_type" : "<type>", "_id" : "<id>"}}
{ }
區別:如果文件已經錯在,使用create操作會失敗,但是index操作不會。
刪除文件:
{ "delete" : { "_index" : "<index>", "_type" : "<type>", "_id" : "<id>"}}
更新文件:
{ "update" : { "_index" : "<index>", "_type" : "<type>", "_id" : "<id>"}}
{ }
使用 Bulk 操作需要注意一次提交請求檔案的大小,整個批量請求需要被載入到接受請求節點的記憶體中,所以請求越大,留給其他請求的可用記憶體就會越小。
六、版本控制
Elasticsearch 更新文件的過程如下:首先,讀取源文件,對源文件進行更新操作,然後重新索引整個文件。所以,當多個執行緒同時修改一個文件時,就會發生衝突。
Elasticsearch 更新文件是可以通過控制 _version 欄位來避免上述衝突。
Elasticsearch 的文件版本控制機制主要有內部版本控制和外部版本控制,內部版本控制機制要求每次操作請求,只有當版本號相等時才可以操作成功,外部版本控制機制要求外部文件版本比內部文件版本高時才可以更新成功。
所以,我們每次進行更新操作時,只需要新增上要更新文件的版本號就可以避免多個執行緒同時操作文件引發的衝突。
put http://localhost:9200/<index>/<Type>/<Id>?version=_version
七、路由機制
Elasticsearch 的路由機制是通過雜湊演算法,將具有相同雜湊值的文件放到同一個主分片中。
shard = hash(routing) % number_of_primary_shards
routing 是一個任意字串,可以自定義,Elasticsearch 預設將文件的id值作為routing值。
Elasticsearch 在叢集中執行一次查詢的過程如下:
1、查詢請求首先被叢集中的一個節點接收;
2、接收到這個請求的節點,將這個查詢廣播到這個索引的每一個分片上;
3、每個分片執行完搜尋查詢並返回結果;
4、結果在通道節點上合併、排序並返回給使用者。
所以預設情況下,每次請求Elasticsearch不知道文件在哪個主分片上,需要將這個請求廣播到所有主分片上。如果採用自定義的路由模式,可以使查詢更具有目的性,可以提高查詢效率。
Elasticsearch 的index、get、delete等文件的API都可以接受一個 routing 引數,以索引文件為例,執行 index 操作時給文件設定一個 routing 引數,具有相同 routing 的文件會被分配到同一個分片上。
put http://localhost:9200/<index>/<Type>/<Id>?routing=<routing>
當需要查詢查詢文件時,可以指定routing引數,避免向所有分片傳送查詢求情,大大減少系統的資源。
get http://localhost:9200/<index>/<Type>/_search?routing=<routing>
查詢時可以指定多個路由值,路由值之間使用逗號隔開。