_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. Mysql 二进制日志备份还原

    Mysql 二进制日志备份还原 一.开启二进制日志 1.进入配置文件[mysqld]下添加配置 方案一 vim /etc/my.cnf log-bin = /usr/local/mysql/logs/ ...

  2. 机器学习(九)-------- 聚类(Clustering) K-均值算法 K-Means

    无监督学习 没有标签 聚类(Clustering) 图上的数据看起来可以分成两个分开的点集(称为簇),这就是为聚类算法. 此后我们还将提到其他类型的非监督学习算法,它们可以为我们找到其他类型的结构或者 ...

  3. SQL Server like 字段

    参考资料:Cannot resolve the collation conflict between "Chinese_PRC_CI_AS" and "SQL_L及由于排 ...

  4. echarts白色实心环形图(空心饼图)的编写

    // 数据接入机构统计let myDom = document.getElementById('myChart');let myWidth = myDom.offsetWidth - 5; // 获取 ...

  5. SPC软控件提供商NWA的产品在各行业的应用(石油天然气行业)

    Northwest Analytical (NWA)是全球领先的“工业4.0”制造分析SPC软件控件提供商.产品(包含: NWA Quality Analyst , NWA Focus EMI 和 N ...

  6. QString判断空 isEmpty

    isEmpty Returns true if the string has no characters; otherwise returns false. QString().isEmpty(); ...

  7. 带你理解Xcode Derived Data

    什么是Xcode Derived Data?为什么它很重要呢? “Clean derived data”,当你遇到一些极其奇怪的构建问题时,你也许经常听到这句话. Derived Data是一个文件夹 ...

  8. vuejs兄弟通信$emit和$on

    1   vm.$on( event, callback ) 监听当前实例上的自定义事件.事件可以由vm.$emit触发.回调函数会接收所有传入事件触发函数的额外参数. 2 vm.$emit( even ...

  9. Docker 清理日志

    docker 长时间运行后,日志文件会逐渐变大可以使用下面命令进行清除 #!/bin/bash echo "==================== start clean docker c ...

  10. nodejs实现简单爬虫

    nodejs结合cheerio实现简单爬虫 let cheerio = require("cheerio"), fs = require("fs"), util ...