前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch 急速入门·文档与搜索

Elasticsearch 急速入门·文档与搜索

作者头像
netkiller old
发布2018-03-05 17:08:49
1.1K0
发布2018-03-05 17:08:49
举报
文章被收录于专栏:NetkillerNetkiller

节选自《Netkiller Database 手札》

60.2. 文档API

60.2.1. 快速上手

文档通过 _index、_type、_id 元数据(metadata),确定 URL 唯一

代码语言:javascript
复制
			GET /<_index>/<_type>/<_id>		
代码语言:javascript
复制
# 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 写入数据

代码语言:javascript
复制
[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。

代码语言:javascript
复制
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 读取数据

代码语言:javascript
复制
[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 数据,去掉元数据

代码语言:javascript
复制
# 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。

代码语言:javascript
复制
				# 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. 检查记录是否存在

代码语言:javascript
复制
[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
代码语言:javascript
复制
# 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. 搜索

搜索所有内容

代码语言:javascript
复制
# curl -XGET 'http://localhost:9200/_search?pretty'	
# curl -XGET 'http://localhost:9200/_all/_search?pretty'			

指定 _index 搜索

代码语言:javascript
复制
# curl -XGET 'http://localhost:9200/website/_search?pretty'
# curl -XGET 'http://localhost:9200/website/news/_search?pretty'		

指定 _type 搜索

代码语言:javascript
复制
# 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 搜索

代码语言:javascript
复制
# 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个结果,那么请求如下:

代码语言:javascript
复制
			第一页 GET /_search?size=10
第二页 GET /_search?size=10&from=10
第三页 GET /_search?size=10&from=20			

60.3.2. 字符串搜索

代码语言:javascript
复制
			# curl -XGET 'http://localhost:9200/_all/_search?q=neo&pretty'						

同时满足两个条件

代码语言:javascript
复制
+name:neo +age:30			

查找name为mary 或者 john的数据

代码语言:javascript
复制
+name:(mary john)			

查询姓名是neo或者jam并且年龄小于30岁同时1980-09-10之后出生的

代码语言:javascript
复制
			+name:(neo jam) +age:<30 +date:>1980-09-10			
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-08-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Netkiller 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 60.2. 文档API
    • 60.2.1. 快速上手
      • 60.2.2. 写入 PUT/POST
        • 60.2.3. 获取 GET
          • 60.2.3.1. _source
        • 60.2.4. 检查记录是否存在
          • 60.2.5. 参数
            • 60.2.5.1. pretty 格式化 json
        • 60.3. 搜索
          • 60.3.1. 分页
            • 60.3.2. 字符串搜索
            相关产品与服务
            Elasticsearch Service
            腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档