|
13 | 13 | import org.apache.lucene.analysis.TokenStream; |
14 | 14 | import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; |
15 | 15 | import org.apache.lucene.util.SetOnce; |
| 16 | +import org.elasticsearch.ElasticsearchStatusException; |
| 17 | +import org.elasticsearch.rest.RestStatus; |
16 | 18 |
|
17 | 19 | import java.io.IOException; |
| 20 | +import java.util.concurrent.atomic.AtomicReference; |
18 | 21 |
|
19 | 22 | public class StopRemoteWordListFilter extends FilteringTokenFilter { |
20 | 23 | private final SetOnce<CharArraySet> stopWordsHolder; |
| 24 | + private final AtomicReference<Exception> asyncInitException; |
21 | 25 | private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class); |
22 | 26 |
|
23 | | - public StopRemoteWordListFilter(TokenStream in, SetOnce<CharArraySet> stopWordsHolder) { |
| 27 | + public StopRemoteWordListFilter(TokenStream in, SetOnce<CharArraySet> stopWordsHolder, AtomicReference<Exception> asyncInitException) { |
24 | 28 | super(in); |
25 | 29 | this.stopWordsHolder = stopWordsHolder; |
| 30 | + this.asyncInitException = asyncInitException; |
26 | 31 | } |
27 | 32 |
|
28 | 33 | @Override |
29 | 34 | protected boolean accept() throws IOException { |
30 | 35 | // TODO: Would be nice if we could wrap a StopFilter instance and call its accept method, but it has protected access |
31 | 36 | CharArraySet stopWords = stopWordsHolder.get(); |
| 37 | + Exception asyncInitException = this.asyncInitException.get(); |
32 | 38 | if (stopWords == null) { |
33 | | - throw new IllegalStateException("Stop filter not initialized yet"); |
| 39 | + throw asyncInitException != null |
| 40 | + ? new ElasticsearchStatusException("Stop filter async initialization failed", RestStatus.INTERNAL_SERVER_ERROR, asyncInitException) |
| 41 | + : new ElasticsearchStatusException("Stop filter not initialized yet", RestStatus.CONFLICT); |
34 | 42 | } |
35 | 43 |
|
36 | 44 | return stopWords.contains(termAtt.buffer(), 0, termAtt.length()) == false; |
|
0 commit comments