_search.php_搜索操作 | Elasticsearch-PHP | Elastic

Scrolling(游标)查询edit

在用 bulk 时,经常要用 Scrolling 功能对文档进行分页处理,如输出一个用户的所有文档。这比常规的搜索要高效,因为这里不需要对文档执行性能消耗较大的排序操作。

Scrolling 会保留某个时间点的索引快照数据,然后用快照数据进行分页。游标查询窗口允许持续分页操作,即使后台正在执行索引文档、更新文档和删除文档。首先,你要在发送搜索请求时增加 scroll 参数。然后就会返回一个文档“页数”信息,还有一个用来获取 hits 分页数据的 scroll_id。

更多详情请查看​游标查询。

以下代码更为深入的操作的示例:

$client = ClientBuilder::create()->build();

$params = [

"scroll" => "30s", // how long between scroll requests. should be small!

"size" => 50, // how many results *per shard* you want back

"index" => "my_index",

"body" => [

"query" => [

"match_all" => new \stdClass()

]

]

];

// Execute the search

// The response will contain the first batch of documents

// and a scroll_id

$response = $client->search($params);

// Now we loop until the scroll "cursors" are exhausted

while (isset($response['hits']['hits']) && count($response['hits']['hits']) > 0) {

// **

// Do your work here, on the $response['hits']['hits'] array

// **

// 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 = $client->scroll([

"scroll_id" => $scroll_id, //...using our previously obtained _scroll_id

"scroll" => "30s" // and the same timeout window

]

);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值