我正在使用elasticsearch-php。
我使用此代码,在我做的测试中效果很好。
require_once 'app/init.php';
if (isset($_GET['q'])) {
$q = $_GET['q'];
$query= $es->search([
'index' => 'rebajas',
//'search_type' => 'scan',
'scroll' => '2m',
//'from' => 0,
'size' => 1000,
'body'=>[
'query'=>[
'bool' => [
'should' => [
'match'=>['titulo'=>$q],
]
]
]
]
]);
if ($query['hits']['total']>=1) {
$scroll_size = $query['hits']['total'];
$results = $query['hits']['hits'];
//scrol
$scroll_id = $query['_scroll_id'];
print " total results: " . $scroll_size;
$count = 0;
// first set of scroll results
for ($i=0; $i<$scroll_size; $i++) {
$count++;
}
//scroll
while (isset($query['hits']['hits']) && count($query['hits']['hits']) > 0) {
// **
// Do your work here, on the $response['hits']['hits'] array
// **
$conta =0;
foreach ($results as $r ) {
$conta++;
?>
<h1 class="mt-5"><a href=""><?php echo $conta." " .$r['_source']['titulo'];?></a> <i class="fa fa-search-plus" aria-hidden="true"></i></h1>
<?php
}
// When done, get the new scroll_id
// You must always refresh your _scroll_id! It can change sometimes
$scroll_id = $query['_scroll_id'];
// Execute a Scroll request and repeat
$query = $es->scroll([
"scroll_id" => $scroll_id, //...using our previously obtained _scroll_id
"scroll" => "30s" // and the same timeout window
]
);
}
}
}
问题是: 这个滚动如何工作,当它在生产中,提取大约一万条记录时,它是否会阻止具有如此多记录的服务器? 我怎样才能检查它是什么装? 这与无限滚动的工作方式相同,也就是说,当我沿着页面下载时,记录会被加载。
答案 0 :(得分:0)
这个问题是如何实现elasticsearch支持的无限滚动?这是一个完整的课程/框架问题。但是让我试一试。
如果您想要无限滚动,您可能希望首先从您的网站保护端点中分析您的数据API(即数据不应与您的网页一起加载[您可能希望您的第一页与您的网页一起提供]网页,但它没有和加载下一页,你几乎肯定希望将其作为自己的API])。然后当用户向下滚动你的网页时你需要你的AJAX来请求页面(即如果用户滚动到n / PAGE_SIZE然后请求下一页)。您基本上可以谷歌'无限滚动实现',但this captures the intention pretty well。
据说这通常不是弹性搜索的一个很好的用例(使用ES作为网站的直接无限滚动支持)。大多数reasons why are encapsulated here。它基本上归结为这样一个事实,即在弹性搜索中使用许多开放式卷轴是昂贵的,但如果你有很少的用户和严格的超时可能没问题呢?滚动不是用于“实时”使用,而是用于数据导出用例。