elasticsearch 基础语句

本文介绍了Elasticsearch中文档ID的生成方式、文档的_source元数据定制返回结果的方法、文档的全量替换与删除机制及部分更新操作。通过具体实例展示了如何使用REST API进行文档的创建、读取、更新和删除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.  doucument id 的两种生成方式

自动生成document id
自动生成的id,长度为20个字符,URL安全,base64编码,GUID,分布式系统并行生成时不可能会发生冲突

POST /test_index/test_type (这里没有标识id)
{
"test_content": "my test"
}

GET /test_index/test_type/_search


手动指定document id

PUT /test_index/test_type/2
{
"test_content":"ding-jiang"
}

GET /test_index/test_type/2

 

注意点:1 我们的{}不能和 PUT写到一行 否则会报错 failed to parse, document is empty
    2 GET /test_index/test_type/_search 会得到该节点下所有的数据列表
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
2.    doucument的_source元数据以及定制返回结果解析

put /test_index/test_type/1
{
"test":"test11",
"test1":"test22"
}
GET /test_index/test_type/1

返回
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"test": "test11",
"test1": "test22"
}
}

_source元数据:就是创建document时我们存入的数据

如果我们不想返回全部的数据,只想返回一部分数据,比如有test 和test11 而我们只用test那么

GET /test_index/test_type/1?_source=test 则_source只返回
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"test": "test11"
}
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

3.    doucument的全量替换 强制创建和 delete 机制

1、document的全量替换
(1)语法与创建文档是一样的,如果document id不存在,那么就是创建;如果document id已经存在,那么就是全量替换操作,替换document的json串内容
(2)document是不可变的,如果要修改document的内容,第一种方式就是全量替换,直接对document重新建立索引,替换里面所有的内容
(3)es会将老的document标记为deleted,然后新增我们给定的一个document,当我们创建越来越多的document的时候,es会在适当的时机在后台自动删除标记为deleted的document

就是说全量替换会将_source的内容替换,但是之前的内容并没有从库中删除而是被标记为了deleted,es在恰当的时候会在动删除这些deleted数据(document)
创建的标识就是GET /test_index/test_type/1 中的_version会加1
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 3,
"found": true,
"_source": {
"test": "test11",
"test1": "test22",
"test_version": 3
}
}

2. 强制创建
(1)创建文档与全量替换的语法是一样的,有时我们只是想新建文档,不想替换文档,如果强制进行创建呢?
PUT /test_index/test_type/1/_create
{
"test333":"ding"
}
会报错 因为id是不能重复的 如果我们想创建那么id必须修改
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[test_type][1]: version conflict, document already exists (current version [3])",
"index_uuid": "XtO4uL9HTo2v34qmximdLg",
"shard": "3",
"index": "test_index"
}
],
"type": "version_conflict_engine_exception",
"reason": "[test_type][1]: version conflict, document already exists (current version [3])",
"index_uuid": "XtO4uL9HTo2v34qmximdLg",
"shard": "3",
"index": "test_index"
},
"status": 409
}

3. 删除

DELETE /test_index/test_type/11
GET /test_index/test_type/11

这个和全量替换一样不会立即物理删除,只会将其标记为deleted,当数据越来越多的时候,在后台自动删除

 

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

4   partial update

什么是partial update?

PUT /index/type/id,创建文档&替换文档,一样的语法

post /index/type/id/_update
{
"doc": {
"要修改的少数几个field即可,不需要全量的数据"
}
}

 例如:

POST /test_index/test_type/8/_update
{
"doc":{
"test_field":"partial update",
"test2":"test22"
}
}

GET /test_index/test_type/8

{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_version": 7,
"found": true,
"_source": {
"test_field": "partial update",
"test2": "test22"
}
}

 

转载于:https://www.cnblogs.com/studyitskill/p/7804451.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值