Elasticsearch PHP 客户端引发异常“在集群中找不到活动节点”

2022-08-30 14:19:42

我正在尝试对索引执行扫描和滚动操作,如示例所示:

$client = ClientBuilder::create()->setHosts([MYESHOST])->build();
$params = [
    "search_type" => "scan",    // use search_type=scan
    "scroll" => "30s",          // how long between scroll requests. should be small!
    "size" => 50,               // how many results *per shard* you want back
    "index" => "my_index",
    "body" => [
        "query" => [
            "match_all" => []
        ]
    ]
];

$docs = $client->search($params);   // Execute the search
$scroll_id = $docs['_scroll_id'];   // The response will contain no results, just a _scroll_id

// Now we loop until the scroll "cursors" are exhausted
while (\true) {

    // Execute a Scroll request
    $response = $client->scroll([
            "scroll_id" => $scroll_id,  //...using our previously obtained _scroll_id
            "scroll" => "30s"           // and the same timeout window
        ]
    );

    // Check to see if we got any search hits from the scroll
    if (count($response['hits']['hits']) > 0) {
        // If yes, Do Work Here

        // Get new scroll_id
        // Must always refresh your _scroll_id!  It can change sometimes
        $scroll_id = $response['_scroll_id'];
    } else {
        // No results, scroll cursor is empty.  You've exported all the data
        break;
    }
}

第一个API调用执行正常,我能够取回滚动ID。但是API失败了,我得到了异常:“Elasticsearch\Common\Exceptions\NoNodesAvailableException在您的集群中找不到活动节点”$client->search($params)$client->scroll()

我正在使用 Elasticsearch 1.7.1 和 PHP 5.6.11

请帮忙


答案 1

我发现用于elasticsearch的php驱动程序充满了问题,我的解决方案是通过php实现带有curl的RESTful API,一切都工作得更快,调试也容易得多。


答案 2

我猜这个例子不是你正在使用的版本的最新版本(你提供的链接是2.0,你正在使用1.7.1)。只需在循环中添加:

try {
      $response = $client->scroll([
            "scroll_id" => $scroll_id,  //...using our previously obtained _scroll_id
            "scroll" => "30s"           // and the same timeout window
        ]
    );
}catch (Elasticsearch\Common\Exceptions\NoNodesAvailableException $e) {
   break;
}

推荐