Last active
December 18, 2015 00:48
-
-
Save damienalexandre/5698929 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function indexAction($page) | |
{ | |
$request = $this->container->get('request'); | |
$routeName = $request->get('_route'); | |
$f_r = $request->query->get('f_r'); | |
$query = new \Elastica\Query\MatchAll(); | |
$my_facets = array(); | |
$elasticaQuery = new \Elastica\Query(); | |
$elasticaQuery->setQuery($query); | |
//$elasticaQuery->setSize(550); | |
// facet region | |
$elasticaFacet = new \Elastica\Facet\Terms('region'); | |
$elasticaFacet->setField('region'); | |
$elasticaFacet->setSize(60); | |
// Allow to control the ordering of the terms facets, to be ordered by count, term, reverse_count or reverse_term. The default is count | |
$elasticaFacet->setOrder('term'); | |
$elasticaQuery->addFacet($elasticaFacet); | |
$my_facets[] = $elasticaFacet; | |
// facet appellation | |
$elasticaFacet2 = new \Elastica\Facet\Terms('appellation'); | |
$elasticaFacet2->setField('appellation'); | |
$elasticaFacet2->setSize(560); | |
$elasticaFacet2->setOrder('term'); | |
$elasticaQuery->addFacet($elasticaFacet2); | |
$my_facets[] = $elasticaFacet; | |
// If there is a Facet Query string, apply it on the query BEFORE the search. | |
if ($f_r) { | |
$filters = $this->getFacetsFilters($f_r, $my_facets); | |
if ($filters) { | |
$elasticaQuery->setQuery(new Filtered($query, $filters)); | |
} | |
} | |
// ResultSet | |
$elasticaResultSet = $search->search($elasticaQuery); | |
return $this->container->get('templating')->renderResponse('ApplicationGhvVinsBundle:Default:index.html.twig', array( | |
'entities' => $entities, | |
'pager' => $pagerfanta, | |
'regions' => $results, | |
'appellations' => $results2 | |
)); | |
} | |
// The function I have published here: https://gist.github.com/damienalexandre/5661320 | |
/** | |
* Return query Filter from a list of query string & allowed Facets | |
* | |
* @param array $query The user query (facet_name => query, facet_name => query...) | |
* @param array $allowed_facets An array of Elastica\Facet | |
* | |
* @return \Elastica\Filter\AbstractFilter (if multiple filters, they are combined in BoolAnd filter) | |
*/ | |
private function getFacetsFilters($query, $allowed_facets) | |
{ | |
$filters = array(); | |
if (!is_array($query)) { | |
return false; | |
} | |
if (!$allowed_facets || !is_array($allowed_facets) || empty($allowed_facets)) { | |
return false; | |
} | |
foreach ($query as $facet_name => $term) | |
{ | |
$facet = false; | |
foreach ($allowed_facets as $allowed_facet) { | |
if ($allowed_facet->getName() === $facet_name) { | |
$facet = $allowed_facet; break; | |
} | |
} | |
if ($facet instanceof AbstractFacet) { | |
switch (get_class($facet)) | |
{ | |
case 'Elastica\Facet\Terms': | |
$filters[] = new \Elastica\Filter\Terms($facet->getParam('field'), array($term)); | |
break; | |
case 'Elastica\Facet\Range': | |
$ranges = $facet->getParam('ranges'); | |
if (isset($ranges[(int) $term])) { | |
$filters[] = new \Elastica\Filter\NumericRange($facet->getParam('field'), $ranges[(int) $term]); | |
} | |
break; | |
case 'Elastica\Facet\Filter': | |
$facet_filters = $facet->toArray(); | |
if (isset($facet_filters['filter']['exists'])) { | |
$filters[] = new \Elastica\Filter\Exists($facet_filters['filter']['exists']['field']); | |
} else { | |
throw new \Exception(sprintf('The "%s" facet query filter cant be set', $facet_name)); | |
} | |
break; | |
default: | |
throw new \Exception(sprintf('The "%s" facet query filter is not implemented!!', $facet_name)); | |
} | |
} | |
} | |
if (count($filters) > 1) { | |
$and = new \Elastica\Filter\BoolAnd(); | |
foreach ($filters as $filter) { | |
$and->addFilter($filter); | |
} | |
return $and; | |
} elseif (count($filters) === 1) { | |
return $filters[0]; | |
} else { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment