mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 10:25:15 +00:00
use a private rewrite context to prevent exposing isCachable
This commit is contained in:
parent
7ba22bb75b
commit
e556c289b9
@ -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 {
|
||||||
|
@ -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();
|
||||||
|
@ -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);
|
||||||
|
@ -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 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user