3

I am using FOSElasticaBundle with symfony2 and doctrine 2.

I have trouble understanding how to retrieve actual doctrine objets from a search result. I am under the impression that it is the default behaviour but I get this kind of result :

object(Elastica\Result)[1239]
  protected '_hit' => 
    array (size=5)
      '_index' => string 'foodmeup' (length=8)
      '_type' => string 'recipes' (length=7)
      '_id' => string '2' (length=1)
      '_score' => float 2.2963967
      '_source' => 
        array (size=5)
          'name' => string 'Bavaroise vanille' (length=17)
          'nickName' => string 'Bavaroise vanille' (length=17)
          'content' => null
          'userRecipes' => 
            array (size=1)
              ...
          'tags' => 
            array (size=0)

Here is my FOSElasticaBundle configuration:

#Elastic Search
fos_elastica:
    default_manager: orm
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        search:
            client: default
            index_name: foodmeup
            types:
                recipes:
                    mappings:
                        name: { type: string, boost: 5}
                        nickName: { type: string }
                        content: { type: string }
                        userRecipes:
                            type: "nested"
                            properties:
                                name: { type: string }
                                content: { type: string }
                        tags:
                            type: "nested"
                            boost: 5
                            properties:
                                name: { type: string }
                    persistence:
                        driver: orm
                        model: AppBundle\Entity\FoodAnalytics\Recipe
                        repository: AppBundle\Repository\FoodAnalytics\RecipeRepository
                        provider: ~
                        finder: ~
                        listener: ~ # by default, listens to "insert", "update" and "delete"

And the code in my controller :

public function searchAction(Request $request)
{
    $search = $request->query->get('search');
    $finder = $this->get('fos_elastica.index.search.recipes');
    $results = $finder->search($search)->getResults();

    return array(
        'search' => $search,
        'results' => $results
    );
}

I understood I could use a custom repository method to get the objects, but before I reach that point, what is the default way to get objects ? (Here I want a Recipe Object, an instance of my model).

Thanks a lot !

Sébastien
  • 5,263
  • 11
  • 55
  • 116

1 Answers1

1

Got it! I called the wrong service. The correct controller code to retrieve directly object instances is:

public function searchAction(Request $request)
{
    $search = $request->query->get('search');
    $finder = $this->get('fos_elastica.finder.search.recipes');
    $results = $finder->find($search);

    return array(
        'search' => $search,
        'results' => $results
    );
}
Sébastien
  • 5,263
  • 11
  • 55
  • 116