import com.cdel.utils.EsClientServiceFactoryByENV;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.ClearScrollRequest;
import org.elasticsearch.action.search.ClearScrollResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import java.util.Map;
public class bulkGetFaqAccV4 {
private static Logger logger = LogManager.getLogger(bulkGetFaqAccV4.class);
private static TransportClient client = EsClientServiceFactoryByENV.getInstance().getClient();
public static void main(String[] args) {
String srcIndex = "xxx";
String desType = "xxx";
SearchResponse response = client.prepareSearch(srcIndex)
.setQuery(QueryBuilders.matchAllQuery()).setTypes(desType)
.setScroll(TimeValue.timeValueMinutes(20))
.setSize(1000).execute().actionGet();
long totalHits = response.getHits().getTotalHits();
logger.info("---------一共有{}个", totalHits);
while (response.getHits().getHits().length > 0) {
BulkRequestBuilder bulkRequest = client.prepareBulk();
SearchHits hits = response.getHits();
SearchHit[] result = hits.getHits();
for (SearchHit hit : result) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
}
String scrollId = response.getScrollId();
response = client.prepareSearchScroll(scrollId)
.setScroll(TimeValue.timeValueMinutes(20))
.execute().actionGet();
}
ClearScrollRequest request = new ClearScrollRequest();
request.addScrollId(response.getScrollId());
ClearScrollResponse clearScrollResponse = client.clearScroll(request).actionGet();
client.close();
}
}