SearchBundle for ElasticaBundle
1) You need Fos Elastica Bundle installed and configurated
2) Add SearchBundle to your appKernel, (*1)
$bundles = array(
[..]
new manugarciaes\SearchBundle\SearchBundle(),
);
3) Change your Fos Elastica Bundle configuration, using as repository the next one, (*2)
persistence:
[..]
repository: manugarciaes\SearchBundle\Repository\SearchRepository
4) Create a new service using the Easy Search Service like next example:
- elastica_entity is the fos elastica entity in your configuration
- search_string is the fields of your entity where you want to search, (*3)
## EXAMPLE
article.search:
class: SearchBundle\Services\SearchService
arguments:
- "@fos_elastica.manager.orm"
- %elastica_entity%
- ['%search_string%']
If you want Add Filters, (*4)
1) create a new service with Easy Search Filter Object, (*5)
## FILTER EXAMPLE
elastic.search.filter.enabled:
class: SearchBundle\Filter\ObjectFilter
calls:
- [setField, ['enabled']]
- [setValue, [true]]
If you want a dynamic value you can use for example a service or something like
this:, (*6)
- [setValue, ["@=service('request_stack').getMasterRequest.get('enabled')"]]
With this your filter change the value using the request, (*7)
2) Add this filter to your Search Service, (*8)
## EXAMPLE
article.search:
class: SearchBundle\Services\SearchService
arguments:
- "@fos_elastica.manager.orm"
- %elastica_entity%
- ['%search_string%']
calls:
- [addFilter, ['@elastic.search.filter.enabled']]
If you want add Aggregations, (*9)
1) Create a new service with Easy Search Aggregation Object, (*10)
## example
elastic.search.aggregation.categories:
class: Elastica\Aggregation\Terms
arguments:
- %name%
calls:
- [setField, ['%category_field%']]
- [setSize, [%category_size%]]
2) Add this aggregation to your Easy Search Service, (*11)
## EXAMPLE
article.search:
class: SearchBundle\Services\SearchService
arguments:
- "@fos_elastica.manager.orm"
- %elastica_entity%
- ['%search_string%']
calls:
- [addAggregation, [@elastic.search.aggregation.categories]], (*12)
Now you can make a search using this service, example:, (*13)
$searchService = $this->get('article.search');
$results = $searchService->search('hello');