Elasticsearch由浅入深(七)搜索引擎:_search含义、_multi-index搜索模式、分页搜索以及深分页性能问题、query string search语法以及_all metadata原理
_search含义
_search查询返回结果数据含义分析
GET _search
{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"failed":
},
"hits": {
"total": ,
"max_score": ,
"hits": [
{
"_index": ".kibana",
"_type": "config",
"_id": "5.2.0",
"_score": ,
"_source": {
"buildNum":
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "AWypxxLYFCl_S-ox4wvd",
"_score": ,
"_source": {
"test_content": "my test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": ,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_doc",
"_id": "",
"_score": ,
"_source": {
"test_field": "test10 routing _id"
}
},
{
"_index": "test_index",
"_type": "test_doc",
"_id": "",
"_score": ,
"_routing": "",
"_source": {
"test_field": "test routing not _id"
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "",
"_score": ,
"_source": {
"name": "jiajieshi yagao",
"desc": "youxiao fangzhu",
"price": ,
"producer": "jiajieshi producer",
"tags": [
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "",
"_score": ,
"_source": {
"name": "special yagao",
"desc": "special meibai",
"price": ,
"producer": "special yagao producer",
"tags": [
"meibai"
]
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": ,
"_source": {
"test_field": "test test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": ,
"_source": {
"test_field": "test4"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": ,
"_source": {
"test_field": "replaces test2"
}
}
]
}
}
- took: 整个搜索请求花费了多少毫秒
- timed_out:表示请求是否超时
- hits:total:value表示返回结果的总数,relation表示关系 例如一般是eq表示相等
- hits:max_score: 表示本次搜索的所有结果中,最大的相关度分数是多少,每一条document对于search的相关度,越相关,_score分数就越大,排位就越靠前
- hits:hits: 表示查询出来document的结果集合
- shards:total表示打到的所有分片,
- shards:successful表示打到的分片中查询成功的分片,
- shards:skipped表示打到的分片中跳过的分片,
- shards:failed表示打到的分片中查询失败的分片
search timeout机制
因为ES默认是没有timeout的,所以先描述一下场景假设我们有些搜索应用,对时间是很敏感的,比如电商网站,你不能让用户等个10分钟,如果那样的话,人家早就走了,不来买东西了。
于是我们就需要有timeout机制,指定每个shard,就只能在timeout时间范围内,将搜索到的部分数据(也可能全都搜索到了),直接返回给客户端,而不是等到所有数据全都搜索出来以后在返回。
这样就可以确保说,一次搜索请求可以在用户指定的timeout时长内完成,为一些时间敏感的搜索应用提供良好的支持。
注意:ES在默认情况下是没有所谓的timeout的,比如说如果你的搜索特别慢,每个shard都要花好几分钟才能查询出来所有的数据,那么你的搜索请求也会等待好几分钟之后才会返回。
下面画图简单描述一下timeout机制

语法:
GET _search?timeout=10ms
_multi-index&multi-type搜索模式
先说明一下,低版本的ES一个index是支持多type的,所以就有multi-type这一种搜索模式,这里不做详细讲解,因为和multi-index搜索模式是基本一样的。而且高版本的ES会弃用type。
multi-index搜索模式
- /_search:所有索引下的所有数据都搜索出来
GET /_search
- /{index}/_search:指定一个index,搜索这个索引下的所有数据
GET /test/_search
- /index1,index2/_search:同时搜索两个索引下的数据
GET /test_index,test/_search
- /1,2/_search: 通过通配符匹配多个索引,查询多个索引下的数据
GET /test*/_search
- /_all/_search: 代表所有的index
GET /_all/_search
搜索原理浅析
当客户端发送查询请求到ES时,会把请求打到所有的primary shard上去执行,因为每个shard都包含部分数据,所有每个shard都可能会包含搜索请求的结果,但是如果primary shard有replica shard,那么请求也可以打到replica shard上去。
如下图所示:

分页搜索以及deep paging性能揭秘
在实际应用中,分页是必不可少的,例如,前端页面展示数据给用户往往都是分页进行展示的。
ES分页搜索
Elasticsearch分页搜索采用的是from+size。from表示查询结果的起始下标,size表示从起始下标开始返回文档的个数。
示例:
GET test_index/test_type/_search?from=&size=
{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"failed":
},
"hits": {
"total": ,
"max_score": ,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "AWypxxLYFCl_S-ox4wvd",
"_score": ,
"_source": {
"test_content": "my test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": ,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": ,
"_source": {
"test_field": "test test"
}
}
]
}
}
深分页性能问题
什么是深分页(deep paging)?简单来说,就是搜索的特别深,比如总共有60000条数据,三个primary shard,每个shard上分了20000条数据,每页是10条数据,这个时候,你要搜索到第1000页,实际上要拿到的是10001~10010。
注意这里千万不要理解成每个shard都是返回10条数据。这样理解是错误的!
下面做一下详细的分析:
请求首先可能是打到一个不包含这个index的shard的node上去,这个node就是一个协调节点coordinate node,那么这个coordinate node就会将搜索请求转发到index的三个shard所在的node上去。比如说我们之前说的情况下,要搜索60000条数据中的第1000页,实际上每个shard都要将内部的20000条数据中的第10001~10010条数据,拿出来,不是才10条,是10010条数据。3个shard的每个shard都返回10010条数据给协调节点coordinate node,coordinate node会收到总共30030条数据,然后在这些数据中进行排序,根据_score相关度分数,然后取到10001~10010这10条数据,就是我们要的第1000页的10条数据。
如下图所示:

deep paging问题就是说from + size分页太深,那么每个shard都要返回大量数据给coordinate node协调节点,会消耗大量的带宽,内存,CPU。
query string search语法以及_all metadata原理
query string基础语法
GET /test_index/test_type/_search?q=test_field:test
{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"failed":
},
"hits": {
"total": ,
"max_score": 0.843298,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": 0.843298,
"_source": {
"test_field": "test test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": 0.43445712,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": 0.25316024,
"_source": {
"test_field": "test client 1"
}
}
]
}
}
GET /test_index/test_type/_search?q=+test_field:test
{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"failed":
},
"hits": {
"total": ,
"max_score": 0.843298,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": 0.843298,
"_source": {
"test_field": "test test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": 0.43445712,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": 0.25316024,
"_source": {
"test_field": "test client 1"
}
}
]
}
}
GET /test_index/test_type/_search?q=-test_field:test
{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"failed":
},
"hits": {
"total": ,
"max_score": ,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "AWypxxLYFCl_S-ox4wvd",
"_score": ,
"_source": {
"test_content": "my test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": ,
"_source": {
"test_field": "test4"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": ,
"_source": {
"test_field": "replaces test2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": ,
"_source": {
"test_field1": "test field1",
"test_field2": "partial updated test1"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": ,
"_source": {
"num": ,
"tags": []
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": ,
"_source": {
"test_field": "test3"
}
}
]
}
}
对于query string只要掌握q=field:search content的语法,以及+和-的含义
- +:代表包含这个筛选条件结果
- -:代表不包含这个筛选条件的结果
_all metadata
GET /test_index/test_type/_search?q=test
{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"failed":
},
"hits": {
"total": ,
"max_score": 0.843298,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": 0.843298,
"_source": {
"test_field": "test test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "AWypxxLYFCl_S-ox4wvd",
"_score": 0.3794414,
"_source": {
"test_content": "my test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": 0.31387395,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": 0.18232156,
"_source": {
"test_field": "test client 1"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "",
"_score": 0.16203022,
"_source": {
"test_field1": "test field1",
"test_field2": "partial updated test1"
}
}
]
}
}
也就是在使用query string的时候,如果不指定field,那么默认就是_all。_all元数据是在建立索引的时候产生的,我们插入一条document,它里面包含了多个field,此时ES会自动将多个field的值全部用字符串的方式串联起来,变成一个长的字符串。这个长的字符串就是_all field的值。同时建立索引。
举个例子:
对于一个document:
{
"name": "jack",
"age": ,
"email": "jack@sina.com",
"address": "guamgzhou"
}
那么"jack 26 jack@sina.com guamazhou",就会作为这个document的_all fieldd的值,同时进行分词后建立对应的倒排索引。
注意在生产环境中一般不会使用query string这种查询方式。
Elasticsearch由浅入深(七)搜索引擎:_search含义、_multi-index搜索模式、分页搜索以及深分页性能问题、query string search语法以及_all metadata原理的更多相关文章
- ES 25 - Elasticsearch的分页查询及其深分页问题 (deep paging)
目录 1 分页查询方法 2 分页查询的deep paging问题 1 分页查询方法 在GET请求中拼接from和size参数 // 查询10条数据, 默认从第0条数据开始 GET book_shop/ ...
- Elasticsearch由浅入深(一)
什么是Elasticsearch 什么是搜索 百度:我们比如说想找寻任何的信息的时候,就会上百度去搜索一下,比如说找一部自己喜欢的电影,或者说找一本喜欢的书,或者找一条感兴趣的新闻(提到搜索的第一印象 ...
- Elasticsearch from/size-浅分页查询-深分页 scroll-深分页search_after深度查询区别使用及应用场景
Elasticsearch调研深度查询 1.from/size 浅分页查询 一般的分页需求我们可以使用from和size的方式实现,但是这种的分页方式在深分页的场景下应该是避免使用的.深分页的页次增加 ...
- Elasticsearch由浅入深(十)搜索引擎:相关度评分 TF&IDF算法、doc value正排索引、解密query、fetch phrase原理、Bouncing Results问题、基于scoll技术滚动搜索大量数据
相关度评分 TF&IDF算法 Elasticsearch的相关度评分(relevance score)算法采用的是term frequency/inverse document frequen ...
- Elasticsearch由浅入深(九)搜索引擎:query DSL、filter与query、query搜索实战
search api的基本语法 语法概要: GET /_search {} GET /index1,index2/type1,type2/_search {} GET /_search { , } h ...
- ELK(elasticsearch+kibana+logstash)搜索引擎(二): elasticsearch基础教程
1.elasticsearch的结构 首先elasticsearch目前的结构为 /index/type/id id对应的就是存储的文档ID,elasticsearch一般将数据以JSON格式存储. ...
- Elasticsearch笔记七之setting,mapping,分片查询方式
Elasticsearch笔记七之setting,mapping,分片查询方式 setting 通过setting可以更改es配置可以用来修改副本数和分片数. 1:查看,通过curl或浏览器可以看到副 ...
- 使用Node,Vue和ElasticSearch构建实时搜索引擎
(译者注:相关阅读:node.js,vue.js,Elasticsearch) 介绍 Elasticsearch是一个分布式的RESTful搜索和分析引擎,能够解决越来越多的用例. Elasticse ...
- 转 Solr vs. Elasticsearch谁是开源搜索引擎王者
转 https://www.cnblogs.com/xiaoqi/p/6545314.html Solr vs. Elasticsearch谁是开源搜索引擎王者 当前是云计算和数据快速增长的时代,今天 ...
随机推荐
- 使用 jQuery.TypeAhead 让文本框自动完成 (四)(自定义模板)
项目地址:https://github.com/twitter/typeahead.js 直接贴代码了: @section headSection { <script type="te ...
- python实现词云
一.安装使用命令[pip install wordcloud]安装词云 二.参数使用了OpenCV的数据格式进行读取,字体可以多试几种 def create_wordcloud_pic(): stop ...
- Elasticsearch(ES) 创建索引
欢迎关注笔者的公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site/ ...
- 面试题深入解析:Synchronized底层实现
本文为synchronized系列第二篇.主要内容为分析偏向锁的实现. 偏向锁的诞生背景和基本原理在上文中已经讲过了,强烈建议在有看过上篇文章的基础下阅读本文. 本文将分为几块内容: 1.偏向锁的入口 ...
- MySQL5.7增量备份恢复全实战
一. 简介 1. 增量备份 增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份与前一次相比增加或者被修改的文件.这就意味着,第一次增量 备份的对象是进行全备后所产生的增加和修改的文件; ...
- 小鸟初学Shell编程(四)管道符
管道作用 管道实际上就是进程之间的一个通信工具,那么用在Linux命令中主要是方便两条命令互相之间可以相互通信. 管道符 管道符(匿名管道)是Shell编程经常用到的通信工具. 管道符是"| ...
- MES助力日立电梯提升精细化管理水平
项目背景介绍 日立电梯在2008年到2012年期间分别在五地工厂(上海.广州.天津.成都.扶梯)上线了ERP系统,在后续的使用时间里,逐渐发现现有ERP系统对于生产现场管理,产品质量追溯,产能控制等方 ...
- Ubuntu16.04的搭建l.2.t.p.d(宿舍访问公司内网)
主要的实现步骤 openswan(ipsec) : 提供一个密钥 ppp :提供用户名和密码 xl2tpd : 提供L2TP服务 sysctl : 提供服务器内部转发 iptables : 提供请求从 ...
- vue学习指南:第六篇(详细) - Vue的组件 component
1. 什么是组件?有两种解释 1. 第一种解释: 什么是组件? 1. 组件是 vue 中的一个可复用的实例,所以new Vue() 是vue中最大的那个组件(根组件),有名字,使用的时候以单标签或双标 ...
- nrm : 无法加载文件 C:\Users\TANG\AppData\Roaming\npm\nrm.ps1,因为在此系统上禁止运行脚本。
1.win+s 搜索powershell 以管理身份运行 2.使用set-ExecutionPolicy RemoteSigned命令将计算机上的执行策略更改为 RemoteSigned,输入Y确定 ...