Vote count:
0
I received this message when making index with Elasticsearch:
No alive nodes found in your cluster
The function that makes error is indexAll()
Here is the code:
<?php
/*
+------------------------------------------------------------------------+
| Phosphorum |
+------------------------------------------------------------------------+
| Copyright (c) 2013-2014 Phalcon Team and contributors |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
*/
namespace Bcdcnt\Search;
use Elasticsearch\Client;
use Bcdcnt\Models\Song;
/**
* Indexer
*
* This component uses ElasticSearch to search items in the forum
*/
class Indexer
{
/**
* Search documents in ElasticSearch by the specified criteria
*
* @param array $fields
* @param int $limit
* @param boolean $returnPosts
*/
public function search(array $fields, $limit = 10, $returnPosts = false)
{
try {
$client = new Client();
$searchParams['index'] = 'bcdcnt';
$searchParams['type'] = 'song';
$searchParams['body']['fields'] = array('id', 'title', 'content', 'views', 'downloads', 'created_at');
if (count($fields) == 1) {
$searchParams['body']['query']['match'] = $fields;
} else {
$terms = array();
foreach ($fields as $field => $value) {
$terms[] = array('term' => array($field => $value));
}
$searchParams['body']['query']['bool']['must'] = $terms;
}
$searchParams['body']['from'] = 0;
$searchParams['body']['size'] = $limit;
$queryResponse = $client->search($searchParams);
$results = array();
if (is_array($queryResponse['hits'])) {
$d = 0.5;
foreach ($queryResponse['hits']['hits'] as $hit) {
$post = Song::findFirstById($hit['fields']['id'][0]);
if ($post) {
$score = $hit['_score'] * 250 + $hit['fields']['views'][0] + $hit['fields']['downloads'][0];
if (!$returnPosts) {
$results[$score] = array(
'id' => $post->id,
'title' => $post->title,
'created_at' => $post->created_at
);
} else {
$results[$score] = $post;
}
}
}
}
krsort($results);
return array_values($results);
} catch (\Exception $e) {
return array();
}
}
/**
* Index a single document
*
* @param Client $client
* @param Posts $post
*/
protected function _doIndexSong($client, $post)
{
$params = array();
$params['body'] = array(
'id' => $post->id,
'title' => $post->title,
'content' => $post->content,
'views' => $post->views,
'downloads' => $post->downloads,
'created_at' => $post->created_at
);
$params['index'] = 'bcdcnt';
$params['type'] = 'song';
$params['id'] = 'song-' . $post->id;
$ret = $client->index($params);
var_dump($ret);
}
public function searchCommon()
{
$client = new Client();
$searchParams['index'] = 'bcdcnt';
$searchParams['type'] = 'song';
$searchParams['body']['common']['body']['fields'] = array('id', 'title', 'content', 'views', 'downloads', 'created_at');
$searchParams['body']['common']['body']['query'] = "nelly the elephant not as a cartoon";
$searchParams['body']['common']['body']["cutoff_frequency"] = 0.001;
$queryResponse = $client->search($searchParams);
}
/**
* Puts a post in the search server
*
* @param Posts $post
*/
public function index($post)
{
$client = new Client();
$this->_doIndexSong($client, $post);
}
/**
* Indexes all posts in the forum in ES
*/
public function indexAll()
{
$client = new Client();
try {
$deleteParams['index'] = 'bcdcnt';
$client->indices()->delete($deleteParams);
} catch (\Exception $e) {
// the index does not exist yet
}
foreach (Song::find() as $post) {
$this->_doIndexSong($client, $post);
}
}
}
Does anybody get the same problem? How to fix it?
asked 38 secs ago
Elasticsearch: No alive nodes found in your cluster
Aucun commentaire:
Enregistrer un commentaire