Commit a37393ba authored by Mark Harding's avatar Mark Harding Committed by Mark Harding

(feat): improve new top feeds to work with channels and groups

No related merge requests found
Pipeline #102256225 waiting for manual action with stages
in 8 minutes and 7 seconds
......@@ -154,7 +154,7 @@ class All extends Cli\Controller implements Interfaces\CliControllerInterface
$this->out(sprintf(
"%s -> %s",
date('r', $from),
date('r', $to),
date('r', $to)
));
$this->out("Syncing {$displayType} / {$metric}");
......
......@@ -250,6 +250,16 @@ class Repository
];
}
if ($type === 'group') {
$body['query']['function_score']['query']['bool']['must'][] = [
'range' => [
'access_id' => [
'gt' => 2,
]
]
];
}
// Time bounds
$timestampUpperBounds = []; // LTE
......@@ -386,6 +396,10 @@ class Repository
$esType = $opts['type'];
if ($type === 'user' || $type === 'group') {
$esType = 'activity,object:image,object:video,object:blog';
}
if ($esType === 'all') {
$esType = 'object:image,object:video,object:blog';
}
......@@ -416,9 +430,31 @@ class Repository
]);
}
}
$docs = $response['hits']['hits'];
// Sort channels / groups by post scores
if ($type === 'user' || $type === 'group') {
$newDocs = []; // New array so we return only users and groups, not posts
foreach ($docs as $doc) {
$key = $doc['_source'][$this->getSourceField($type)];
$newDocs[$key] = $newDocs[$key] ?? [
'_source' => [
'guid' => $key,
'owner_guid' => $key,
$this->getSourceField($type) => $key,
'@timestamp' => $doc['_source']['@timestamp'],
],
'_type' => $type,
'_score' => 0,
];
$newDocs[$key]['_score'] = log10($newDocs[$key]['_score'] + $algorithm->fetchScore($doc));
}
$docs = $newDocs;
}
$guids = [];
foreach ($response['hits']['hits'] as $doc) {
foreach ($docs as $doc) {
$guid = $doc['_source'][$this->getSourceField($opts['type'])];
if (isset($guids[$guid])) {
continue;
......@@ -436,12 +472,12 @@ class Repository
private function getSourceField(string $type)
{
switch ($type) {
//case 'user':
// return 'owner_guid';
// break;
//case 'group':
// return 'container_guid';
// break;
case 'user':
return 'owner_guid';
break;
case 'group':
return 'container_guid';
break;
default:
return 'guid';
break;
......
......@@ -7,10 +7,23 @@
namespace Minds\Core\Search\SortingAlgorithms;
use Minds\Core\Di\Di;
use Minds\Core\Features\Manager as Features;
class Hot implements SortingAlgorithm
{
/** @var Features */
protected $features;
/** @var string */
protected $period;
public function __construct($features = null)
{
$this->features = $features ?? Di::_()->get('Features');
}
/**
* @return bool
*/
......@@ -25,7 +38,9 @@ class Hot implements SortingAlgorithm
*/
public function setPeriod($period)
{
$this->period = $period;
if (!$this->features->has('top-feeds-by-age')) {
$this->period = $period;
}
return $this;
}
......@@ -34,24 +49,34 @@ class Hot implements SortingAlgorithm
*/
public function getQuery()
{
return [
'bool' => [
'must' => [
[
'range' => [
"votes:up:{$this->period}:synced" => [
'gte' => strtotime("1 hour ago", time()),
if ($this->period) {
return [
'bool' => [
'must' => [
[
'range' => [
"votes:up:{$this->period}:synced" => [
'gte' => strtotime("7 days ago", time()),
],
],
],
/*'range' => [
"votes:up:{$this->period}" => [
'gte' => 1,
],
],*/
],
],
]
];
]
];
}
return [
'bool' => [
'must' => [
[
'range' => [
"votes:up" => [
'gte' => 1,
],
],
],
],
]
];
}
/**
......@@ -60,17 +85,32 @@ class Hot implements SortingAlgorithm
public function getScript()
{
$time = time();
if ($this->period) {
return "
def up = doc['votes:up:{$this->period}'].value ?: 0;
def down = doc['votes:down:{$this->period}'].value ?: 0;
def age = $time - (doc['@timestamp'].value.millis / 1000) - 1546300800;
def votes = up - down;
def sign = (votes > 0) ? 1 : (votes < 0 ? -1 : 0);
def order = Math.log(Math.max(Math.abs(votes), 1));
return (sign * order) - (age / 43200);
";
}
return "
def up = doc['votes:up:{$this->period}'].value ?: 0;
def down = doc['votes:down:{$this->period}'].value ?: 0;
def up = doc['votes:up'].value ?: 0;
def down = doc['votes:down'].value ?: 0;
def age = $time - (doc['@timestamp'].value.millis / 1000) - 1546300800;
def age = doc['@timestamp'].value.millis / 1000;
def votes = up - down;
def sign = (votes > 0) ? 1 : (votes < 0 ? -1 : 0);
def order = Math.log(Math.max(Math.abs(votes), 1));
return (sign * order) - (age / 43200);
// Rounds to 7
return Math.round((sign * order + age / 43200) * 1000000) / 1000000;
";
}
......
......@@ -93,7 +93,7 @@ class RepositorySpec extends ObjectBehavior
$this->client->request(Argument::that(function ($query) {
$query = $query->build();
return $query['type'] === 'user' && in_array('guid', $query['body']['_source'], true);
return $query['type'] === 'activity,object:image,object:video,object:blog' && in_array('owner_guid', $query['body']['_source'], true);
}))
->shouldBeCalled()
->willReturn([
......@@ -126,10 +126,10 @@ class RepositorySpec extends ObjectBehavior
$gen = $this->getList($opts);
$gen->current()->getGuid()->shouldReturn('1');
$gen->current()->getScore()->shouldReturn(100.0);
$gen->current()->getScore()->shouldReturn(log10(100.0));
$gen->next();
$gen->current()->getGuid()->shouldReturn('2');
$gen->current()->getScore()->shouldReturn(50.0);
$gen->current()->getScore()->shouldReturn(log10(50.0));
}
public function it_should_query_a_list_of_group_guids()
......@@ -143,7 +143,7 @@ class RepositorySpec extends ObjectBehavior
$this->client->request(Argument::that(function ($query) {
$query = $query->build();
return $query['type'] === 'group' && in_array('guid', $query['body']['_source'], true);
return $query['type'] === 'activity,object:image,object:video,object:blog' && in_array('container_guid', $query['body']['_source'], true);
}))
->shouldBeCalled()
->willReturn([
......@@ -178,10 +178,10 @@ class RepositorySpec extends ObjectBehavior
$gen = $this->getList($opts);
$gen->current()->getGuid()->shouldReturn('1');
$gen->current()->getScore()->shouldReturn(100.0);
$gen->current()->getScore()->shouldReturn(log10(100));
$gen->next();
$gen->current()->getGuid()->shouldReturn('2');
$gen->current()->getScore()->shouldReturn(50.0);
$gen->current()->getScore()->shouldReturn(log10(50));
}
// Seems like yielded functions have issues with PHPSpec
......
Please register or to comment