_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原理的更多相关文章

  1. ES 25 - Elasticsearch的分页查询及其深分页问题 (deep paging)

    目录 1 分页查询方法 2 分页查询的deep paging问题 1 分页查询方法 在GET请求中拼接from和size参数 // 查询10条数据, 默认从第0条数据开始 GET book_shop/ ...

  2. Elasticsearch由浅入深(一)

    什么是Elasticsearch 什么是搜索 百度:我们比如说想找寻任何的信息的时候,就会上百度去搜索一下,比如说找一部自己喜欢的电影,或者说找一本喜欢的书,或者找一条感兴趣的新闻(提到搜索的第一印象 ...

  3. Elasticsearch from/size-浅分页查询-深分页 scroll-深分页search_after深度查询区别使用及应用场景

    Elasticsearch调研深度查询 1.from/size 浅分页查询 一般的分页需求我们可以使用from和size的方式实现,但是这种的分页方式在深分页的场景下应该是避免使用的.深分页的页次增加 ...

  4. Elasticsearch由浅入深(十)搜索引擎:相关度评分 TF&IDF算法、doc value正排索引、解密query、fetch phrase原理、Bouncing Results问题、基于scoll技术滚动搜索大量数据

    相关度评分 TF&IDF算法 Elasticsearch的相关度评分(relevance score)算法采用的是term frequency/inverse document frequen ...

  5. Elasticsearch由浅入深(九)搜索引擎:query DSL、filter与query、query搜索实战

    search api的基本语法 语法概要: GET /_search {} GET /index1,index2/type1,type2/_search {} GET /_search { , } h ...

  6. ELK(elasticsearch+kibana+logstash)搜索引擎(二): elasticsearch基础教程

    1.elasticsearch的结构 首先elasticsearch目前的结构为 /index/type/id  id对应的就是存储的文档ID,elasticsearch一般将数据以JSON格式存储. ...

  7. Elasticsearch笔记七之setting,mapping,分片查询方式

    Elasticsearch笔记七之setting,mapping,分片查询方式 setting 通过setting可以更改es配置可以用来修改副本数和分片数. 1:查看,通过curl或浏览器可以看到副 ...

  8. 使用Node,Vue和ElasticSearch构建实时搜索引擎

    (译者注:相关阅读:node.js,vue.js,Elasticsearch) 介绍 Elasticsearch是一个分布式的RESTful搜索和分析引擎,能够解决越来越多的用例. Elasticse ...

  9. 转 Solr vs. Elasticsearch谁是开源搜索引擎王者

    转 https://www.cnblogs.com/xiaoqi/p/6545314.html Solr vs. Elasticsearch谁是开源搜索引擎王者 当前是云计算和数据快速增长的时代,今天 ...

随机推荐

  1. locally weighted regression - CS229

    欠拟合和过拟合 看下方的三张图 第一幅拟合为了 y=θ0+θ1xy=θ0+θ1x 的一次函数 第二幅拟合为了y=θ0+θ1x+θ2x2y=θ0+θ1x+θ2x2 的二次函数 第三幅拟合为了 y=∑5j ...

  2. Spring源码系列 — Envoriment组件

    何为Envoriment Envoriment是集成在Spring上下文容器中的核心组件,在Spring源码中由Envoriment接口抽象. 在Environment中,有两大主要概念: Profi ...

  3. 2019年JVM最新面试题,必须收藏它

    1.JVN内存结构 方法区和对是所有线程共享的内存区域:而java栈.本地方法栈和程序员计数器是运行是线程私有的内存区域. Java堆(Heap),是Java虚拟机所管理的内存中最大的一块.Java堆 ...

  4. MySQL快速入门及常用命令

    数据库 笔记内容 SQL语言共分为四大类:数据查询语言DQL,数据操纵语言DML,数据定义语言DDL,数据控制语言DCL. 1. 数据查询语言DQL 数据查询语言DQL基本结构是由SELECT子句,F ...

  5. CSS box-sizing属性的正确用法

    CSS自定义属性(变量) CSS box-sizing属性允许我们在元素的总宽度和高度中包含填充和边框. 没有CSS box-sizing属性 默认情况下,元素的宽度和高度计算如下: width + ...

  6. SharpGL之透视投影和摄像机

    当三维体放在世界坐标系中后,由于显示器只能用二维图像显示三维休,因此必须要依赖投影来把三维体降低维数. 投影变换的目的就是定义了一个视景体,使得视景体外多余的部分不会显示. 投影包括透视投影(pers ...

  7. 阿里云 centos7 安装MySQL8.0.13

    1.下载MySQL安装包(这里是有技巧的,说不定我这时写这个的时候版本还是你看到时的旧版本了,如果已经不是8.0了,可以根据这样来 下新版本) 先进入官网 再将这两者一结合,就是最新版本的了 所以 [ ...

  8. Innodb Change Buffer

    Change Buffer属于Innodb内存中的一块结构,它主要用来缓存对二级索引数据的修改(insert, update, delete)操作当二级索引不在Buffer pool中的时候,这些写操 ...

  9. 一不小心把windows资源管理器给结束任务了 电脑黑屏了 怎么处理

    按键盘上的三个键,Ctrl+Shift+Esc来启动任务管理器: 在任务管理器界面的左上角,有一个“文件”按钮,点击它会出现下拉的“新建任务(运行)...”按钮 鼠标点了“新建任务(运行)...”会弹 ...

  10. Excel数据导入到Sql server

    问题:数据库内直接操作导致 错误 0xc020901c: 数据流任务 1: 源 - yndata1$.输出[Excel 源输出] 上的 源 - yndata1$.输出[Excel 源输出].列[ind ...