|
10 | 10 |
|
11 | 11 | import com.fasterxml.jackson.core.JsonParseException; |
12 | 12 |
|
| 13 | +import org.apache.lucene.document.Document; |
| 14 | +import org.apache.lucene.index.LeafReaderContext; |
| 15 | +import org.apache.lucene.index.RandomIndexWriter; |
13 | 16 | import org.apache.lucene.index.Term; |
| 17 | +import org.apache.lucene.search.IndexSearcher; |
14 | 18 | import org.apache.lucene.search.MatchAllDocsQuery; |
15 | 19 | import org.apache.lucene.search.MatchNoDocsQuery; |
16 | 20 | import org.apache.lucene.search.Query; |
| 21 | +import org.apache.lucene.search.ScoreMode; |
17 | 22 | import org.apache.lucene.search.TermQuery; |
| 23 | +import org.apache.lucene.search.Weight; |
| 24 | +import org.apache.lucene.store.Directory; |
18 | 25 | import org.elasticsearch.common.ParsingException; |
19 | 26 | import org.elasticsearch.common.Strings; |
20 | 27 | import org.elasticsearch.common.bytes.BytesReference; |
|
70 | 77 | import static org.hamcrest.Matchers.closeTo; |
71 | 78 | import static org.hamcrest.Matchers.containsString; |
72 | 79 | import static org.hamcrest.Matchers.equalTo; |
| 80 | +import static org.hamcrest.Matchers.greaterThan; |
73 | 81 | import static org.hamcrest.Matchers.instanceOf; |
74 | 82 | import static org.hamcrest.Matchers.nullValue; |
75 | 83 |
|
@@ -815,34 +823,72 @@ public List<ScoreFunctionSpec<?>> getScoreFunctions() { |
815 | 823 | */ |
816 | 824 | @Override |
817 | 825 | public void testCacheability() throws IOException { |
| 826 | + Directory directory = newDirectory(); |
| 827 | + RandomIndexWriter iw = new RandomIndexWriter(random(), directory); |
| 828 | + iw.addDocument(new Document()); |
| 829 | + final IndexSearcher searcher = new IndexSearcher(iw.getReader()); |
| 830 | + iw.close(); |
| 831 | + assertThat(searcher.getIndexReader().leaves().size(), greaterThan(0)); |
| 832 | + |
818 | 833 | FunctionScoreQueryBuilder queryBuilder = createTestQueryBuilder(); |
819 | | - boolean isCacheable = isCacheable(queryBuilder); |
820 | | - SearchExecutionContext context = createSearchExecutionContext(); |
| 834 | + boolean requestCache = isCacheable(queryBuilder); |
| 835 | + SearchExecutionContext context = createSearchExecutionContext(searcher); |
821 | 836 | QueryBuilder rewriteQuery = rewriteQuery(queryBuilder, new SearchExecutionContext(context)); |
822 | 837 | assertNotNull(rewriteQuery.toQuery(context)); |
823 | | - // we occasionally need to update the expected "isCacheable" flag after rewrite for MatchNoneQueryBuilder |
| 838 | + // we occasionally need to update the expected request cache flag after rewrite to MatchNoneQueryBuilder |
824 | 839 | if (rewriteQuery instanceof MatchNoneQueryBuilder) { |
825 | | - isCacheable = true; |
| 840 | + requestCache = true; |
| 841 | + } |
| 842 | + assertEquals("query should " + (requestCache ? "" : "not") + " be eligible for the request cache: " + queryBuilder.toString(), |
| 843 | + requestCache, context.isCacheable()); |
| 844 | + |
| 845 | + // test query cache |
| 846 | + if (rewriteQuery instanceof MatchNoneQueryBuilder == false) { |
| 847 | + Query luceneQuery = rewriteQuery.toQuery(context); |
| 848 | + Weight queryWeight = context.searcher().createWeight(searcher.rewrite(luceneQuery), ScoreMode.COMPLETE, 1.0f); |
| 849 | + for (LeafReaderContext ctx : context.getIndexReader().leaves()) { |
| 850 | + assertFalse(queryWeight.isCacheable(ctx)); |
| 851 | + } |
826 | 852 | } |
827 | | - assertEquals("query should " + (isCacheable ? "" : "not") + " be cacheable: " + queryBuilder.toString(), isCacheable, |
828 | | - context.isCacheable()); |
829 | 853 |
|
830 | 854 | ScoreFunctionBuilder<?> scriptScoreFunction = new ScriptScoreFunctionBuilder( |
831 | 855 | new Script(ScriptType.INLINE, MockScriptEngine.NAME, "1", Collections.emptyMap())); |
832 | 856 | queryBuilder = new FunctionScoreQueryBuilder(new FilterFunctionBuilder[] { |
833 | 857 | new FilterFunctionBuilder(RandomQueryBuilder.createQuery(random()), scriptScoreFunction) }); |
834 | | - context = createSearchExecutionContext(); |
| 858 | + context = createSearchExecutionContext(searcher); |
835 | 859 | rewriteQuery = rewriteQuery(queryBuilder, new SearchExecutionContext(context)); |
836 | 860 | assertNotNull(rewriteQuery.toQuery(context)); |
837 | | - assertTrue("function script query should be cacheable" + queryBuilder.toString(), context.isCacheable()); |
| 861 | + assertTrue("function script query should be eligible for the request cache: " + queryBuilder.toString(), |
| 862 | + context.isCacheable()); |
| 863 | + |
| 864 | + // test query cache |
| 865 | + if (rewriteQuery instanceof MatchNoneQueryBuilder == false) { |
| 866 | + Query luceneQuery = rewriteQuery.toQuery(context); |
| 867 | + Weight queryWeight = context.searcher().createWeight(searcher.rewrite(luceneQuery), ScoreMode.COMPLETE, 1.0f); |
| 868 | + for (LeafReaderContext ctx : context.getIndexReader().leaves()) { |
| 869 | + assertFalse(queryWeight.isCacheable(ctx)); |
| 870 | + } |
| 871 | + } |
838 | 872 |
|
839 | 873 | RandomScoreFunctionBuilder randomScoreFunctionBuilder = new RandomScoreFunctionBuilderWithFixedSeed(); |
840 | 874 | queryBuilder = new FunctionScoreQueryBuilder(new FilterFunctionBuilder[] { |
841 | 875 | new FilterFunctionBuilder(RandomQueryBuilder.createQuery(random()), randomScoreFunctionBuilder) }); |
842 | | - context = createSearchExecutionContext(); |
| 876 | + context = createSearchExecutionContext(searcher); |
843 | 877 | rewriteQuery = rewriteQuery(queryBuilder, new SearchExecutionContext(context)); |
844 | 878 | assertNotNull(rewriteQuery.toQuery(context)); |
845 | | - assertFalse("function random query should not be cacheable: " + queryBuilder.toString(), context.isCacheable()); |
| 879 | + assertFalse("function random query should not be eligible for the request cache: " + queryBuilder.toString(), |
| 880 | + context.isCacheable()); |
| 881 | + |
| 882 | + // test query cache |
| 883 | + if (rewriteQuery instanceof MatchNoneQueryBuilder == false) { |
| 884 | + Query luceneQuery = rewriteQuery.toQuery(context); |
| 885 | + Weight queryWeight = context.searcher().createWeight(searcher.rewrite(luceneQuery), ScoreMode.COMPLETE, 1.0f); |
| 886 | + for (LeafReaderContext ctx : context.getIndexReader().leaves()) { |
| 887 | + assertFalse(queryWeight.isCacheable(ctx)); |
| 888 | + } |
| 889 | + } |
| 890 | + searcher.getIndexReader().close(); |
| 891 | + directory.close(); |
846 | 892 | } |
847 | 893 |
|
848 | 894 | private boolean isCacheable(FunctionScoreQueryBuilder queryBuilder) { |
|
0 commit comments