我在一个使用 FosElasticaBundle (@dev) 的 Symfony 2.5.6 项目中。
在我的项目中,我只需要获取 Elastic Search 请求的总点击数。也就是说,我使用普通请求查询弹性搜索,但通过特殊的“计数”URL:
localhost:9200/_search?search_type=count
请注意“search_type=count” URL 参数。
这是示例查询:
{
"query": {
"filtered": {
"query": {
"match_all": []
},
"filter": {
"bool": {
"must": [
{
"terms": {
"media.category.id": [
681
]
}
}
]
}
}
}
},
"sort": {
"published_at": {
"order": "desc"
}
},
"size": 1
}
结果包含正常的 JSON 响应,但在part中没有任何文档hits
。从这个回复中,我很容易得到总数:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 81,
"max_score": 0,
"hits": [ ]
}
}
好的,hits.total == 81
。
现在,我在存储库中找不到任何通过 FOSElasticaBundle 执行相同操作的解决方案。
我试过这个:
$query = (...) // building the Elastica query here
$count = $this->finder->findPaginated(
$query,
array(ES::OPTION_SEARCH_TYPE => ES::OPTION_SEARCH_TYPE_COUNT)
)->getNbResults();
但是我尝试加载类“Pagerfanta”。我不想要Pagerfanta。
然后这个:
$count = $this->finder->createPaginatorAdapter(
$query,
array(ES::OPTION_SEARCH_TYPE => ES::OPTION_SEARCH_TYPE_COUNT)
)->getTotalHits();
但它总是给我0。
如果我可以从存储库访问 Elastica Finder 服务会很容易(然后我可以从查询搜索中获得一个 ResultSet,并且这个 ResultSet 有一个正确的 getTotalHits() 方法)。但是来自存储库的服务......你知道的。
感谢您提供任何帮助或线索!