Elasticsearch笔记第十六篇

114 阅读4分钟

Elasticsearch核心知识篇(34)

初识搜索引擎_search结果深入解析(search timeout机制揭秘)

1、我们如果发出一个搜索请求的话,会拿到一堆搜索结果,本节课,我们来讲解一下,这个搜索结果里的各种数据,都代表了什么含义 2、我们来讲解一下,搜索的timeout机制,底层的原理,画图讲解

GET /_search

{
  "took": 45,
  "timed_out": false,
  "_shards": {
    "total": 21,
    "successful": 21,
    "failed": 0
  },
  "hits": {
    "total": 13,
    "max_score": 1,
    "hits": [
      {
        "_index": ".kibana",
        "_type": "config",
        "_id": "5.2.0",
        "_score": 1,
        "_source": {
          "buildNum": 14695
        }
      },
      {
        "_index": "waws_test",
        "_type": "waws_type",
        "_id": "8",
        "_score": 1,
        "_source": {
          "waws": "waws"
        }
      }
    ]
  }
}
  • 返回数据的含义:

    • took:整个搜索请求花费了多少毫秒
    • hits.total:本次搜索,返回了几条结果
    • hits.max_score:本次搜索的所有结果中,最大的相关度分数是多少,每一条document对于search的相关度,越相关,_score分数越大,排位越靠前
    • hits.hits:默认查询前10条数据,完整数据,_score降序排序
    • shards:shards fail的条件(primary和replica全部挂掉),不影响其他shard。默认情况下来说,一个搜索请求,会打到一个index的所有primary shard上去,当然了,每个primary shard都可能会有一个或多个replic shard,所以请求也可以到primary shard的其中一个replica shard上去。
    • timeout:默认无timeout,latency平衡completeness,手动指定timeout,timeout查询执行机制
  • timeout=10mstimeout=1stimeout=1m

    • GET /_search?timeout=10m
    • 机制:指定每个shard,就只能在指定的时间范围内,将搜索到的部分数据(也有可能是全部都搜索到了),直接立即返回给client程序,而不是等到所有的数据都搜索出来在将数据返回
    • 确保说,一次搜索请求在用户指定的timeout的时长中完成。为一些时间敏感的搜索应用提供良好的支持。

image.png

Elasticsearch核心知识篇(35)

初识搜索引擎_multi-index&multi-type搜索模式解析以及搜索原理初步图解

multi-indexmulti-type搜索模式

告诉你如何一次性搜索多个index和多个type下的数据

  • /_search:所有索引,所有type下的所有数据都搜索出来

  • /index1/_search:指定一个index,搜索其下所有type的数据

    GET /waws_index/_search
    
    {
      "took": 1,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 3,
        "max_score": 1,
        "hits": [
          {
            "_index": "waws_index",
            "_type": "waws_type",
            "_id": "2",
            "_score": 1,
            "_source": {
              "name": "waws2"
            }
          },
          {
            "_index": "waws_index",
            "_type": "waws_type",
            "_id": "4",
            "_score": 1,
            "_source": {
              "waws_name": "waws4"
            }
          },
          {
            "_index": "waws_index",
            "_type": "waws_type",
            "_id": "3",
            "_score": 1,
            "_source": {
              "waws_name": "update_waws_name"
            }
          }
        ]
      }
    }
    
  • /index1,index2/_search:同时搜索两个index下的数据

  • /*1,*2/_search:按照通配符去匹配多个索引

  • /index1/type1/_search:搜索一个index下指定的type的数据

    GET /waws_index/waws_type/_search
    
    {
      "took": 1,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 3,
        "max_score": 1,
        "hits": [
          {
            "_index": "waws_index",
            "_type": "waws_type",
            "_id": "2",
            "_score": 1,
            "_source": {
              "name": "waws2"
            }
          },
          {
            "_index": "waws_index",
            "_type": "waws_type",
            "_id": "4",
            "_score": 1,
            "_source": {
              "waws_name": "waws4"
            }
          },
          {
            "_index": "waws_index",
            "_type": "waws_type",
            "_id": "3",
            "_score": 1,
            "_source": {
              "waws_name": "update_waws_name"
            }
          }
        ]
      }
    }
    
  • /index1/type1,type2/_search:可以搜索一个index下多个type的数据

  • /index1,index2/type1,type2/_search:搜索多个index下的多个type的数据

  • /_all/type1,type2/_search:_all,可以代表搜索所有index下的指定type的数据

初步图解一下简单的搜索原理

  • client发送一个请求,会把所有请求打到所有的primary shard上去执行,因为每个shard都包含部分数据,所以每个shard上都可能会包含搜索请求的结果 image.png

Elasticsearch核心知识篇(36)

初识搜索引擎_分页搜索以及deep paging性能问题深度图解揭秘

分页搜索

 size,from
 
 GET /_search?size=10
 GET /_search?size=10&from=0
 GET /_search?size=10&from=20
  • 分页的上机实验
 GET /waws_index/waws_type/_search
 
 {
   "took": 1,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 3,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws_index",
         "_type": "waws_type",
         "_id": "2",
         "_score": 1,
         "_source": {
           "name": "waws2"
         }
       },
       {
         "_index": "waws_index",
         "_type": "waws_type",
         "_id": "4",
         "_score": 1,
         "_source": {
           "waws_name": "waws4"
         }
       },
       {
         "_index": "waws_index",
         "_type": "waws_type",
         "_id": "3",
         "_score": 1,
         "_source": {
           "waws_name": "update_waws_name"
         }
       }
     ]
   }
 }

我们假设将这3条数据分成3页,每一页是3条数据,来实验一下这个分页搜索的效果

 GET /waws_index/waws_type/_search?from=0&size=1
 
 {
   "took": 1,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 3,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws_index",
         "_type": "waws_type",
         "_id": "2",
         "_score": 1,
         "_source": {
           "name": "waws2"
         }
       }
     ]
   }
 }
 
 结果展示:
 第一页:id=2
 
 GET /waws_index/waws_type/_search?from=1&size=1
 第二页:id=4
 
 GET /waws_index/waws_type/_search?from=2&size=1
 第三页:id=3

deep paging

  • 什么是deep paging问题?为什么会产生这个问题,它的底层原理是什么?

    • deep paging性能问题,以及原理深度图解揭秘,很高级的知识点

深度理解:

  • 首先是我们的请求发送到一个不包含这个index的node上去,这个node就是我们的候选节点,然后候选节点会将这个请求发送到这个index包含的三个shard上去(可以全是primary shard,也可以是primary+replice shard 所在的node上去)
  • 每个shard上都是10000条数据,我们的size设置的是10,from设置的也是10,这样我们从三个shard中获得的数据是10 * 10 + 0 ~ 10 * 10 + 9的数据,也就是100---109的数据,实际上从shard上返回给coordinate node的数据是0--109,三个shard工110 * 3=330条数据,在coordinate node上在进行排序,取出排名为100--109的序号的数据,最终返回给前端,实际上涉及到的数据总量是 ((size * from) + size) * shard,进行综合排名。
  • 搜索过深的时候,就需要在coordinate node上保存大量的数据,还要进行大量数据的排序,排序之后,在取出来对应的那一页。所以这个过程,即消耗网络带宽,还要消耗cpu。尽量避免出现这种deep paging的操作。

image.png