一般是在kibana中查看数据,查看数据的api中有get,mget,search等。
一、 get api
1.1 基于id获取数据
GET twitter/tweet/AWEsKql_h7jwcgPfOd03
运行结果:
{
"_index": "twitter",
"_type": "tweet",
"_id": "AWEsKql_h7jwcgPfOd03",
"_version": 1,
"found": true,
"_source": {
"user": "hello world",
"post_date": "2009-11-15T14:12:12",
"message": "trying out good"
}
}
这个操作是实时的,会自动调用reflesh来更新最新的数据,可以通过设置realtime=false关闭实时性;
如果只是不想查看数据,查看这个文档的元数据,可增加_source过滤,设置_source=false:
GET twitter/tweet/AWEsKql_h7jwcgPfOd03?_source=false
运行结果:
{
"_index": "twitter",
"_type": "tweet",
"_id": "AWEsKql_h7jwcgPfOd03",
"_version": 1,
"found": true
}
如查想查询某些字段或过滤掉某些字段,可设置_source_include & _source_exclude参数,也支持通配符。
GET twitter/tweet/AWEsKql_h7jwcgPfOd03?_source_include=user&_source_exclude=post_date
运行结果:
{
"_index": "twitter",
"_type": "tweet",
"_id": "AWEsKql_h7jwcgPfOd03",
"_version": 1,
"found": true,
"_source": {
"user": "hello world"
}
}
或者
GET twitter/tweet/AWEsKql_h7jwcgPfOd03?_source=user
运行结果:
{
"_index": "twitter",
"_type": "tweet",
"_id": "AWEsKql_h7jwcgPfOd03",
"_version": 1,
"found": true,
"_source": {
"user": "hello world"
}
}
1.2查询Stored Fields
PUT twitter
{
"mappings": {
"tweet": {
"properties": {
"counter": {
"type": "integer",
"store": false
},
"tags": {
"type": "keyword",
"store": true
}
}
}
}
}
PUT twitter/tweet/1
{
"counter" : 1,
"tags" : ["red"]
}
GET twitter/tweet/1?stored_fields=tags,counter
返回结果:
{
"_index": "twitter",
"_type": "tweet",
"_id": "1",
"_version": 1,
"found": true,
"fields": {
"tags": [
"red"
]
}
}
1.3直接返回source的内容
GET twitter/tweet/1/_source
运行结果
{
"counter": 1,
"tags": [
"red"
]
}
1.4查询Routing数据
GET twitter/tweet/AWEsM2rah7jwcgPfOeM8?routing=happyprince
返回结果:
{
"_index": "twitter",
"_type": "tweet",
"_id": "AWEsM2rah7jwcgPfOeM8",
"_version": 1,
"_routing": "happyprince",
"found": true,
"_source": {
"user": "happyprince",
"post_date": "2009-11-15T14:12:12",
"message": "go to beijing"
}
}
二、Multi Get API
Multi GET API allows to get multiple documents based on an index, type (optional) and id (and possibly routing).
2.1基于index,type,id获取多文档
GET edu/student/_mget
{
"ids" : ["1", "2"]
}
运行结果:
{
"docs": [
{
"_index": "edu",
"_type": "student",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "张三",
"age": 12
}
},
{
"_index": "edu",
"_type": "student",
"_id": "2",
"_version": 2,
"found": true,
"_source": {
"name": "张四",
"age": 13
}
}
]
}
注意
For example, if you have a document 1 within typeA and typeB then following request will give you back only the same document twice。
如果在type1与type2中都有文档1,要写两个。
GET edu/_mget
{
"ids" : ["1", "1"]
}
结果好像与官网的不一致.如果哪位知道为什么的,请告之一下。
数据中是有两type的,一个class,一个studen.可是出来的结果是:
{
"docs": [
{
"_index": "edu",
"_type": "student",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "张三",
"age": 12
}
},
{
"_index": "edu",
"_type": "student",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "张三",
"age": 12
}
}
]
}
还是这个保证一些:
GET edu/_mget/
{
"docs" : [
{
"_type":"class",
"_id" : "1"
},
{
"_type":"student",
"_id" : "1"
}
]
}
运行结果:
GET edu/_mget/
{
"docs" : [
{
"_type":"class",
"_id" : "1"
},
{
"_type":"student",
"_id" : "1"
}
]
}
2.2多index查询,多type,多id查询
按传统的数据库解释,对任意数据库,任意的表与任意字段进行查询。
GET _mget
{
"docs" : [
{
"_index" : "edu",
"_type" : "class",
"_id" : "1",
"_source" : false
},
{
"_index" : "iktest",
"_type" : "article",
"_id" : "8",
"_source" : ["subject"]
},
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1",
"_source" : {
"include": ["tags"],
"exclude": ["counter"]
}
}
]
}
作者:happyprince , http://blog.csdn.net/ld326/article/details/79258610