1: 概念
顾名思义,该API是查询获取数据的接口,
2:样例
获取索引twitter中文档_doc中id为1的数据
GET twitter/_doc/1
上面的结果中包含_id ,_type, _index, _version ,fount 和,最终结果_source ,
如果该文档不存在,则返回结果如下
实际上,可以使用head 命令来预先查询所查询的文档是否存在
HEAD twitter/_doc/3
不存在,则返回404 存在则返回200
该API的一些概念
-
实时
默认情况下,GET API是实时的,并且不受索引刷新率的影响(当数据对搜索可见时)。例如如果文档已更新但未刷新,则GET API会就地发出刷新请求使文档请求可见,这也将上次刷新后其他文档也发生更新。可见realtime参数设置为false.(如何设置 待定)
-
源过滤(字段过滤)
GET API在默认情况下是返回所有字段的,如果在mapping的时候指定了 stored或者_source 禁用了的情况下除外,例如要查询fields中包含"ss"的
GET twitter/_doc/1?_source=*ss*
运行结果如下:
使用_source=false 来禁用返回字段
GET twitter/_doc/1?_source=false
如果想查询返回一两个字段或者过滤一两个字段,则可以使用_source_include 或者 _source_exclude ,这两个可以使用通配符或者逗号列表来展示
*ss* 返回fields中包含ss的所有列
user1 ,user2 返回fields中的 user1和user2列
*id,user2 返回fields中的所有id结尾和user2的列
GET twitter/_doc/1?_source_include=user_name,password
运行结果如下:
过滤一个字段:
GET twitter/_doc/1?_source_exclude=user_name
运行结果如下:
- store fieds
顾名思义,就是存储的字段,在mapping中指定。例如
PUT twitter1
{
"mappings": {
"_doc": {
"properties": {
"counter": {
"type": "integer",
"store": false
},
"tags": {
"type": "keyword",
"store": true
}
}
}
}
}
添加数据
PUT twitter1/_doc/1
{
"counter" : 1,
"tags" : ["red"]
}
使用store_fields进行检索
GET twitter/_doc/1?stored_fields=tags,counter
执行结果如下:
_routing 指定路由
PUT twitter1/_doc/2?routing=user1
{
"counter" : 1,
"tags" : ["white"]
}
根据路由检索数据
如果路由不对则检索不出数据。
- _source
获取所有的fields
GET twitter1/_doc/2?_source
执行结果
Preference (偏好)
控制可以分片