如何在 symfony2 上使用 elasticsearch 进行方面查询?
我可以查询,并得到结果,它有效!
现在在这个查询上,我想对结果进行分面。
如何在 symfony2 上使用 elasticsearch 进行方面查询?
我可以查询,并得到结果,它有效!
现在在这个查询上,我想对结果进行分面。
public function facetAction()
{
// index
$search = $this->get('fos_elastica.index.appellations.appellation');
$query = new \Elastica\Query\MatchAll();
$elasticaQuery = new \Elastica\Query();
$elasticaQuery->setQuery($query);
$elasticaQuery->setSize(550);
$elasticaFacet = new \Elastica\Facet\Terms('regions');
$elasticaFacet->setField('regions');
$elasticaFacet->setSize(550);
$elasticaFacet->setOrder('reverse_count');
$elasticaQuery->addFacet($elasticaFacet);
// ResultSet
$elasticaResultSet = $search->search($elasticaQuery);
// Get Facets
$elasticaFacets = $elasticaResultSet->getFacets();
foreach ($elasticaFacets['regions']['terms'] as $elasticaFacet) {
$results[] = $elasticaFacet;
}
return $this->container->get('templating')->renderResponse
('ApplicationGhvAppellationsBundle:Default:indexFacets.html.twig', array(
'appellations' => $results
));
}
由于 Facets 将在此处被弃用,因此更新了带有聚合的代码
public function aggregationAction()
{
// index
$search = $this->get('fos_elastica.index.appellations.appellation');
$query = new \Elastica\Query\MatchAll();
$elasticaQuery = new \Elastica\Query();
$elasticaQuery->setQuery($query);
$elasticaQuery->setSize(550);
$elasticaAggreg= new \Elastica\Aggregation\Terms('regions');
$elasticaAggreg->setField('regions');
$elasticaAggreg->setSize(550);
$elasticaAggreg->setOrder('_count', 'desc');
$elasticaQuery->addAggregation($elasticaAggreg);
// ResultSet
$elasticaResultSet = $search->search($elasticaQuery);
// Get Aggregations
$elasticaAggregs = $elasticaResultSet->getAggregations();
foreach ($elasticaAggregs['regions']['buckets'] as $elasticaAggreg) {
$results[] = $elasticaAggreg;
}
return $this->container->get('templating')->renderResponse
('ApplicationGhvAppellationsBundle:Default:indexFacets.html.twig', array(
'appellations' => $results
));
}