package io.monokkel.utils;
import io.monokkel.factories.ClientFactory;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.SearchHit;
/**
* Created by tarjei on 14/09/14.
*/
public class Reindexer {
/**
* Extremely beta reindexer!
*
* @param args
*/
public static void main(final String[] args) {
ClientFactory clientFactory = new ClientFactory("localhost", 9300);
final Client client = clientFactory.buildStandardClientConnectedToOneNode();
QueryStringQueryBuilder qb = QueryBuilders.queryString("*");
SearchResponse scrollResp = client.prepareSearch("asite.no").setTypes("snapshot")
.setScroll(new TimeValue(60000))
.setQuery(qb)
.setSize(100).execute().actionGet();
while (true) {
if (scrollResp.getHits().getHits().length == 0) {
break;
}
final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
for (SearchHit hit : scrollResp.getHits()) {
bulkRequestBuilder.add(client.prepareIndex("test_1", "snapshot").setId(hit.getId()).setSource(hit.getSource()));
}
final BulkResponse bulkItemResponses = bulkRequestBuilder.execute().actionGet();
if (bulkItemResponses.hasFailures()) {
System.out.print(bulkItemResponses.buildFailureMessage());
}
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
}
}
}