基础知识请参考一下链接
有很多写的不错的文章,自己先脑补下
https://www.jianshu.com/p/1df1529aaca7
https://blog.youkuaiyun.com/achuo/article/details/87865141
https://blog.youkuaiyun.com/Zereao/article/details/89378283
简介
本文使用的elasticsearch-7.5.0版本
注意:ES 7.0.0 开始,Type就被移除了 具体参考https://blog.youkuaiyun.com/Zereao/article/details/89378283
下边代码部分是使用postman生成的,为了方便让大家看我传输的数据,注意header部分 'Content-Type: application/json',
请求体body选择raw,里边直接写入json数据就行
例如:
如果想提高的话多多尝试吧~~下边给的几个案例只是为了让大家快速入门
创建索引库
var settings = {
"url": "http://localhost:9200/people",
"method": "PUT",
"timeout": 0,
"headers": {
"Content-Type": "application/json"
},
"data": JSON.stringify({"settings":{"number_of_shards":3,"number_of_replicas":1},"mappings":{"properties":{"type":{"type":"keyword"},"name":{"type":"text"},"country":{"type":"keyword"},"age":{"type":"integer"},"date":{"type":"date","format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"}}}}),
};
$.ajax(settings).done(function (response) {
console.log(response);
});
结果
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "wuqi"
}
查看索引库
var settings = {
"url": "http://localhost:9200/people/_mapping",
"method": "GET",
"timeout": 0,
};
$.ajax(settings).done(function (response) {
console.log(response);
});
结果:
{"people":{"mappings":{"properties":{"age":{"type":"integer"},"country":{"type":"keyword"},"date":{"type":"date","format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"},"name":{"type":"text"},"type":{"type":"keyword"}}}}}
创建映射
var settings = {
"url": "http://localhost:9200/people/_mapping",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json"
},
"data": JSON.stringify({"properties":{"name":{"type":"text"},"description":{"type":"text"},"studymodel":{"type":"keyword"}}}),
};
$.ajax(settings).done(function (response) {
console.log(response);
});
结果:
{
"acknowledged": true
}
创建文档
var settings = {
"url": "http://localhost:9200/people/_doc/",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json"
},
"data": JSON.stringify({"name":"我是男人","description":"不要问一个男人行不行777","studymodel":"2010133101"}),
};
$.ajax(settings).done(function (response) {
console.log(response);
});
结果:
{"_index":"people","_type":"_doc","_id":"AZj7NW8BQw2NfPKHHzFy","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":2}
浏览数据
var settings = {
"url": "http://localhost:9200/people/_doc/AZj7NW8BQw2NfPKHHzFy",
"method": "GET",
"timeout": 0,
"headers": {
"Content-Type": "application/json"
},
};
$.ajax(settings).done(function (response) {
console.log(response);
});
结果:
{"_index":"people","_type":"_doc","_id":"AZj7NW8BQw2NfPKHHzFy","_version":1,"_seq_no":0,"_primary_term":2,"found":true,"_source":{
"name":"我是男人",
"description":"不要问一个男人行不行777",
"studymodel":"2010133101"
}}
修改数据
var settings = {
"url": "http://localhost:9200/people/_update/AZj7NW8BQw2NfPKHHzFy",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json"
},
"data": JSON.stringify({"doc":{"name":"UpdateZereao22222222222222222222222"}}),
};
$.ajax(settings).done(function (response) {
console.log(response);
});
结果:
{"_index":"people","_type":"_doc","_id":"AZj7NW8BQw2NfPKHHzFy","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":2}
条件查询
curl --location --request POST 'http://localhost:9200/l_suoyiku/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match": {
"description":"777"
}
},
"highlight": { //高亮显示
"fields": {
"description": {}
}
},
"from": 1,
"size": 100
}'
结果:
{"took":106,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":7,"relation":"eq"},"max_score":1.5126113,"hits":[{"_index":"l_suoyiku","_type":"_doc","_id":"JrRKMm8BU9yncorEfwSU","_score":1.5126113,"_source":{
"name":"我是男人",
"description":"不要问一个男人行不行777",
"studymodel":"2010133101"
},"highlight":{"description":["不要问一个男人行不行<em>777</em>"]}},{"_index":"l_suoyiku","_type":"_doc","_id":"J7RKMm8BU9yncorEggQ3","_score":1.5126113,"_source":{
"name":"我是男人",
"description":"不要问一个男人行不行777",
"studymodel":"2010133101"
},"highlight":{"description":["不要问一个男人行不行<em>777</em>"]}},{"_index":"l_suoyiku","_type":"_doc","_id":"KLRKMm8BU9yncorEhARl","_score":1.5126113,"_source":{
"name":"我是男人",
"description":"不要问一个男人行不行777",
"studymodel":"2010133101"
},"highlight":{"description":["不要问一个男人行不行<em>777</em>"]}},{"_index":"l_suoyiku","_type":"_doc","_id":"KbRKMm8BU9yncorEhgS2","_score":1.5126113,"_source":{
"name":"我是男人",
"description":"不要问一个男人行不行777",
"studymodel":"2010133101"
},"highlight":{"description":["不要问一个男人行不行<em>777</em>"]}},{"_index":"l_suoyiku","_type":"_doc","_id":"KrRKMm8BU9yncorEiQQS","_score":1.5126113,"_source":{
"name":"我是男人",
"description":"不要问一个男人行不行777",
"studymodel":"2010133101"
},"highlight":{"description":["不要问一个男人行不行<em>777</em>"]}},{"_index":"l_suoyiku","_type":"_doc","_id":"K7RKMm8BU9yncorEiwRQ","_score":1.5126113,"_source":{
"name":"我是男人",
"description":"不要问一个男人行不行777",
"studymodel":"2010133101"
},"highlight":{"description":["不要问一个男人行不行<em>777</em>"]}}]}}
删除文档
curl --location --request DELETE 'http://localhost:9200/l_suoyiku/_doc/2' \
--header 'Content-Type: application/json'