节选自《Netkiller Database 手札》
60.2. 文档API
60.2.1. 快速上手
文档通过 _index、_type、_id 元数据(metadata),确定 URL 唯一
GET /<_index>/<_type>/<_id>
复制
# curl -XPUT 'http://localhost:9200/website/profile/1' -d '{
"name" : "neo",
"nickname" : "netkiller",
"age" : "35",
"message" : "Helloworld !!!"
}'
# curl -XGET 'http://localhost:9200/website/profile/1?pretty'
{
"_index" : "website",
"_type" : "profile",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "neo",
"nickname" : "netkiller",
"age" : "35",
"message" : "Helloworld !!!"
}
}
# curl -XPUT 'http://localhost:9200/website/blog/1?pretty' -d '{
> "title": "My first blog entry",
> "text": "Just trying this out...",
> "date": "2014/01/01"
> }'
{
"_index" : "website",
"_type" : "blog",
"_id" : "1",
"_version" : 1,
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}复制
后面会详细讲解 PUT与GET的使用方法以及相关参数
60.2.2. 写入 PUT/POST
通过 PUT 写入数据
[root@localhost ~]# curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
> "user" : "kimchy",
> "post_date" : "2009-11-15T14:12:12",
> "message" : "trying out Elasticsearch"
> }'
{"_index":"twitter","_type":"tweet","_id":"1","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}
复制
使用 UUID 替代 _id, 注意使用UUID 必须使用 POST方式提交,不能使用 PUT。
curl -XPOST 'http://localhost:9200/website/news/?pretty' -d '{
"title": "My first news entry",
"text": "Just trying this out..."
}'
{
"_index" : "website",
"_type" : "news1",
"_id" : "AVY0RJrvJRTrBLpmYzBH",
"_version" : 1,
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}
curl -XGET 'http://localhost:9200/website/news/AVY0RJrvJRTrBLpmYzBH?pretty'复制
提交后会输出 "_id" : "AVY0RJrvJRTrBLpmYzBH",查询时将此放到放到URL中即可。
60.2.3. 获取 GET
通过 GET 读取数据
[root@localhost ~]# curl -XGET 'http://localhost:9200/twitter/tweet/1'
{"_index":"twitter","_type":"tweet","_id":"1","_version":1,"found":true,"_source":{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}}复制
60.2.3.1. _source
只返回 _source 数据,去掉元数据
# curl -XGET 'http://localhost:9200/website/news1/AVY0Q4SqdtH0Up0t-WB2/_source?pretty'
{
"title" : "My first news entry",
"text" : "Just trying this out..."
}复制
选择字段 _source=title,超过一个字段使用逗号分隔_source=title,text。
# curl -XGET 'http://localhost:9200/website/news1/AVY0Q4SqdtH0Up0t-WB2?_source=title&pretty'
{
"_index" : "website",
"_type" : "news1",
"_id" : "AVY0Q4SqdtH0Up0t-WB2",
"_version" : 1,
"found" : true,
"_source" : {
"title" : "My first news entry"
}
}
# curl -XGET 'http://localhost:9200/website/news1/AVY0Q4SqdtH0Up0t-WB2?_source=title,text&pretty'
{
"_index" : "website",
"_type" : "news1",
"_id" : "AVY0Q4SqdtH0Up0t-WB2",
"_version" : 1,
"found" : true,
"_source" : {
"text" : "Just trying this out...",
"title" : "My first news entry"
}
}
复制
60.2.4. 检查记录是否存在
[root@localhost elasticsearch]# curl -i -XHEAD http://localhost:9200/website/blog/1
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
[root@localhost elasticsearch]# curl -i -XHEAD http://localhost:9200/website/blog/100
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=UTF-8
Content-Length: 0复制
HTTP/1.1 200 OK 表示已经找到你要的数据
HTTP/1.1 404 Not Found 表示数据不存在
60.2.5. 参数
60.2.5.1. pretty 格式化 json
# curl -XGET 'http://localhost:9200/twitter/tweet/1?pretty'
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
}复制
60.3. 搜索
搜索所有内容
# curl -XGET 'http://localhost:9200/_search?pretty'
# curl -XGET 'http://localhost:9200/_all/_search?pretty'
复制
指定 _index 搜索
# curl -XGET 'http://localhost:9200/website/_search?pretty'
# curl -XGET 'http://localhost:9200/website/news/_search?pretty'复制
指定 _type 搜索
# curl -XGET 'http://localhost:9200/website,twitter/_search?pretty'
# curl -XGET 'http://localhost:9200/website/news,blog/_search?pretty'
# curl -XGET 'http://localhost:9200/website,twitter/news,blog/_search?pretty'复制
所有 _index 包含指定 _type 搜索
# curl -XGET 'http://localhost:9200/_all/news,blog/_search?pretty'复制
60.3.1. 分页
该功能与SQL的LIMIT关键字结果一样,Elasticsearch接受size和from两个参数参数:
size: 返回结果集数量,默认10,用法与SQL中的 Limit相同
from: 偏移量,默认0,用法与 SQL中的 Offset相同
如果你想每页显示10个结果,那么请求如下:
第一页 GET /_search?size=10
第二页 GET /_search?size=10&from=10
第三页 GET /_search?size=10&from=20
复制
60.3.2. 字符串搜索
# curl -XGET 'http://localhost:9200/_all/_search?q=neo&pretty'
复制
同时满足两个条件
+name:neo +age:30复制
查找name为mary 或者 john的数据
+name:(mary john)复制
查询姓名是neo或者jam并且年龄小于30岁同时1980-09-10之后出生的
+name:(neo jam) +age:<30 +date:>1980-09-10复制
延伸阅读
使用OpenLDAP 操作 Windows Active Directory
长按下面二维码,关注我的公众号,每天推推送原创技术文章。
文章转载自netkiller,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
数据库国产化替代深化:DBA的机遇与挑战
代晓磊
1275次阅读
2025-04-27 16:53:22
2025年4月国产数据库中标情况一览:4个千万元级项目,GaussDB与OceanBase大放异彩!
通讯员
751次阅读
2025-04-30 15:24:06
国产数据库需要扩大场景覆盖面才能在竞争中更有优势
白鳝的洞穴
618次阅读
2025-04-14 09:40:20
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
523次阅读
2025-04-17 17:02:24
一页概览:Oracle GoldenGate
甲骨文云技术
485次阅读
2025-04-30 12:17:56
GoldenDB数据库v7.2焕新发布,助力全行业数据库平滑替代
GoldenDB分布式数据库
475次阅读
2025-04-30 12:17:50
阿里云 Elasticsearch Serverless 检索增强型 8.17 版来袭!
阿里云大数据AI技术
380次阅读
2025-04-18 10:24:15
优炫数据库成功入围新疆维吾尔自治区行政事业单位数据库2025年框架协议采购!
优炫软件
364次阅读
2025-04-18 10:01:22
给准备学习国产数据库的朋友几点建议
白鳝的洞穴
337次阅读
2025-05-07 10:06:14
XCOPS广州站:从开源自研之争到AI驱动的下一代数据库架构探索
韩锋频道
299次阅读
2025-04-29 10:35:54