1


I want to get the total results for a query using elastica for symfony2 what I did :

$u = $this->container->get('fos_elastica.finder.documents_index.documents');
$maxItems=10;
$query = new \Elastica\Query\Bool();
$elasticaQuery = new \Elastica\Query\QueryString($key_words.'~');
$elasticaQuery->setFuzzyPrefixLength(3);
$query->addMust($elasticaQuery);
try {
        $q = new \Elastica\Query();
        $q->setQuery($query);
        $q->setFrom(($page - 1) * $maxItems);
    } 
catch (Exception $e) {
        }
$data = $u->find($q);

$data is always 10 documents but this is not the problem , the problem is how to get the total hits so I can use them in Pagination :)

4

1 回答 1

0

Elastica\ResultSet 类有一个名为 getTotalHits() 的方法,因此您可以在搜索后执行类似的操作并获取总计/分页/等的完整记录数:

$hits = $resultSet->getTotalHits();

在此处查看源代码:Elastica ResultSet 类

顺便说一句,Elastica 源代码结构良好且易于阅读,这足以弥补其缺乏文档的不足。如果您遇到困难,非常值得阅读源代码和单元测试。

于 2014-05-07T07:08:35.913 回答