Solr FieldCollapsing for More Like This queries


Solr FieldCollapsing for More Like This queries

我想使用"More Like This"查询来查找类似的文档,并折叠字段"image"中具有相同值的文档。我试着使用字段折叠参数,但它们似乎不适用于"更像这样"。

下面是我的代码片段。你能告诉我如何使用"More Like This"查询来折叠结果吗?

$url = "http://{$host}:{$port}/solr/{$core}/mlt";
$data = [
    'stream.body' => $content,
    'fl' => 'image,content,title,signature',
    'start' => 0,
    'order' => "score desc",
    'wt' => 'json',
    'mlt.fl' => 'content,title',
    // these lines do nothing ---v
    'group' => 'true',
    'group.field' => 'image',
    'group.sort' => 'impressions desc',
    'group.main' => 'true'
];
$curlHandle = curl_init($url);
$options = array (
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => $data
);
curl_setopt_array($curlHandle , $options);
$result = json_decode(curl_exec($curlHandle));

一般答案

我无法使用字段折叠参数折叠结果。然而,我能够使用CollapsingQParserPlugin实现所需的结果。以下过滤器查询将文档折叠在字段"图像"上,并选择字段"印象"值最高的文档:{!collapse field=image max=impressions}

实施

由于某些原因,我无法将此筛选器查询与其他筛选器查询组合在一个关键字下,如下所示:

$filterQueries = [
    "-signature:{$signature}",
    ...
    "{!collapse field=image max=impressions}"
];
$data = [
    ...
    'fq' => implode(' AND ', $filterQueries),
    ...
];

这产生了错误:Query没有实现createWeight

我的解决方案是做一个GET请求(而不是上面问题中做的POST)。使用GET请求,可以为每个筛选器查询提供一个密钥:http://solr-url/mtl?...&fq=-签名%3A0&fq=%7B!塌陷+场%3D图像+最大%3D印象%7D

以下是问题中片段的php解决方案:

$url = "http://{$host}:{$port}/solr/{$core}/mlt?"; // Note the added question mark
$data = [
    'stream.body' => $content,
    'fl' => 'image,content,title,signature',
    'fq' => $filterQueries,
    'start' => 0,
    'order' => "score desc",
    'wt' => 'json',
    'mlt.fl' => 'content,title'
];
$params = [];
foreach ($data as $key=>$value) {
    if (is_array($value)) {
        foreach ($value as $subvalue) {
            $subvalue = urlencode($subvalue);
            $params[] = "{$key}={$subvalue}";
        }
    } else {
        $value = urlencode($value);
        $params[] = "{$key}={$value}";
    }
}
$url .= implode('&', $params);
$curlHandle = curl_init($url);
$options = array ();
curl_setopt_array($curlHandle , $options);
$result = json_decode(curl_exec($curlHandle));