我正在使用PHP elasticsearch客户端,并使用以下代码从elasticsearch获取所有匹配的数据。

$sponsorSearch['index'] = 'sponsors';
$sponsorSearch['type'] = 'couchbaseDocument';
$sponsorSearch['body']['query']['bool']['must'][]['match']['eventid'] = $EventID;
$sponsorSearch['body']['query']['bool']['must'][]['match']['paystatus'] = "complete";
$sponsorCount = $client->count($sponsorSearch);
if($sponsorCount['count']>0) {
   $sponsorSearch['from'] = 0;
   $sponsorSearch['size'] = $sponsorCount['count'];
   $sponsorResponse = $client->search($sponsorSearch);
}

但是它使用两个匹配项来进行 flex 搜索,一个用于计数文档的数量,另一个用于获取文档。我只想在一次点击中执行此操作。

最佳答案

如果您有10个以上的文档(但少于10000个),则只需在查询中指定大于10的大小,然后进行搜索(即无计数查询):

$sponsorSearch['index'] = 'sponsors';
$sponsorSearch['type'] = 'couchbaseDocument';
$sponsorSearch['size'] = 1000;
$sponsorSearch['body']['query']['bool']['must'][]['match']['eventid'] = $EventID;
$sponsorSearch['body']['query']['bool']['must'][]['match']['paystatus'] = "complete";
$sponsorResponse = $client->search($sponsorSearch);

09-25 23:30