大批量拉取ES数据,es索引过多,scroll_id超长,如何处理?

scroll_id过长,返回数据给前端会出问题,后端也会出现请求过长的问题,因此给后端大数据压缩后的scroll_id

    private function scrollQuery(){

        $scroll_id = $this->requestData['data']['scroll_id'];
        $model = new LogCenterModel();

        if($scroll_id){
            $data = $model->scrollQuery($scroll_id);
        }else{
            $param = $this->getAttackLogParam();
            if(intval($param['size'])>1000){
                $this->error('最多请求1000条数据');
            }
            $data = $model->scrollQuery('',$param);
        }

        $data['scroll_id'] = $data['scroll_id_key'];
        unset($data['scroll_id_key']);
        $this->printJson(1,$data);
    }
    
    
     /**
     * 使用 scroll_id 进行翻页查询
     * @param string|null $scroll_id 上一次查询返回的 scroll_id,第一次查询时传入 null
     * @param array $params 其他查询参数
     * @return array 包含下一页数据和新的 scroll_id 的响应数组
     */

    public function scrollQuery($scroll_id_key = '', $params = [])
    {
        $client = ClientBuilder::create()->setHosts([WEBACCESSLOG_ES])->build();
        $redis = self::Redis(8);

        if (empty($scroll_id_key)) {
            // 第一次查询,执行 search 操作
            $query = $this->extractedWebAccess($params);
            $result = $client->search($query);
        } else {
            // 后续查询,从 Redis 中获取 scroll_id
            $scroll_id = $redis->get($scroll_id_key);
            if (empty($scroll_id)) {
                // scroll_id 已过期或不存在
                return [
                    'list' => [],
                    'scroll_id_key' => '',
                ];
            }
            $scroll_id = gzuncompress($scroll_id);
            $result = $client->scroll([
                "scroll_id" => $scroll_id,
                "scroll" => "60s", // 保持 scroll 上下文有效的时间
            ]);
        }

        $hits = $result['hits']['hits'];
        $compressed_scroll_id = gzcompress($result['_scroll_id']);

        // 将压缩后的 scroll_id 存储到 Redis 中,并设置过期时间为 60 秒
        // 生成一个唯一的键值
        $scroll_id_key = uniqid('scroll_', true);
        $redis->setex($scroll_id_key, 60, $compressed_scroll_id);

        $web_access_protect_module = LogCenterModel::getProtectModule('strategy', 'web_access');

        $response = [
            'list' => $this->formatWebAccessLog($hits, $web_access_protect_module),
            'scroll_id_key' => $scroll_id_key,
        ];

        return $response;
    }
所有用户都可以去薅羊毛,192元充值200元话费!先到先得!导航栏话费充值,正规可靠,快充慢充自由选择。
欧阳逸资源站 » 大批量拉取ES数据,es索引过多,scroll_id超长,如何处理?

发表评论