3

我正在将 FOSElasticaBundle 与 symfony2 和教义 2 一起使用。

我很难理解如何从搜索结果中检索实际的教义对象。我的印象是这是默认行为,但我得到了这种结果:

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)

这是我的 FOSElasticaBundle 配置:

#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"

我的控制器中的代码:

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
    );
}

我知道我可以使用自定义存储库方法来获取对象,但是在达到那个点之前,获取对象的默认方法是什么?(这里我想要一个食谱对象,我的模型的一个实例)。

非常感谢 !

4

1 回答 1

1

知道了!我打错了服务。直接检索对象实例的正确控制器代码是:

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
    );
}
于 2015-01-18T16:12:49.617 回答