gpt4 book ai didi

php - 如何使用Elasticsearch在PHP中 “randomize”搜索结果?

转载 作者:行者123 更新时间:2023-12-02 22:24:52 25 4
gpt4 key购买 nike

我正在尝试使用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 - 如何使用Elasticsearch在PHP中 “randomize”搜索结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30810387/

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