Custom Filter

There is possibility to add custom filters to filter managers via tagged filter service. You must create filter class, define it as a service with ongr_filter_manager.filter tag. Afterwards it will be available to use the same way that the regular filters are.

1. Create filter class

The only real requirement for the class must implement FilterInterface, But our recommendation is to extend one of the filters to gain the base functionality.

In this example we will provide a filter that will exclude documents with certain values in specified field:


namespace AppBundle\Filter;

use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
use ONGR\ElasticsearchDSL\Search;
use ONGR\FilterManagerBundle\Filter\FilterState;
use ONGR\FilterManagerBundle\Filter\Widget\Search\DocumentValue;
use ONGR\FilterManagerBundle\Search\SearchRequest;

* Filter for ONGR FilterManager. Excludes certain values from search.
class ExclusionFilter extends DocumentValue
    * {@inheritdoc}
    public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
        $exclusion = new BoolQuery();

        if (!empty($values = $this->getOption('exclude'))) {
            foreach ($values as $value) {
                $exclusion->add(new TermQuery($this->getDocumentField(), $value), BoolQuery::MUST_NOT);


    * {@inheritdoc}
    public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null)
        // Nothing more to do here.

As you can see, the filter will exclude all the values that will be defined in the exclude option from the results.

2. Defining service

Filter service must be tagged with ongr_filter_manager.filter tag, and you must also provide a unique type name that will be used when you will be defining the actual filters.

# app/config/services.yml

        class: AppBundle\Filter\ExclusionFilter
            - { name: ongr_filter_manager.filter, type: app.exclusion_filter }

3. Adding filter to manager

You can add custom filter in the same way that you add regular filters. The only catch here is that you need to provide your newly created filter type

# app/config/config.yml

                - custom_exclusion_filter
                # ...
            repository: 'es.manager.default.product'
        # ...
            type: app.exclusion_filter #notice that we are using our custom filter type here
            request_field: ~
            document_field: category
                exclude: ['hidden', 'private', 'classified']

4. Using filter

Filter can be used as other filters through FilterManager, see basics chapter.