public function search(array $fields, $limit = 10, $returnPosts = false)
{
$results = [];
$searchParams = ['index' => $this->config->get('index', 'phosphorum'), 'type' => 'post', 'body' => ['fields' => ['id', 'karma'], 'query' => [], 'from' => 0, 'size' => intval($limit)]];
if (count($fields) == 1) {
$searchParams['body']['query']['match'] = $fields;
} else {
$terms = [];
foreach ($fields as $field => $value) {
$terms[] = ['term' => [$field => $value]];
}
$searchParams['body']['query']['bool'] = ['must' => $terms];
}
try {
$queryResponse = $this->client->search($searchParams);
$queryResponse = $this->parseElasticResponse($queryResponse);
$d = 0.5;
foreach ($queryResponse as $hit) {
if (!isset($hit['fields']['id'][0])) {
continue;
}
$id = $hit['fields']['id'][0];
$post = Posts::findFirstById($id);
if (!$post || $post->deleted == Posts::IS_DELETED) {
continue;
}
if ($hit['fields']['karma'][0] > 0 && ($post->hasReplies() || $post->hasAcceptedAnswer())) {
$score = $hit['_score'] * 250 + $hit['fields']['karma'][0] + $d;
if (!$returnPosts) {
$results[$score] = $this->createPostArray($post);
} else {
$results[$score] = $post;
}
$d += 0.05;
}
}
} catch (\Exception $e) {
$this->logger->error("Indexer: {$e->getMessage()}. {$e->getFile()}:{$e->getLine()}");
}
krsort($results);
return array_values($results);
}