ElasticSearch_(4)文檔介紹

新增文檔 PUT

指定id 新增數據 _doc

curl -X PUT "localhost:9200/nba/_doc/1" -H 'Content-Type:application/json' -d '
{
	"name":"哈登",
	"team_name":"火箭",
	"position":"得分後衛",
	"play_year":"10",
	"jerse_no":"13"
}
'

shell 中需如下填寫

curl -X PUT "localhost:9200/nba/_doc/1" -H 'Content-Type:application/json' -d '{"name":"哈登","team_name":"火箭","position":"得分後衛","play_year":"10","jerse_no":"13"}'

返回一個json

{
	"_index":"nba",
	"_type":"_doc",
	"_id":"1",
	"_version":1,
	"result":"created",
	"_shards"{
		"total":2,
		"successful":1,
		"failed":0
	},
	"_seq_no":0,
	"_primary_term":3
}

不指定id 自動新增數據 POST

curl -X POST "localhost:9200/nba/_doc" -H 'Content-Type:application/json' -d '
{
	"name":"庫裏",
	"team_name":"勇士",
	"position":"組織後衛",
	"play_year":"10",
	"jerse_no":"30"
}
'

在這裏插入圖片描述

自動創建索引 PUT

查看 auto_create_index 開關狀態

curl -X GET "localhost:9200/_cluster/settings"

返回一個json

{
    "persistent": {},
    "transient": {}
}

當索引不存在並且auto_create_index爲True的時候,新增文檔時會自動創建索引

修改 auto_create_index 狀態

curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type:application/json' -d '
{
	"persistent":{
		"action.auto_create_index":"false"
	}
}
'

返回一個json

{
    "acknowledged": true,
    "persistent": {
        "action": {
            "auto_create_index": "false"
        }
    },
    "transient": {}
}

指定操作類型 ?op_type=create

新增文檔時防止誤操,把已有的文檔更新掉,可以指定操作類型
要新增文檔,重複與已有id重複,不指定操作類型時,會導致數據更新,未達到目標要求
在這裏插入圖片描述
指定操作類型,當輸入重複的id號新增數據,會提示報錯

curl -X PUT "localhost:9200/nba/_doc/1?op_type=create" -H 'Content-Type:application/json' -d '
{
	"name":"大鬍子",
	"team_name":"火箭",
	"position":"得分後衛",
	"play_year":"10",
	"jerse_no":"13"
}
'

在這裏插入圖片描述

查看文檔

指定id 查看文檔 GET

curl -X GET "localhost:9200/nba/_doc/1"

在這裏插入圖片描述

查看多個文檔 POST

_mget 幾種寫法查看多個文檔

curl -X POST "localhost:9200/_mget" -H 'Content-Type:application/json' -d '
{
	"docs":[
		{
			"_index":"nba",
			"_type":"_doc",
			"_id":"1"
		},
		{
			"_index":"nba",
			"_type":"_doc",
			"_id":"2"
		}
	]
}
'
  1. 先指定索引
curl -X POST "localhost:9200/nba/_mget" -H 'Content-Type:application/json' -d '
{
	"docs":[
		{
			"_type":"_doc",
			"_id":"1"
		},
		{
			"_type":"_doc",
			"_id":"2"
		}
	]
}
'
  1. 指定索引和文檔
curl -X POST "localhost:9200/nba/_doc/_mget" -H 'Content-Type:application/json' -d '
{
	"docs":[
		{
			"_id":"1"
		},
		{
			"_id":"2"
		}
	]
}
'
  1. 指定了索引和文檔,傳遞了一個id數組
curl -X POST "localhost:9200/nba/_doc/_mget" -H 'Content-Type:application/json' -d '
{
	"ids":["1","2"]
}
'

在這裏插入圖片描述

修改文檔 POST

_update

根據提供的文檔片段更新數據

curl -X POST "localhost:9200/nba/_update/1" -H 'Content-Type:application/json' -d '
{
	"doc":{
		"name":"大鬍子",
		"team_name":"火箭",
		"position":"得分後衛",
		"play_year":"20",
		"jerse_no":"13"
	}
}
'

_source字段,增加一個字段

script 表示腳本
ctx 上下文
\ json中不能有兩個雙引號,需使用反斜線

curl -X POST "localhost:9200/nba/_update/1" -H 'Content-Type:application/json' -d '
{
	"script":"ctx._source.age = 18"
}
'

_source字段,刪除一個字段

curl -X POST "localhost:9200/nba/_update/1" -H 'Content-Type:application/json' -d '
{
	"script":"ctx._source.remove(\"age\")"
}
'

根據參數值,更新指定文檔的字段內容

curl -X POST "localhost:9200/nba/_update/1" -H 'Content-Type:application/json' -d '
{
	"script":{
		"source":"ctx._source.age += params.age",
		"params":{
			"age":4
		}
	}
}
'

upsert
當指定的文檔不存在時,upsert參數包含的內容將會被插入到索引中,作爲一個新文檔;
如果指定的文檔存在,ES引擎將會執行指定的更新邏輯

curl -X POST "localhost:9200/nba/_update/3" -H 'Content-Type:application/json' -d '
{
	"script":{
		"source":"ctx._source.allstar += params.allstar",
		"params":{
			"allstar":4
		}
	},
	"upsert":{
		"allstar":1
	}
}
'

刪除文檔

根據指定id 刪除文檔

curl -X DELETE "localhost:9200/nba/_doc/3" 

在這裏插入圖片描述

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