search内核原理

doc value

搜索的时候,要依靠倒排索引;排序的时候,需要依靠正排索引,看到每个document的每个field,然后进行排序,所谓的正排索引,其实就是doc values
在建立索引的时候,一方面会建立倒排索引,以供搜索用;一方面会建立正排索引,也就是doc values,以供排序,聚合,过滤等操作使用
doc values是被保存在磁盘上的,此时如果内存足够,os会自动将其缓存在内存中,性能还是会很高;如果内存不足够,os会将其写入磁盘上
如下两个doc
 
doc1: hello world you and me doc2: hi, world, how are you
倒排索引
word doc1 doc2 hello * world * * you * * and * me * hi * how * are *
搜索hello you 会分词为 hello和you
hello 查询关联到 doc1 you 查询关联到 doc1,doc2
 

正排索引

doc1: hello world you and me doc2: hi, world, how are you
sort by age
doc1: { "name": "jack", "age": 27 } doc2: { "name": "tom", "age": 30 }
 
document
name
age
doc1
jack
27
doc2
tom
30

query phase

query phase

fetch phase

fetch phase

preference参数

决定了哪些shard会被用来执行搜索操作
_primary, _primary_first, _local, _only_node:xyz, _prefer_node:xyz, _shards:2,3
bouncing results问题,两个document排序,field值相同;不同的shard上,可能排序不同;每次请求轮询打到不同的replica shard上;每次页面上看到的搜索结果的排序都不一样。这就是bouncing result,也就是跳跃的结果。
搜索的时候,是轮询将搜索请求发送到每一个replica shard(primary shard),但是在不同的shard上,可能document的排序不同
解决方案就是将preference设置为一个字符串,比如说user_id,让每个user每次搜索的时候,都使用同一个replica shard去执行,就不会看到bouncing results了
2、timeout,已经讲解过原理了,主要就是限定在一定时间内,将部分获取到的数据直接返回,避免查询耗时过长
3、routing,document文档路由,_id路由,routing=user_id,这样的话可以让同一个user对应的数据到一个shard上去
4、search_type
default:query_then_fetch dfs_query_then_fetch,可以提升revelance sort精准度