ongr/filter-manager-bundle

Filter manager bundle for product lists

Installs: 122 043

Dependents: 6

Suggesters: 0

Security: 0

Stars: 26

Watchers: 20

Forks: 43

Open Issues: 18

Type:symfony-bundle

v3.1.0 2022-05-30 08:50 UTC

README

Filter manager is used for listing documents. It provides ties between commonly used filtering options and UI elements with Elasticsearch repositories. It is important to mention that filtering is everything what has impact on list, it can be:

  • Filtering on specific field value object have (color, country etc.)
  • Filtering range (price range, distance from point etc.)
  • Documents list pagination. Paging changes representation of list, so it is considered to be filter and is treated like one.
  • Documents list sorting. Same as paging - sorting is filter in this bundle.
  • Any custom factor which has influence (not always directly visible) on result list. It can exclude, boost, modify some results, collect some metrics or any other action you can imagine.

If you need any help, stack overflow is the preffered and recommended way to ask ONGR support questions.

Build Status Coverage Status Latest Stable Version Scrutinizer Code Quality

Documentation

For online documentation of the bundle click here. All docs pages are located in Resources/doc/.

Installation

Step 1: Install FilterManager bundle

FilterManager bundle is installed using Composer.

# You can require any version you need, check the latest stable to make sure you are using the newest version.
$ composer require ongr/filter-manager-bundle "~3.0"

Please note that filter manager requires Elasticsearch bundle, guide on how to install and configure it can be found here.

Step 2: Enable FilterManager bundle

Enable Filter Manager bundle in your AppKernel:

// config/bundles.php

<?php

return [
    ...

    ONGR\ElasticsearchBundle\ONGRElasticsearchBundle::class => ['all' => true],
    ONGR\FilterManagerBundle\ONGRFilterManagerBundle::class => ['all' => true],
    JMS\SerializerBundle\JMSSerializerBundle::class => ['all' => true],

    ...
];

Step 3: Add configuration for manager

Add minimal configuration for Elasticsearch and FilterManager bundles.

# app/config/config.yml

ongr_elasticsearch:
  indexes:
    App\Document\Product:
      hosts: 
         - 127.0.0.1:9200 

ongr_filter_manager:
    managers:
        search_list: # <- Filter manager name
            filters:
                - country
            repository: App\Document\Product # <- Product document rindex service (used to be a repository prior to v3.0)
    filters:
        country: # <- Filter name
            type: choice
            request_field: country
            document_field: country

Note that Product document has to be defined. More about that in ElasticsearchBundle documentation.

In this particular example, we defined a single filter manager named search_list to filter documents from product repository, and we will be using the filter named country to filter on countries defined in document.

Step 4: Use your new bundle

FilterManagerBundle is ready to use. When you define filter manager the bundle generates a service according manager name. In this particular case it will be ongr_filter_manager.manager.search_list.

To get a list grab the service and call handleRequest(). Here's a short example in the controller:

<?php
 
use ONGR\FilterManagerBundle\DependencyInjection\ONGRFilterManagerExtension;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
 
class ProductController extends Controller
{
    /**
     * @param Request $request Request.
     *
     * @return Response
     */
    public function listAction(Request $request)
    {
        $searchList = $this->get(ONGRFilterManagerExtension::getFilterManagerId('search_list'))
                          ->handleRequest($request);
        $this->render(
            'AppBundle:Product:list.html.twig',
            [
              'filters' => $searchList->getFilters(),  
              'products' => $searchList->getResult(),  
            ]
        );                  
    }
}

More information how to use filters and render the results are in basics topic here.

Troubleshooting

If you face any issue or difficulty by implementing bundle, do not be afraid to create an issue with bug or question. Also ONGR organization has a tag in Stackoverflow so you can ask about all ONGR bundles also there.

License

This bundle is covered by the MIT license. Please see the complete license in the bundle LICENSE file.