11package com .pannous .es .reindex ;
22
3- import java .io .IOException ;
4- import java .util .ArrayList ;
5- import java .util .Collection ;
6- import java .util .Collections ;
7- import java .util .List ;
8- import org .elasticsearch .action .admin .indices .flush .FlushRequest ;
93import org .elasticsearch .action .bulk .BulkItemResponse ;
104import org .elasticsearch .action .bulk .BulkRequestBuilder ;
115import org .elasticsearch .action .bulk .BulkResponse ;
159import org .elasticsearch .action .search .SearchType ;
1610import org .elasticsearch .client .Client ;
1711import org .elasticsearch .client .Requests ;
18- import org .elasticsearch .common .StopWatch ;
1912import org .elasticsearch .common .inject .Inject ;
2013import org .elasticsearch .common .settings .Settings ;
2114import org .elasticsearch .common .unit .TimeValue ;
15+ import org .elasticsearch .common .util .concurrent .EsExecutors ;
2216import org .elasticsearch .common .xcontent .XContentBuilder ;
23- import org .elasticsearch .rest .BaseRestHandler ;
24- import org .elasticsearch .rest .RestChannel ;
25- import org .elasticsearch .rest .RestController ;
26- import org .elasticsearch .rest .RestRequest ;
27- import org .elasticsearch .rest .XContentRestResponse ;
28- import org .elasticsearch .rest .XContentThrowableRestResponse ;
29- import static org .elasticsearch .rest .RestRequest .Method .*;
30- import static org .elasticsearch .rest .RestStatus .*;
31- import static org .elasticsearch .rest .action .support .RestXContentBuilder .*;
17+ import org .elasticsearch .rest .*;
18+
19+ import java .io .IOException ;
20+ import java .util .ArrayList ;
21+ import java .util .Collection ;
22+ import java .util .Collections ;
23+ import java .util .List ;
24+
25+ import static org .elasticsearch .rest .RestRequest .Method .POST ;
26+ import static org .elasticsearch .rest .RestRequest .Method .PUT ;
27+ import static org .elasticsearch .rest .RestStatus .OK ;
28+ import static org .elasticsearch .rest .action .support .RestXContentBuilder .restContentBuilder ;
3229
3330/**
3431 * Refeeds all the documents which matches the type and the (optional) query.
3734 */
3835public class ReIndexAction extends BaseRestHandler {
3936
40- @ Inject public ReIndexAction (Settings settings , Client client , RestController controller ) {
37+ @ Inject
38+ public ReIndexAction (Settings settings , Client client , RestController controller ) {
4139 super (settings , client );
4240
4341 if (controller != null ) {
@@ -47,7 +45,8 @@ public class ReIndexAction extends BaseRestHandler {
4745 }
4846 }
4947
50- @ Override public void handleRequest (RestRequest request , RestChannel channel ) {
48+ @ Override
49+ public void handleRequest (RestRequest request , RestChannel channel ) {
5150 handleRequest (request , channel , null , false );
5251 }
5352
@@ -111,7 +110,7 @@ public void handleRequest(RestRequest request, RestChannel channel, String newTy
111110 }
112111
113112 public SearchRequestBuilder createScrollSearch (String oldIndexName , String oldType , String filter ,
114- int hitsPerPage , boolean withVersion , int keepTimeInMinutes ) {
113+ int hitsPerPage , boolean withVersion , int keepTimeInMinutes ) {
115114 SearchRequestBuilder srb = client .prepareSearch (oldIndexName ).
116115 setTypes (oldType ).
117116 setVersion (withVersion ).
@@ -124,80 +123,14 @@ public SearchRequestBuilder createScrollSearch(String oldIndexName, String oldTy
124123 return srb ;
125124 }
126125
127- public int reindex (MySearchResponse rsp , String newIndex , String newType , boolean withVersion ,
128- float waitSeconds ) {
129- boolean flushEnabled = false ;
130- long total = rsp .hits ().totalHits ();
131- int collectedResults = 0 ;
132- int failed = 0 ;
133- while (true ) {
134- if (collectedResults > 0 && waitSeconds > 0 ) {
135- try {
136- Thread .sleep (Math .round (waitSeconds * 1000 ));
137- } catch (InterruptedException ex ) {
138- break ;
139- }
140- }
141- StopWatch queryWatch = new StopWatch ().start ();
142- int currentResults = rsp .doScoll ();
143- if (currentResults == 0 )
144- break ;
145-
146- MySearchHits res = callback (rsp .hits ());
147- if (res == null )
148- break ;
149- queryWatch .stop ();
150- StopWatch updateWatch = new StopWatch ().start ();
151- failed += bulkUpdate (res , newIndex , newType , withVersion ).size ();
152- if (flushEnabled )
153- client .admin ().indices ().flush (new FlushRequest (newIndex )).actionGet ();
154-
155- updateWatch .stop ();
156- collectedResults += currentResults ;
157- logger .debug ("Progress " + collectedResults + "/" + total
158- + ". Time of update:" + updateWatch .totalTime ().getSeconds () + " query:"
159- + queryWatch .totalTime ().getSeconds () + " failed:" + failed );
160- }
161- String str = "found " + total + ", collected:" + collectedResults
162- + ", transfered:" + (float ) rsp .bytes () / (1 << 20 ) + "MB" ;
163- if (failed > 0 )
164- logger .warn (failed + " FAILED documents! " + str );
165- else
166- logger .info (str );
167- return collectedResults ;
168- }
169-
170- Collection <Integer > bulkUpdate (MySearchHits objects , String indexName ,
171- String newType , boolean withVersion ) {
172- BulkRequestBuilder brb = client .prepareBulk ();
173- for (MySearchHit hit : objects .getHits ()) {
174- if (hit .id () == null || hit .id ().isEmpty ()) {
175- logger .warn ("Skipped object without id when bulkUpdate:" + hit );
176- continue ;
177- }
126+ public Thread reindex (MySearchResponse rsp , String newIndex , String newType , boolean withVersion ,
127+ float waitSeconds ) {
178128
179- try {
180- IndexRequest indexReq = Requests .indexRequest (indexName ).type (newType ).id (hit .id ()).source (hit .source ());
181- if (withVersion )
182- indexReq .version (hit .version ());
129+ Indexer indexer = new Indexer (client , rsp , newIndex , newType , withVersion , waitSeconds );
130+ Thread indexerThread = EsExecutors .daemonThreadFactory (settings , this .getClass ().getCanonicalName ()).newThread (indexer );
131+ indexerThread .start ();
183132
184- brb .add (indexReq );
185- } catch (Exception ex ) {
186- logger .warn ("Cannot add object:" + hit + " to bulkIndexing action." + ex .getMessage ());
187- }
188- }
189- if (brb .numberOfActions () > 0 ) {
190- BulkResponse rsp = brb .execute ().actionGet ();
191- if (rsp .hasFailures ()) {
192- List <Integer > list = new ArrayList <Integer >(rsp .items ().length );
193- for (BulkItemResponse br : rsp .items ()) {
194- if (br .isFailed ())
195- list .add (br .itemId ());
196- }
197- return list ;
198- }
199- }
200- return Collections .emptyList ();
133+ return indexerThread ;
201134 }
202135
203136 protected MySearchHits callback (MySearchHits hits ) {
0 commit comments