Phosphorum\Search\Indexer::search PHP Method

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

Usage Example

 /**
  * Finds related posts
  */
 public function showRelatedAction()
 {
     $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
     $post = Posts::findFirstById($this->request->getPost('id'));
     if ($post) {
         $indexer = new Indexer();
         $posts = $indexer->search(['title' => $post->title, 'category' => $post->categories_id], 5, true);
         if (count($posts) == 0) {
             $posts = $indexer->search(['title' => $post->title], 5, true);
         }
         $this->view->setVar('posts', $posts);
     } else {
         $this->view->setVar('posts', []);
     }
 }