如何使用 Elasticsearch 在 PHP 中 "randomize" 搜索结果?
How to use Elasticsearch to "randomize" search results in PHP?
我正在尝试使用 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
)
);
我正在尝试使用 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
)
);