一、添加文档数据
POST /my_doc/_doc/1 -> {索引名}/_doc/{索引ID}(是指索引在es中的id,而不是这条记录的id,比如记录的id从数据库来是1001,并不是这个。如果不写,则自动生成一个字符串。建议和数据id保持一致> )
{
"id":1001,
"name":"CSDN-1",
"desc":"CSDN is very good, CSDN非常牛!",
"create_date":"2019-12-24"
}{
"id":1002,
"name":"CSDN-2",
"desc":"CSDN is fashion, CSDN非常时尚!",
"create_date":"2019-12-25"
}{
"id":1003,
"name":"CSDN-3",
"desc":"CSDN is niubility, CSDN很好很强大!",
"create_date":"2019-12-26"
}{
"id":1004,
"name":"CSDN-4",
"desc":"CSDN is good~!",
"create_date":"2019-12-27"
}{
"id":1005,
"name":"CSDN-5",
"desc":"CSDN is 强大!",
"create_date":"2019-12-28"
}{
"id":1006,
"name":"CSDN-6",
"desc":"CSDN是一个强大网站!",
"create_date":"2022-08-31"
}{
"id":1007,
"name":"CSDN-7",
"desc":"CSDN是很牛网站!",
"create_date":"2022-08-31"
}{
"id":1008,
"name":"CSDN-8",
"desc":"CSDN是很好看!",
"create_date":"2022-08-31"
}{
"id":1009,
"name":"CSDN-9",
"desc":"在CSDN学习很久!",
"create_date":"2022-08-31"
}
演示:
点击数据浏览,找到索引里面的my_doc,点击这个索引,再点击一下右侧表格的my_doc就会弹出原始数据。
- 注:如果索引没有手动建立mappings,那么当插入文档数据的时候,会根据文档类型自动设置属性类型。这个就是es的动态映射,帮我们在index索引库中去建立数据结构的相关配置信息。
- "fields":{"type":"keyword"}
对于一个字段设置多种索引模式,使用text类型做全文检索,也可以使用keyword类型做聚合和排序
- "ignore_above":256
设置字段索引和存储的长度最大值,超过则被忽略
我们在将剩余的文档数据都添加进去
剩余的添加操作类似,省略截图,最后结果如下:
二、删除文档数据
DELETE /my_doc/_doc/1
- 注:文档删除不是立即删除,文档还是保存在磁盘上,索引增长越来越多,才会把那些曾经标识过删除的文档进行清理,从磁盘中移除。
三、修改文档数据
- 局部替换:
POST /my_doc/_doc/1/_update
{
"doc": {
"name": "CSDN-1001"
}
}
- 全量替换:
PUT /my_doc/_doc/1
{
"id": 1001,
"name": "CSDN-1001",
"desc": "CSDN is very good, CSDN非常牛!",
"create_date": "2022-08-31"
}
- 注:每次修改,version会更改。
四、查询文档数据
常规查询
GET /index_demo/_doc/1
GET /index_demo/_search
查询结果:
查询结果:
{
"took":7,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":10,
"relation":"eq"
},
"max_score":1,
"hits":[
{
"_index":"my_doc",
"_id":"3",
"_score":1,
"_source":{
"id":1003,
"name":"CSDN-3",
"desc":"CSDN is niubility, CSDN很好很强大!",
"create_date":"2019-12-26"
}
},
{
"_index":"my_doc",
"_id":"5",
"_score":1,
"_source":{
"id":1005,
"name":"CSDN-5",
"desc":"CSDN is 强大!",
"create_date":"2019-12-28"
}
},
{
"_index":"my_doc",
"_id":"9",
"_score":1,
"_source":{
"id":1009,
"name":"CSDN-9",
"desc":"在CSDN学习很久!",
"create_date":"2022-08-31"
}
},
{
"_index":"my_doc",
"_id":"4",
"_score":1,
"_source":{
"id":1004,
"name":"CSDN-4",
"desc":"CSDN is good~!",
"create_date":"2019-12-27"
}
},
{
"_index":"my_doc",
"_id":"1001",
"_score":1,
"_source":{
}
},
{
"_index":"my_doc",
"_id":"7",
"_score":1,
"_source":{
"id":1007,
"name":"CSDN-7",
"desc":"CSDN是很牛网站!",
"create_date":"2022-08-31"
}
},
{
"_index":"my_doc",
"_id":"8",
"_score":1,
"_source":{
"id":1008,
"name":"CSDN-8",
"desc":"CSDN是很好看!",
"create_date":"2022-08-31"
}
},
{
"_index":"my_doc",
"_id":"2",
"_score":1,
"_source":{
"id":1002,
"name":"CSDN-2",
"desc":"CSDN is fashion, CSDN非常时尚!",
"create_date":"2019-12-25"
}
},
{
"_index":"my_doc",
"_id":"6",
"_score":1,
"_source":{
"id":1006,
"name":"CSDN-6",
"desc":"CSDN是一个强大网站!",
"create_date":"2022-08-31"
}
},
{
"_index":"my_doc",
"_id":"1",
"_score":1,
"_source":{
"id":1001,
"name":"CSDN-1",
"desc":"CSDN is very good, CSDN非常牛!",
"create_date":"2019-12-24"
}
}
]
}
}
元数据-文档数据
- _index:文档数据所属那个索引,理解为数据库的某张表即可。
- _type:类型,新版本使用_doc。
- _id:文档的唯一标识,表的主键。可以自动生成或手动指定。
- _score:查询相关度,是否契合用户匹配,分数越高用户的搜索体验越高。
- _version:版本号。
- _source:文档数据,json格式。
查询定制结果集
GET /my_doc/_doc/1?_source=id,name
GET /my_doc/_search?_source=id,name
判断文档是否存在
HEAD /my_doc/_doc/1
文章跳转:
上一篇文章:1.4 Elasticsearch索引的mappings映射_Iamlvxiaosha的博客-CSDN博客
下一篇文章:1.6 Elasticsearch文档乐观锁控制if_seq_no与if_primary_term观察操作_Iamlvxiaosha的博客-CSDN博客