ElasticSearch(7.2.2)-⽂檔的增刪改查

  • 2019 年 10 月 30 日
  • 筆記

版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。

本文鏈接:https://blog.csdn.net/weixin_42528266/article/details/102798546

簡介:⼿把⼿演示⽂檔的增刪改查

新增文檔
  • PUT localhost:9200/nba/_doc/1 (指定id)
{   "name":"哈登",   "team_name":"⽕箭",   "position":"得分後衛",   "play_year":"10",   "jerse_no":"13"    }
{  	"_index": "nba",  	"_type": "_doc",  	"_id": "1",  	"_version": 1,  	"result": "created",  	"_shards": {  		"total": 2,  		"successful": 1,  		"failed": 0  	},  	"_seq_no": 0,  	"_primary_term": 1  }
  • POST localhost:9200/nba/_doc (不指定id)
{   "name":"庫⾥",   "team_name":"勇⼠",   "position":"組織後衛",   "play_year":"10",   "jerse_no":"30"    }
{  	"_index": "nba",  	"_type": "_doc",  	"_id": "cVi582sB6wrnBnZnFqog",  	"_version": 1,  	"result": "created",  	"_shards": {  		"total": 2,  		"successful": 1,  		"failed": 0  	},  	"_seq_no": 1,  	"_primary_term": 1  }
⾃動創建索引
  • 查看auto_create_index開關狀態,請求http://localhost:9200/_cluster/settings
  • 當索引不存在並且auto_create_index為true的時候,新增⽂檔時會⾃動創建索引
  • 修改auto_create_index狀態
    • PUT localhost:9200/_cluster/settings
{  	"persistent": {  		"action.auto_create_index": "false"  	}  }
{  	"acknowledged": true,  	"persistent": {  		"action": {  			"auto_create_index": "false"  		}  	},  	"transient": {}  }
  • 當auto_create_index=false時,指定⼀個不存在的索引,新增⽂檔
    • PUT localhost:9200/wnba/_doc/1
{   "name":"楊超越",   "team_name":"夢之隊",   "position":"組織後衛",   "play_year":"0",   "jerse_no":"18"    }	
{  	"error": {  		"root_cause": [{  			"type": "index_not_found_exception",  			"reason": "no such index [wnba]",  			"resource.type": "index_expression",  			"resource.id": "wnba",  			"index_uuid": "_na_",  			"index": "wnba"  		}],  		"type": "index_not_found_exception",  		"reason": "no such index [wnba]",  		"resource.type": "index_expression",  		"resource.id": "wnba",  		"index_uuid": "_na_",  		"index": "wnba"  	},  	"status": 404  }
  • 當auto_create_index=true時,指定⼀個不存在的索引,新增⽂檔
{   "name":"楊超越",   "team_name":"夢之隊",   "position":"組織後衛",   "play_year":"0",   "jerse_no":"18"    }
{  	"_index": "wnba",  	"_type": "_doc",  	"_id": "1",  	"_version": 1,  	"result": "created",  	"_shards": {  		"total": 2,  		"successful": 1,  		"failed": 0  	},  	"_seq_no": 0,  	"_primary_term": 1  }
  • 指定操作類型
    • PUT localhost:9200/nba/_doc/1?op_type=create
{   "name":"哈登",   "team_name":"⽕箭",   "position":"得分後衛",   "play_year":"10",   "jerse_no":"13"    }
{  	"error": {  		"root_cause": [{  			"type": "version_conflict_engine_exception",  			"reason": "[1]: version conflict, document already  			exists(current version[2])  			",  			"index_uuid": "oPdc9qAjRO-IlzPfymnpkg",  			"shard": "0",  			"index": "nba"  		}],  		"type": "version_conflict_engine_exception",  		"reason": "[1]: version conflict, document already exists  			(current version[2])  		",  		"index_uuid": "oPdc9qAjRO-IlzPfymnpkg",  		"shard": "0",  		"index": "nba"  	},  	"status": 409  }
查看⽂檔
  • GET localhost:9200/nba/_doc/1
{  	"_index": "nba",  	"_type": "_doc",  	"_id": "1",  	"_version": 3,  	"_seq_no": 3,  	"_primary_term": 1,  	"found": true,  	"_source": {  		"name": "哈登",  		"team_name": "⽕箭",  		"position": "得分後衛",  		"play_year": "10",  		"jerse_no": "13"  	}  }
查看多個文檔
  • POST localhost:9200/_mget
{  	"docs": [{  			"_index": "nba",  			"_type": "_doc",  			"_id": "1"  		},  		{  			"_index": "nba",  			"_type": "_doc",  			"_id": "2"  		}  	]  } {  	"docs": [{  			"_index": "nba",  			"_type": "_doc",  			"_id": "1",  			"_version": 3,  			"_seq_no": 3,  			"_primary_term": 1,  			"found": true,  			"_source": {  				"name": "哈登",  				"team_name": "⽕箭",  				"position": "得分後衛",  				"play_year": "10",  				"jerse_no": "13"  			}  		},  		{  			"_index": "nba",  			"_type": "_doc",  			"_id": "2",  			"found": false  		}  	]  }
  • POST localhost:9200/nba/_mget
{  	"docs": [{  			"_type": "_doc",  			"_id": "1"  		},  		{  			"_type": "_doc",  			"_id": "2"  		}  	]  }
  • POST localhost:9200/nba/doc/mget
{  	"docs": [{  			"_id": "1"  		},  		{  			"_id": "2"  		}  	]  }
  • GET localhost:9200/nba/doc/mget
{   	"ids" : ["1", "2"]  }
修改⽂檔
  • 根據提供的⽂檔⽚段更新數據
    • POST localhost:9200/nba/_update/1
{  	"doc": {  		"name": "哈登",  		"team_name": "⽕箭",  		"position": "雙能衛",  		"play_year": "10",  		"jerse_no": "13"  	}  }
  • 向_source欄位,增加⼀個欄位
    • POST localhost:9200/nba/_update/1
{  	 "script": "ctx._source.age = 18"  }
  • 從_source欄位,刪除⼀個欄位
    • POST localhost:9200/nba/_update/1
{   	"script": "ctx._source.remove("age")"  }
  • 根據參數值,更新指定⽂檔的欄位
    • POST localhost:9200/nba/_update/1
{  	"script": {  		"source": "ctx._source.age += params.age",  		"params": {  			"age": 4  		}  	}  }
  • upsert 當指定的⽂檔不存在時,upsert參數包含的內容將會被插⼊到索引中,作為⼀個新⽂檔;如果指定的⽂檔存在,ElasticSearch引擎將會執⾏指定的更新邏輯。
    • POST localhost:9200/nba/_update/3
{  	"script": {  		"source": "ctx._source.allstar += params.allstar",  		"params": {  			"allstar": 4  		}  	},  	"upsert": {  		"allstar": 1  	}  }
刪除⽂檔
  • DELETE localhost:9200/nba/_doc/1