ElasticSearch reindex by JAVA API

[html]  view plain  copy
 
  1. package elasticsearch.importdata;  
  2.   
  3. import org.elasticsearch.action.search.SearchResponse;  
  4. import org.elasticsearch.action.search.SearchType;  
  5. import org.elasticsearch.client.Client;  
  6. import org.elasticsearch.client.transport.TransportClient;  
  7. import org.elasticsearch.common.settings.ImmutableSettings;  
  8. import org.elasticsearch.common.settings.Settings;  
  9. import org.elasticsearch.common.transport.InetSocketTransportAddress;  
  10. import org.elasticsearch.common.unit.TimeValue;  
  11. import org.elasticsearch.index.query.QueryBuilder;  
  12. import org.elasticsearch.index.query.QueryBuilders;  
  13. import org.elasticsearch.search.SearchHit;  
  14.   
  15. public class ESBulkDataApi {  
  16.   
  17.     public static void main(String[] args) {  
  18.         // establish the client  
  19.         Settings settings = ImmutableSettings.settingsBuilder()  
  20.                 .put("cluster.name", "genbank").build();  
  21.         @SuppressWarnings("resource")  
  22.         Client client = new TransportClient(settings)  
  23.                 .addTransportAddress(new InetSocketTransportAddress(  
  24.                         "10.0.26.1", 9300));  
  25.         QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(  
  26.                 QueryBuilders.matchAllQuery());  
  27.   
  28.         SearchResponse searchResponse = client.prepareSearch("liu")  
  29.                 .setTypes("seqs").setSearchType(SearchType.SCAN)  
  30.                 .setScroll(new TimeValue(60000)).setQuery(queryBuilder)  
  31.                 .setSize(100).execute().actionGet();  
  32.   
  33.         //scroll to get the data  
  34.         while (true) {  
  35.   
  36.             searchResponse = client  
  37.                     .prepareSearchScroll(searchResponse.getScrollId())  
  38.                     .setScroll(new TimeValue(600000)).execute().actionGet();  
  39.               
  40.   
  41.             for (SearchHit hit : searchResponse.getHits().getHits()) {  
  42.                 // copy the data to the new index  
  43.                   
  44.                 client.prepareIndex("my_index_v1", "seqs", hit.getId())  
  45.                         .setSource(hit.getSourceAsString()).execute().actionGet();  
  46.             }  
  47.   
  48.             // when there is no data,break the loop  
  49.             if (searchResponse.getHits().getHits().length == 0) {  
  50.                 break;  
  51.             }  
  52.   
  53.         }  
  54.     }  
  55.   
  56. }  
 
 

猜你喜欢

转载自hunan.iteye.com/blog/2386665