|
53 | 53 | import org.apache.lucene.search.LeafCollector;
|
54 | 54 | import org.apache.lucene.search.MatchAllDocsQuery;
|
55 | 55 | import org.apache.lucene.search.MatchNoDocsQuery;
|
| 56 | +import org.apache.lucene.search.MultiTermQuery; |
| 57 | +import org.apache.lucene.search.PrefixQuery; |
56 | 58 | import org.apache.lucene.search.Query;
|
57 | 59 | import org.apache.lucene.search.ScoreDoc;
|
58 | 60 | import org.apache.lucene.search.Sort;
|
|
75 | 77 | import org.elasticsearch.index.mapper.MapperService;
|
76 | 78 | import org.elasticsearch.index.mapper.NumberFieldMapper;
|
77 | 79 | import org.elasticsearch.index.query.ParsedQuery;
|
| 80 | +import org.elasticsearch.index.query.QueryShardContext; |
78 | 81 | import org.elasticsearch.index.search.ESToParentBlockJoinQuery;
|
79 | 82 | import org.elasticsearch.index.shard.IndexShard;
|
80 | 83 | import org.elasticsearch.index.shard.IndexShardTestCase;
|
|
83 | 86 | import org.elasticsearch.search.internal.ScrollContext;
|
84 | 87 | import org.elasticsearch.search.internal.SearchContext;
|
85 | 88 | import org.elasticsearch.search.sort.SortAndFormats;
|
| 89 | +import org.elasticsearch.tasks.TaskCancelledException; |
86 | 90 | import org.elasticsearch.test.TestSearchContext;
|
87 | 91 |
|
88 | 92 | import java.io.IOException;
|
@@ -825,7 +829,55 @@ public void testMinScore() throws Exception {
|
825 | 829 |
|
826 | 830 | reader.close();
|
827 | 831 | dir.close();
|
| 832 | + } |
| 833 | + |
| 834 | + public void testCancellationDuringPreprocess() throws IOException { |
| 835 | + try (Directory dir = newDirectory(); |
| 836 | + RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig())) { |
| 837 | + |
| 838 | + for (int i = 0; i < 10; i++) { |
| 839 | + Document doc = new Document(); |
| 840 | + doc.add(new StringField("foo", "a".repeat(i), Store.NO)); |
| 841 | + w.addDocument(doc); |
| 842 | + } |
| 843 | + w.flush(); |
| 844 | + w.close(); |
| 845 | + |
| 846 | + IndexReader reader = DirectoryReader.open(dir); |
| 847 | + TestSearchContext context = new TestSearchContextWithRewriteAndCancellation( |
| 848 | + null, indexShard, newContextSearcher(reader)); |
| 849 | + PrefixQuery prefixQuery = new PrefixQuery(new Term("foo", "a")); |
| 850 | + prefixQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE); |
| 851 | + context.parsedQuery(new ParsedQuery(prefixQuery)); |
| 852 | + SearchShardTask task = mock(SearchShardTask.class); |
| 853 | + when(task.isCancelled()).thenReturn(true); |
| 854 | + context.setTask(task); |
| 855 | + expectThrows(TaskCancelledException.class, () -> new QueryPhase().preProcess(context)); |
| 856 | + |
| 857 | + reader.close(); |
| 858 | + } |
| 859 | + } |
828 | 860 |
|
| 861 | + private static class TestSearchContextWithRewriteAndCancellation extends TestSearchContext { |
| 862 | + |
| 863 | + public TestSearchContextWithRewriteAndCancellation(QueryShardContext queryShardContext, IndexShard indexShard, |
| 864 | + ContextIndexSearcher searcher) { |
| 865 | + super(queryShardContext, indexShard, searcher); |
| 866 | + } |
| 867 | + |
| 868 | + @Override |
| 869 | + public void preProcess(boolean rewrite) { |
| 870 | + try { |
| 871 | + searcher().rewrite(query()); |
| 872 | + } catch (IOException e) { |
| 873 | + fail("IOException shouldn't be thrown"); |
| 874 | + } |
| 875 | + } |
| 876 | + |
| 877 | + @Override |
| 878 | + public boolean lowLevelCancellation() { |
| 879 | + return true; |
| 880 | + } |
829 | 881 | }
|
830 | 882 |
|
831 | 883 | private static ContextIndexSearcher newContextSearcher(IndexReader reader) throws IOException {
|
|
0 commit comments