gpt4 book ai didi

symfony - elastica 搜索如何将 Elastica\Result 转换为实际的学说对象

转载 作者:行者123 更新时间:2023-11-29 02:57:07 24 4
gpt4 key购买 nike

我将 FOSElasticaBundle 与 symfony2 和 doctrine 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"

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

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

非常感谢!

最佳答案

明白了!我打错电话了。直接检索对象实例的正确 Controller 代码是:

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

关于symfony - elastica 搜索如何将 Elastica\Result 转换为实际的学说对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28011083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com