PHP7 Elasticsearch scroll常见错误

本文介绍了使用PHP7查询Elasticsearch7时遇到的两个常见错误及其解决方案:一是生成过多scroll contexts;二是scroll_id失效。文章提供了具体的代码示例来说明如何通过清除scroll_id来解决这些问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景:使用PHP7查询Elasticsearch7的数据

错误1:Elasticsearch 7.0.1 - Trying to create too many scroll contexts. Must be less than or equal to: [500]

原因:在search($params)中设置的timeout时间内,累计生成的scroll_id数超过了最大限制

解决方法:查询scroll以后,删除对应的id,即调用clearScroll方法

代码如下:

$response = $this->client->search($params);   // Execute the search
        while (isset($response['hits']['hits']) && count($response['hits']['hits']) > 0) {

            // **
            // 对response内容进行处理
            // **

            // When done, get the new scroll_id
            // You must always refresh your _scroll_id!  It can change sometimes
            $scroll_id = $response['_scroll_id'];

            // Execute a Scroll request and repeat
            $response = $this->client->scroll([
                        'scroll_id' => $scroll_id,  //...using our previously obtained _scroll_id
                        'scroll'    => $timeout        // and the same timeout window
                    ]
                );

        }
        $this->client->clearScroll(array('scroll_id' => $response['_scroll_id']));

 

错误1:No search context found for id

原因:ES端的scroll_id失效,一般是1)由于PHP中的search($params)设置的size设置过大,导致数据未能及时处理。2)对response数据的处理超时

解决方法:减小size设置,或 排查response的处理耗时

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值