mapping操作
新增索引及mapping
先新增索引
PUT http://localhost:9200/job
新增映射
post http://localhost:9200/job/_mapping
{
"properties": {
"jid": {
"type": "long"
},
"title": {
"type": "text"
},
"company": {
"type": "text"
},
"salary": {
"type": "integer_range"
},
"city": {
"type": "keyword"
},
"description": {
"type": "text"
},
"req_time": {
"type":"date",
"format": "yyyy-MM-dd HH:mm:ss||date_optional_time||epoch_millis"
},
}
}
?或者上述兩步和為一步(創(chuàng)建索引,及創(chuàng)建mapping)
post http://localhost:9200/job/_mapping
{
"mappings": {
? ? "properties": {
? ? ? ? "jid": {
? ? ? ? ? ? "type": "long"
? ? ? ? },
? ? ? ? "title": {
? ? ? ? ? ? "type": "text"
? ? ? ? },
? ? ? ? "company": {
? ? ? ? ? ? "type": "text"
? ? ? ? },
? ? ? ? "salary": {
? ? ? ? ? ? "type": "integer_range"
? ? ? ? },
? ? ? ? "city": {
? ? ? ? ? ? "type": "keyword"
? ? ? ? },
? ? ? ? "description": {
? ? ? ? ? ? "type": "text"
? ? ? ? },
"req_time": {
"type":"date",
"format": "yyyy-MM-dd HH:mm:ss||date_optional_time||epoch_millis"
}
? ? }
}
}
對(duì)已存在的index增加映射
只能增加原有不存在的字段
POST /job/_mapping
{
"properties": {
"test1": {
"type": "long"
},
"test2": {
"type": "integer"
}
}
}
如何修改mapping?
創(chuàng)建一個(gè)全新的索引,映射包含調(diào)整后的字段或類型
將原有索引的數(shù)據(jù)遷移到新的索引
刪除原有索引
將新的索引的別名設(shè)置為原來(lái)索引相同名稱
文檔的操作
創(chuàng)建文檔
創(chuàng)建一個(gè)
post http://localhost:9200/job/_create/1
{
? ? "jid": 1,
? ? "title": "Java開發(fā)工程師",
? ? "company": "北京威米信科技有限公司",
? ? "salary": {
? ? ? ? "gte": 9000,
? ? ? ? "lte": 15000
? ? },
? ? "city": "北京",
? ? "description": "xxx"
}
重建文檔(全量更新)
?
post http://localhost:9200/job/_doc/1
{
? ? "jid": 1,
? ? "title": "Java開發(fā)工程師",
? ? "company": "北京威米信科技有限公司",
? ? "salary": {
? ? ? ? "gte": 9000,
? ? ? ? "lte": 15000
? ? },
? ? "city": "北京",
? ? "description": "xxx"
}
更新操作
全量更新與局部更新
若原文檔為{"a":1,"b":2}
全量更新是:若更新數(shù)據(jù)為{"a":111},則get整個(gè)文檔變?yōu)閧"a":111}
局部更新是:若更新數(shù)據(jù)為{"a":111},則get整個(gè)文檔變?yōu)閧"a":111,"b":2}
https://liuhuiyao.blog.csdn.net/article/details/120849094
條件更新
// POST /lhy_test/_update_by_query
// {
// "query": {
// "terms" : {
// "_id" : ["_create"]
// }
// },
// "script": {
// "source": "ctx._source.salary=100",
// "lang": "painless"
// }
// }
?單個(gè)更新
// POST /xxx/_update/1?refresh=true
// {
// "doc":{
// "isec_opt_state":1
// },
// "doc_as_upsert":false//#重要:false當(dāng)id為1的記錄不存在時(shí)會(huì)更新報(bào)錯(cuò),true當(dāng)id為1的記錄不存在時(shí)會(huì)創(chuàng)建索引并插入記錄
//
// }
批量新增、更新操作
https://liuhuiyao.blog.csdn.net/article/details/121488282文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-501477.html
刪除文檔
delete http://localhost:9200/job/_doc/1文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-501477.html
網(wǎng)上案例
一,索引文檔
// 稱之為index一個(gè)文檔,指定ID,Put創(chuàng)建必須指定ID
// 如果文檔存在,會(huì)先刪除文檔,重新創(chuàng)建
PUT lcy_test/_doc/1
{
? "name":"lch"
}
二,create文檔
// 指定ID,同時(shí)指定是create,如果id存在,則報(bào)錯(cuò)
PUT lcy_test/_doc/1?op_type=create
{
? "name":"lch"
}
三,create文檔2
// 另外一種create的方法,
//指定Id,同時(shí)指定是create,如果id存在,則報(bào)錯(cuò)
PUT lcy_test/_create/4
{
? "name":"lch"
}
四,post創(chuàng)建文檔
// POST創(chuàng)建文檔,不用指定id
POST lcy_test/_doc
{
? "name":"james2"
}
生成新文檔有2中方式:一是指定ID,索引文檔、create文檔,put和post都可以;
二是不指定ID:post方式自動(dòng)生成ID,只有post,put不行;
五,update文檔
// update,修改內(nèi)容必須包含在doc中
POST lcy_test/_update/1
{
? "doc":{
? ? "name":"lcy2",
? ? "first name":"yong"
? }
}
六,查詢文檔
GET lcy_test/_doc/2FJtCHcBTCkjMQHa_GD1
七,批量操作 bulk
// 批量操作1
// bulk
POST ?_bulk
{"index":{"_index":"lcy_test"}}
{"name":"get"}
{"delete":{"_index":"lcy_test","_id":"2FJtCHcBTCkjMQHa_GD1"}}
{"create":{"_index":"lcy_test","_id":1}}
{"name":"get"}
八,批量查詢 mget
// 批量查詢 _mget
GET _mget?
{
? "docs":[
? ? {
? ? ? "_index":"lcy_test",
? ? ? "_id":1
? ? },
? ? {
? ? ? "_index":"lcy_test",
? ? ? "_id":2
? ? }
? ]
}
九,批量查詢 mquery
// 批量查詢 _mquery
POST kibana_sample_data_ecommerce/_msearch
{}
{"query":{"match_all":{}},"size":1}
{"index":"kibana_sample_data_flights"}
{"query":{"match_all":{}},"size":2}
到了這里,關(guān)于es elasticsearch 新增更新索引,新增更新文檔的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!