Search code examples
symfonyfoselasticabundle

FOSElasticaBundle / Custom filter


I have an entity with title, url, description, text, enabled,... I want to search only through enabled = true articles. How can I do it in elastica?

My config:

fos_elastica:
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        appletrh:
            index_name: 'domain.com'
            client: default
            types:
                products:
                    properties:
                        title: { type: string, analyzer: czech }
                    persistence:
                        driver: orm
                        model: Web\MagazineBundle\Entity\Article
                        elastica_to_model_transformer:
                          **query_builder_method: search**
                        provider: ~
                        listener: ~
                        finder: ~

EntityRepository search function

 public function search()
    {
        $qb = $this->createQueryBuilder('p');
        $qb->where('p.enabled = true');

        return $qb;
    }

Action:

public function searchAction(Request $request)
{
    $keyword = $request->query->get('keyword');
    $finder = $this->get('fos_elastica.finder.domain.articles');
    $paginator = $this->get('knp_paginator');
    $articles= $finder->createPaginatorAdapter($keyword);
    $pagination = $paginator->paginate($articles, $request->query->get('page', 1), 12);


    return $this->render('WebMagazineBundle:Search:search.html.twig', ['articles' => $pagination]);
}

I thought that this is the right solution, but it's still returning all of the data from DB.


Solution

  • I feel like you should use "provider", not "elastica_to_model_transformer" option in the bundle config for that purpose, like:

    provider:
       query_builder_method: search