Add filtered query example to README.md

This commit is contained in:
RobertPlant 2013-06-14 11:28:19 +02:00 committed by Jeremy Mikola
parent 1af85115df
commit cce1ffb06c

View file

@ -754,3 +754,32 @@ fos_elastica:
provider:
finder:
```
### Filtering Results and executing a default query
You may want to remove a certain section of results from a query, filtering is ideal because it is slightly faster in performance compared to a standard query because the only the filtered subset of results are cached. This improves query speed because the query is ran against the predetermined subset from previous queries rather than recalculating the whole query again. Filtering can effectively when trying to look for only active records which are indicated by a field in the database. `FilteredQuery` should be used to combine the `QueryString` operator with the `Filter`, an example of `FilteredQuery` is shown Below.
```php
$term = new \Elastica\Filter\Term();
$term->setParams(array(
'active' => 'active',
'address.postCode' => $postCode //Accessing elements nested within an object. address is the object and postCode is the property within it.
'companyGroup' => $department
));
$query = new \Elastica\Query\QueryString();
$query->setQuery($queryString);
$filteredQuery = new \Elastica\Query\Filtered($query, $term);
$results = $this->container->get('fos_elastica.finder.company.company')->findPaginated($filteredQuery);
// Filter results for paging
$results->setMaxPerPage($limit);
$results->setCurrentPage($page);
// Number of total results for paging
$total = $results->getNbResults();
```