55
55
import org .elasticsearch .index .query .xcontent .QueryBuilders ;
56
56
import org .elasticsearch .index .service .IndexService ;
57
57
import org .elasticsearch .index .settings .IndexSettings ;
58
- import org .elasticsearch .index .shard .ShardId ;
59
58
import org .elasticsearch .index .shard .service .IndexShard ;
60
- import org .elasticsearch .indices .IndicesLifecycle ;
59
+ import org .elasticsearch .indices .IndicesService ;
61
60
62
61
import java .io .IOException ;
63
62
import java .io .Reader ;
@@ -170,13 +169,7 @@ public List<String> matches() {
170
169
private volatile ImmutableMap <String , Query > queries = ImmutableMap .of ();
171
170
172
171
173
- private final PercolatorIndexAndShardListener percolatorIndexAndShardListener = new PercolatorIndexAndShardListener ();
174
-
175
- private volatile IndicesLifecycle indicesLifecycle ;
176
-
177
- private volatile IndexService percolatorIndex ;
178
-
179
- private volatile IndexShard percolatorShard ;
172
+ private IndicesService indicesService ;
180
173
181
174
@ Inject public PercolatorExecutor (Index index , @ IndexSettings Settings indexSettings ,
182
175
MapperService mapperService , IndexQueryParserService queryParserService ,
@@ -187,17 +180,11 @@ public List<String> matches() {
187
180
this .indexCache = indexCache ;
188
181
}
189
182
190
- public void setIndicesLifecycle (IndicesLifecycle indicesLifecycle ) {
191
- this .indicesLifecycle = indicesLifecycle ;
192
- if (indicesLifecycle != null ) {
193
- indicesLifecycle .addListener (percolatorIndexAndShardListener );
194
- }
183
+ public void setIndicesService (IndicesService indicesService ) {
184
+ this .indicesService = indicesService ;
195
185
}
196
186
197
187
public synchronized void close () {
198
- if (indicesLifecycle != null ) {
199
- indicesLifecycle .removeListener (percolatorIndexAndShardListener );
200
- }
201
188
ImmutableMap <String , Query > old = queries ;
202
189
queries = ImmutableMap .of ();
203
190
old .clear ();
@@ -362,9 +349,14 @@ public Response percolate(DocAndQueryRequest request) throws ElasticSearchExcept
362
349
}
363
350
}
364
351
} else {
365
- if (percolatorIndex == null || percolatorShard == null ) {
352
+ IndexService percolatorIndex = indicesService .indexService (PercolatorService .INDEX_NAME );
353
+ if (percolatorIndex == null ) {
366
354
throw new PercolateIndexUnavailable (new Index (PercolatorService .INDEX_NAME ));
367
355
}
356
+ if (percolatorIndex .numberOfShards () == 0 ) {
357
+ throw new PercolateIndexUnavailable (new Index (PercolatorService .INDEX_NAME ));
358
+ }
359
+ IndexShard percolatorShard = percolatorIndex .shard (0 );
368
360
Engine .Searcher percolatorSearcher = percolatorShard .searcher ();
369
361
try {
370
362
percolatorSearcher .searcher ().search (request .query (), new QueryCollector (logger , queries , searcher , percolatorIndex , matches ));
@@ -380,32 +372,6 @@ public Response percolate(DocAndQueryRequest request) throws ElasticSearchExcept
380
372
return new Response (matches , request .doc ().mappersAdded ());
381
373
}
382
374
383
- class PercolatorIndexAndShardListener extends IndicesLifecycle .Listener {
384
- @ Override public void afterIndexCreated (IndexService indexService ) {
385
- if (indexService .index ().name ().equals (PercolatorService .INDEX_NAME )) {
386
- percolatorIndex = indexService ;
387
- }
388
- }
389
-
390
- @ Override public void afterIndexClosed (Index index , boolean delete ) {
391
- if (index .name ().equals (PercolatorService .INDEX_NAME )) {
392
- percolatorIndex = null ;
393
- }
394
- }
395
-
396
- @ Override public void afterIndexShardCreated (IndexShard indexShard ) {
397
- if (indexShard .shardId ().index ().name ().equals (PercolatorService .INDEX_NAME )) {
398
- percolatorShard = indexShard ;
399
- }
400
- }
401
-
402
- @ Override public void afterIndexShardClosed (ShardId shardId , boolean delete ) {
403
- if (shardId .index ().name ().equals (PercolatorService .INDEX_NAME )) {
404
- percolatorShard = null ;
405
- }
406
- }
407
- }
408
-
409
375
static class QueryCollector extends Collector {
410
376
private final IndexSearcher searcher ;
411
377
private final IndexService percolatorIndex ;
0 commit comments