1、我们如果发出一个搜索请求的话,会拿到一堆搜索结果,本节课,我们来讲解一下,这个搜索结果里的各种数据,都代表了什么含义
查询所有
GET /_search
响应结果
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 11,
"successful": 11,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 14,
"max_score": 1,
"hits": [
{
"_index": ".kibana",
"_type": "config",
"_id": "5.6.0",
"_score": 1,
"_source": {
"buildNum": 15523
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_score": 1,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "10",
"_score": 1,
"_source": {
"test_field1": "test1",
"test_field2": "updated test2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "12",
"_score": 1,
"_source": {
"test_field": "test12"
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 1,
"_source": {
"name": "jiajieshi yagao",
"desc": "youxiao fangzhu",
"price": 25,
"producer": "jiajieshi producer",
"tags": [
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_score": 1,
"_source": {
"name": "special yagao",
"desc": "special meibai",
"price": 50,
"producer": "special yagao producer",
"tags": [
"meibai"
]
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "4",
"_score": 1,
"_source": {
"test_field1": "test field111111"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "6",
"_score": 1,
"_source": {
"test_field": "test test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_score": 1,
"_source": {
"test_field": "replaced test2"
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 1,
"_source": {
"name": "gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
}
]
}
}
总共有系统自带的.kabana、实战练习中创建的test_index和ecommerce,共3个索引
由于数据过多,只看一部分结果
{
"took": 8,
"timed_out": false,
"_shards":
{
"total": 11,
"successful": 11,
"skipped": 0,
"failed": 0
},
"hits":
{
"total": 14,
"max_score": 1,
"hits": [
{
"_index": ".kibana",
"_type": "config",
"_id": "5.6.0",
"_score": 1,
"_source":
{
"buildNum": 15523
}
}]
}
}
took:整个搜索请求花费了多少毫秒
hits.total:本次搜索,返回了几条结果 hits.max_score:本次搜索的所有结果中,最大的相关度分数是多少,每一条document对于search的相关度,越相关,_ score分数越大,排位越靠前 hits.hits:默认查询前10条数据,每一个document的完整数据,按照_ score降序排序
shards:shards fail的条件(primary和replica全部挂掉),不影响其他shard。默认情况下来说,一个搜索请求,会打到一个index的所有primary shard上去,当然了,每个primary shard都可能会有一个或多个replic shard,所以请求也可以到primary shard对应的其中一个replica shard上去。
timeout:默认无timeout,latency平衡completeness,手动指定timeout,timeout查询执行机制
2、我们来讲解一下,搜索的timeout机制,底层的原理,画图讲解
默认情况下,在你没有设置所谓的timeout时,是没有超时限制的,比如说你的搜索特别慢,每个shard需要花费很长时间才能查询出来请求的数据,那么你的搜索请求会一直等到查询出来才会返回。
一般业务上的搜索请求,是对时间很敏感的,要考虑到用户的体验度,不可能让用户等待几分钟,甚至十几分钟才能等到搜索请求的结果。
timeout机制:指定每个shard,在timeout时间范围内,将搜索的部分数据(也可能已经全部搜索到了),直接返回给应用程序,不用长时间的查询,等到全部数据搜索出来再返回。
就是说,一次搜索请求可以在指定的timeout时间完成,为一些对时间敏感的业务搜索提供更好的支持。
指定timeout
timeout=10ms,timeout=1s,timeout=1m
GET /_search?timeout=10s
内,将搜索的部分数据(也可能已经全部搜索到了),直接返回给应用程序,不用长时间的查询,等到全部数据搜索出来再返回。
就是说,一次搜索请求可以在指定的timeout时间完成,为一些对时间敏感的业务搜索提供更好的支持。
指定timeout
timeout=10ms,timeout=1s,timeout=1m
GET /_search?timeout=10s