最近,因为业务需求,需要把一个集群的索引数据导入到另外一个索引中去,就是简单的读取索引文件并且将读到的数据写入到索引中,代码如下(注意,代码适应es2.0版本以下,其中参数path格式为:索引数据路径,一直指向到../索引名称/分片数/index这块):
public static void executeRecreate(String path, String indexName,
String indexType) {
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", "es").put("client.transport.sniff", true)
.put("client.transport.ping_timeout", "30s")
.put("client.transport.nodes_sampler_interval", "30s").build();
TransportClient client = new TransportClient(settings);
client.addTransportAddress(
new InetSocketTransportAddress("127.0.0.1", 9300));
File file = new File(path);
try {
Directory dir = FSDirectory.open(file);
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
int maxDoc = reader.maxDoc();
int docNum = 0;
BulkRequestBuilder bulkBuilder = new BulkRequestBuilder(client);
for (int i =