我正在尝试使用 Elasticsearch 的功能评分,但也欢迎其他方式。
我现有的搜索如下所示:
$searchParams['body']['query']['filtered']['query']["match_all"] = array("boost" => 1);
如何将 function_score 添加到此搜索中?这就是我现在所拥有的
$randomScore = new \stdClass();
// will be date or userid in the future
$randomScore->seed = 5;
$functionScore = array(
array("random_score" => $randomScore)
);
$searchParams['body']['query']['function_score']['functions'] = $functionScore;
$queryResponse = $client->search($searchParams);
这看起来非常接近正确。我只是需要更多的脑力。
最佳答案
你需要做两件事:
- 将查询嵌套在“functions_score”数组内
- 函数需要嵌套在额外的数组 [] 中
以下内容对我有用,请根据您的需要进行相应更改:
$params = array();
$params['body']['size'] = 10;
$params['body']['from'] = 0;
$random = new \stdClass();
$random->seed = time(); //change this to make randomization consistent, e.g. user id
$params['body']['query']['function_score']['functions'][]['random_score'] = $random;
$params['body']['query']['function_score']['query']['bool']['must_not'] = array(
'term' => array(
'_id' => $this->get('currentPageId'),
)
);
$params['body']['query']['function_score']['query']['bool']['must'] = array(
'term' => array(
'isAlias' => false
)
);
关于php - 如何在PHP中使用Elasticsearch搜索 "randomize"结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30810387/