elasticsearch批量建索引出错
用elasticsearch批量创建索引时候会报以下错误
[0]: index [myshop_portal], type [type-product], id [1], message [UnavailableShardsException[[myshop_portal][2] Primary shard is not active or isn't assigned is a known node. Timeout: [1m], request: org.elasticsearch.action.bulk.BulkShardRequest@47359fdc]]
[2]: index [myshop_portal], type [type-product], id [3], message [UnavailableShardsException[[myshop_portal][4] Primary shard is not active or isn't assigned is a known node. Timeout: [1m], request: org.elasticsearch.action.bulk.BulkShardRequest@2e0a2298]]
[3]: index [myshop_portal], type [type-product], id [4], message [UnavailableShardsException[[myshop_portal][0] Primary shard is not active or isn't assigned is a known node. Timeout: [1m], request: org.elasticsearch.action.bulk.BulkShardRequest@7b5c733d]]
[5]: index [myshop_portal], type [type-product], id [13], message [UnavailableShardsException[[myshop_portal][2] Primary shard is not active or isn't assigned is a known node. Timeout: [1m], request: org.elasticsearch.action.bulk.BulkShardRequest@47359fdc]]
但是用junit跑就不出错....
public void bulkIndex(List<SearchDocument> searchDocuments) {
try {
Node node = NodeBuilder.nodeBuilder().node();
client = node.client();
BulkRequestBuilder bulkRequest = client.prepareBulk();
for (SearchDocument searchDoc : searchDocuments) {
IndexRequestBuilder indexRequest = this.prepareIndexRequest(searchDoc);
bulkRequest.add(indexRequest);
}
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
throw new ElasticSearchException(bulkResponse.buildFailureMessage());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (client != null) {
client.close();
}
}
}
private IndexRequestBuilder prepareIndexRequest(SearchDocument searchDoc) throws Exception {
String id = searchDoc.getId();
String source = searchDoc.getSource();
IndexRequestBuilder indexRequestBuilder = null;
if (source != null) {
System.out.println(source);
indexRequestBuilder = client.prepareIndex(searchDoc.getIndexName(), searchDoc.getType(), id).setSource(source);
} else {
throw new ElasticSearchException("object or source is null, failed to index the document [id: " + searchDoc.getId() + "]");
}
return indexRequestBuilder;
}