Skip to content

Instantly share code, notes, and snippets.

@gordonbanderson
Created August 8, 2015 15:14
Show Gist options
  • Save gordonbanderson/e86bcdc43db550bcc7e1 to your computer and use it in GitHub Desktop.
Save gordonbanderson/e86bcdc43db550bcc7e1 to your computer and use it in GitHub Desktop.
Example of how to aggregate with filtering using Elastica
$queryString = new QueryString($_GET['q']);
$isoTerm = new Term();
$isoTerm->setTerm('ISO',4000);
$apeterm = new Term();
$apeterm->setTerm('Aperture',2.7);
$andFilter = new BoolAnd();
$andFilter->addFilter($apeterm);
$andFilter->addFilter($isoTerm);
$filter = new Filtered(
$queryString,
$andFilter
/*new Range('published', array(
'from' => '2012-01-01 00:00:00',
'to' => '2013-01-01 00:00:00',
))*/
);
$query = new Query($filter);
@gordonbanderson
Copy link
Author

The documentation for Elastica has no examples of how to this. The logic is this:

  1. create a QueryString with the actual search text
  2. create more filter(s) of type Term with the facets selected, here an film speed (ISO) of 4000 and an aperture of 2.7
  3. create a BoolAnd boolean and filter, and add the above filters to it
  4. create a Filtered object filtering the original query string with the and filter
  5. create the Query object

In the case of step 4) and there only being one filter, simply add that and skip the creation of the Boolean And filter. In the case of no facets chosen simply do not add the filter parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment