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