如何使用Elasticsearch来随机化"用PHP搜索结果?

时间:2015-06-12 19:00:50

标签: php search random elasticsearch

我试图使用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);

这似乎非常接近正确。我只需要更多的脑力。

1 个答案:

答案 0 :(得分:2)

你需要做两件事:

  1. 将查询嵌套在'functions_score'数组
  2. 函数需要嵌套在额外的数组[]
  3. 以下对我有用,请根据您的需要进行更改:

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