use a private rewrite context to prevent exposing isCachable

This commit is contained in:
Simon Willnauer 2016-10-05 11:41:49 +02:00
parent 7ba22bb75b
commit e556c289b9
4 changed files with 10 additions and 21 deletions

View File

@ -119,18 +119,13 @@ public class QueryRewriteContext implements ParseFieldMatcherSupplier {
return new QueryParseContext(defaultScriptLanguage, indicesQueriesRegistry, parser, indexSettings.getParseFieldMatcher()); return new QueryParseContext(defaultScriptLanguage, indicesQueriesRegistry, parser, indexSettings.getParseFieldMatcher());
} }
protected final void markAsNotCachable() { /**
this.cachable = false; * Returns <code>true</code> iff the result of the processed search request is cachable. Otherwise <code>false</code>
} */
public boolean isCachable() { public boolean isCachable() {
return cachable; return cachable;
} }
public void setCachable(boolean cachabe) {
this.cachable = cachabe;
}
public BytesReference getTemplateBytes(Script template) { public BytesReference getTemplateBytes(Script template) {
failIfExecutionMode(); failIfExecutionMode();
ExecutableScript executable = scriptService.executable(template, ExecutableScript executable = scriptService.executable(template,
@ -143,7 +138,7 @@ public class QueryRewriteContext implements ParseFieldMatcherSupplier {
} }
protected void failIfExecutionMode() { protected void failIfExecutionMode() {
markAsNotCachable(); this.cachable = false;
if (executionMode.get() == Boolean.TRUE) { if (executionMode.get() == Boolean.TRUE) {
throw new IllegalArgumentException("features that prevent cachability are disabled on this context"); throw new IllegalArgumentException("features that prevent cachability are disabled on this context");
} else { } else {

View File

@ -521,13 +521,7 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv
DefaultSearchContext context = createSearchContext(request, defaultSearchTimeout, searcher); DefaultSearchContext context = createSearchContext(request, defaultSearchTimeout, searcher);
SearchContext.setCurrent(context); SearchContext.setCurrent(context);
try { try {
request.rewrite(context.getQueryShardContext()); request.rewrite(new QueryShardContext(context.getQueryShardContext()));
// reset that we have used nowInMillis from the context since it may
// have been rewritten so its no longer in the query and the request can
// be cached. If it is still present in the request (e.g. in a range
// aggregation) it will still be caught when the aggregation is
// evaluated.
context.resetCanCache();
if (request.scroll() != null) { if (request.scroll() != null) {
context.scrollContext(new ScrollContext()); context.scrollContext(new ScrollContext());
context.scrollContext().scroll = request.scroll(); context.scrollContext().scroll = request.scroll();

View File

@ -163,10 +163,6 @@ public abstract class SearchContext extends AbstractRefCounted implements Releas
return getQueryShardContext().isCachable(); return getQueryShardContext().isCachable();
} }
public final void resetCanCache() {
getQueryShardContext().setCachable(true);
}
public abstract ScrollContext scrollContext(); public abstract ScrollContext scrollContext();
public abstract SearchContext scrollContext(ScrollContext scroll); public abstract SearchContext scrollContext(ScrollContext scroll);

View File

@ -585,7 +585,11 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
QB firstQuery = createTestQueryBuilder(); QB firstQuery = createTestQueryBuilder();
QB controlQuery = copyQuery(firstQuery); QB controlQuery = copyQuery(firstQuery);
setSearchContext(randomTypes, context); // only set search context for toQuery to be more realistic setSearchContext(randomTypes, context); // only set search context for toQuery to be more realistic
Query firstLuceneQuery = rewriteQuery(firstQuery, context).toQuery(context); /* we use a private rewrite context here since we want the most realistic way of asserting that we are cachabel or not.
* We do it this way in SearchService where
* we first rewrite the query with a private context, then reset the context and then build the actual lucene query*/
QueryBuilder rewritten = rewriteQuery(firstQuery, new QueryShardContext(context));
Query firstLuceneQuery = rewritten.toQuery(context);
if (isCachable(firstQuery)) { if (isCachable(firstQuery)) {
assert context.isCachable() : firstQuery.toString(); assert context.isCachable() : firstQuery.toString();
} else { } else {