导入测试数据:

POST /_bulk
{ "create": { "_index": "us", "_type": "user", "_id": "1" }}
{ "email" : "john@smith.com", "name" : "John Smith", "username" : "@john" }
{ "create": { "_index": "us", "_type": "user", "_id": "2" }}
{ "email" : "mary@jones.com", "name" : "Mary Jones", "username" : "@mary" }
{ "create": { "_index": "us", "_type": "tweet", "_id": "3" }}
{ "date" : "2014-09-13", "name" : "Mary Jones", "tweet" : "Elasticsearch means full text search has never been so easy", "user_id" : 2 }
{ "create": { "_index": "us", "_type": "tweet", "_id": "4" }}
{ "date" : "2014-09-14", "name" : "John Smith", "tweet" : "@mary it is not just text, it does everything", "user_id" : 1 }
{ "create": { "_index": "us", "_type": "tweet", "_id": "5" }}
{ "date" : "2014-09-15", "name" : "Mary Jones", "tweet" : "However did I manage before Elasticsearch?", "user_id" : 2 }
{ "create": { "_index": "us", "_type": "tweet", "_id": "6" }}
{ "date" : "2014-09-16", "name" : "John Smith",  "tweet" : "The Elasticsearch API is really easy to use", "user_id" : 1 }
{ "create": { "_index": "us", "_type": "tweet", "_id": "7" }}
{ "date" : "2014-09-17", "name" : "Mary Jones", "tweet" : "The Query DSL is really powerful and flexible", "user_id" : 2 }
{ "create": { "_index": "us", "_type": "tweet", "_id": "8" }}
{ "date" : "2014-09-18", "name" : "John Smith", "user_id" : 1 }
{ "create": { "_index": "us", "_type": "tweet", "_id": "9" }}
{ "date" : "2014-09-19", "name" : "Mary Jones", "tweet" : "Geo-location aggregations are really cool", "user_id" : 2 }
{ "create": { "_index": "us", "_type": "tweet", "_id": "10" }}
{ "date" : "2014-09-20", "name" : "John Smith", "tweet" : "Elasticsearch surely is one of the hottest new NoSQL products", "user_id" : 1 }
{ "create": { "_index": "us", "_type": "tweet", "_id": "11" }}
{ "date" : "2014-09-21", "name" : "Mary Jones", "tweet" : "Elasticsearch is built for the cloud, easy to scale", "user_id" : 2 }
{ "create": { "_index": "us", "_type": "tweet", "_id": "12" }}
{ "date" : "2014-09-22", "name" : "John Smith", "tweet" : "Elasticsearch and I have left the honeymoon stage, and I still love her.", "user_id" : 1 }
{ "create": { "_index": "us", "_type": "tweet", "_id": "13" }}
{ "date" : "2014-09-23", "name" : "Mary Jones", "tweet" : "So yes, I am an Elasticsearch fanboy", "user_id" : 2 }
{ "create": { "_index": "us", "_type": "tweet", "_id": "14" }}
{ "date" : "2014-09-24", "name" : "John Smith", "tweet" : "How many more cheesy tweets do I have to write?", "user_id" : 1 }

1:size+from浅分页

按照一般的查询流程来说,如果我想查询前10条数据:

· 1 客户端请求发给某个节点

· 2 节点转发给个个分片,查询每个分片上的前10条

· 3 结果返回给节点,整合数据,提取前10条

· 4 返回给请求客户端

那么当我想要查询第10条到第20条的数据该怎么办呢?这个时候就用到分页查询了。

浅分页可以理解为简单意义上的分页。它的原理很简单,就是查询前20条数据,然后截断前10条,只返回10-20的数据。

列子:查找第5条到第10条的数据:

GET /us/_search?pretty
{
"from" : 5 , "size" : 5
 
}

from**定义了目标数据的偏移值,size定义当前返回的事件数目**

"from" : 5 , "size" : 5意思就是说:从第5条开始,一直查询到第10条

【注意】这种浅分页只适合少量数据,因为随from增大,查询的时间就会越大,而且数据量越大,查询的效率指数下降

优点:from+size在数据量不大的情况下,效率比较高

缺点:在数据量非常大的情况下,from+size分页会把全部记录加载到内存中,这样做不但运行速递特别慢,而且容易让es出现内存不足而挂掉

2:scroll“深”分页

对于上面介绍的浅分页,当Elasticsearch响应请求时,它必须确定docs的顺序,排列响应结果。

如果请求的页数较少(假设每页20个docs), Elasticsearch不会有什么问题;

但是如果页数较大时,比如请求第20页,Elasticsearch不得不取出第1页到第20页的所有docs,再去除第1页到第19页的docs,得到第20页的docs。

解决的方式就是使用scroll,scroll就是维护了当前索引段的一份快照信息--缓存(这个快照信息是你执行这个scroll查询时的快照)。

可以把 scroll 分为初始化和遍历两步: 1、初始化时将所有符合搜索条件的搜索结果缓存起来,可以想象成快照; 2、遍历时,从这个快照里取数据;

例子:

1):初始化

GET us/_search?scroll=3m
{
"query": {"match_all": {}},
  "size": 3
}

初始化的时候就像是普通的search一样 其中的scroll=3m代表当前查询的数据缓存3分钟 Size:3 代表当前查询3条数据

2):遍历

在遍历时候,拿到上一次遍历中的_scroll_id,然后带scroll参数,重复上一次的遍历步骤,知道返回的数据为空,就表示遍历完成

GET /_search/scroll
{
 "scroll" : "1m",
 "scroll_id" : "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAADiVFmc0QlJqSzhnUUhXT3ZiQjl2c2h5N3cAAAAAAAA71RZyNFJxSU1vOFJZQ2VRcVBHLXJvb29nAAAAAAAAOJQWZzRCUmpLOGdRSFdPdmJCOXZzaHk3dwAAAAAAADiTFmc0QlJqSzhnUUhXT3ZiQjl2c2h5N3cAAAAAAAA4lhZnNEJSaks4Z1FIV092YkI5dnNoeTd3"
}

【注意】:每次都要传参数scroll,刷新搜索结果的缓存时间,另外不需要指定index和type

不要把缓存的时时间设置太长,占用内存

最新文章

  1. SFC的OAM管理框架
  2. 让wego微购购物分享系统采集拍拍数据功能之腾讯paipai功能采集插件
  3. python入门练习题2
  4. PL/SQL查询Oracle数据乱码/Oracle客户端乱码解决办法
  5. 解释型语言和编译型语言如何交互?以lua和c为例
  6. 排序 归并排序&逆序对
  7. Fragment使用问题
  8. ssh远程登录linux live系统
  9. ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统
  10. jQuery: Callbacks
  11. php 可逆加密方法
  12. spring boot 2.0 整合 elasticsearch6.5.3,spring boot 2.0 整合 elasticsearch NoNodeAvailableException
  13. node20180927
  14. linux取IP的几个方法
  15. 学习笔记TF038:实现估值网络
  16. Java面试题以及答案精选(架构师面试题)-基础题1
  17. QT 小总结
  18. linux -- 查看磁盘空间的大小
  19. SQL - 常用的特殊查询
  20. plot 函数常用参数

热门文章

  1. springcloud用法
  2. C++中setiosflags()的用法
  3. php之CGI、FastCGI、php-fpm运行原理
  4. 05、解剖CEL文件各版本格式和读取方法(非R语言)
  5. Linux中如何添加/删除FTP用户并设置权限
  6. Oracle常用函数(3)
  7. 机器学习-非线性回归(Logistic Regression)及应用
  8. Jmeter分布式测试dubbo接口2
  9. PAT Advanced 1033 To Fill or Not to Fill (25 分)
  10. Linux日常之命令tee