Search code examples
symfonyelasticsearchfoselasticabundle

Foselasticabundle : method returns empty results


I use Elasticsearch with Foselasticabundle to search in my Symfony app, but it returns empty results. This is my config and search method:

foselasticbunde.yml:

  indexes:
       search:
           finder: ~
           client: default
           types:
               course:
                   mappings:
                       id: ~
                       title: ~

                   persistence:
                       driver: orm
                       model: AppBundle\Entity\Course
                       finder: ~
                       provider: ~
                       listener: ~

SearchController.php

 public function elasticSearchAction(Request $request)
    {
        $query = $request->get('q');
        $finder = $this->container->get('fos_elastica.finder.search.course');
        $results = $finder->find($query);
        return new JsonResponse($results);
    }

But it returns this empty results:

[{},{},{},{},{},{},{},{},{},{}]

Whats the problem and how can I fix it?


Solution

  • Results is an array of objects, I changed my code:

    public function elaSearchAction(Request $request)
    {
        $query = $request->get('q');
    
        $finder = $this->container->get('fos_elastica.finder.search.course');
        $results = $finder->find($query);
    
        $data = array();
        foreach ($results as $course){
            $data[] = array(
                'id' => $course->getId(),
                'title' => $course->getTitle(),
                'description' => $course->getDescription(),
            );
        }
    
        return new JsonResponse($data);
    }