[Query Cache] Add a request level flag to control query cache

A request level flag, defaults to be unset, to control the query cache. When not set, it defaults to the index level settings, when explicitly set, will override the index level setting
closes #7167
This commit is contained in:
Shay Banon 2014-08-05 18:28:01 +02:00
parent e6b459cb9f
commit e6e2781ee7
8 changed files with 79 additions and 2 deletions

View File

@ -146,6 +146,10 @@
"version": {
"type" : "boolean",
"description" : "Specify whether to return document version as part of a hit"
},
"query_cache": {
"type" : "boolean",
"description" : "Specify if query cache should be used for this request or not, defaults to index level setting"
}
}
},

View File

@ -137,6 +137,8 @@ public class MultiSearchRequest extends ActionRequest<MultiSearchRequest> implem
searchRequest.types(Strings.splitStringByCommaToArray(parser.text()));
} else if ("search_type".equals(currentFieldName) || "searchType".equals(currentFieldName)) {
searchRequest.searchType(parser.text());
} else if ("query_cache".equals(currentFieldName) || "queryCache".equals(currentFieldName)) {
searchRequest.queryCache(parser.booleanValue());
} else if ("preference".equals(currentFieldName)) {
searchRequest.preference(parser.text());
} else if ("routing".equals(currentFieldName)) {

View File

@ -81,6 +81,7 @@ public class SearchRequest extends ActionRequest<SearchRequest> implements Indic
private BytesReference extraSource;
private boolean extraSourceUnsafe;
private Boolean queryCache;
private Scroll scroll;
@ -493,6 +494,20 @@ public class SearchRequest extends ActionRequest<SearchRequest> implements Indic
return scroll(new Scroll(TimeValue.parseTimeValue(keepAlive, null)));
}
/**
* Sets if this request should use the query cache or not, assuming that it can (for
* example, if "now" is used, it will never be cached). By default (not set, or null,
* will default to the index level setting if query cache is enabled or not).
*/
public SearchRequest queryCache(Boolean queryCache) {
this.queryCache = queryCache;
return this;
}
public Boolean queryCache() {
return this.queryCache;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
@ -533,6 +548,10 @@ public class SearchRequest extends ActionRequest<SearchRequest> implements Indic
templateParams = (Map<String, String>) in.readGenericValue();
}
}
if (in.getVersion().onOrAfter(Version.V_1_4_0)) {
queryCache = in.readOptionalBoolean();
}
}
@Override
@ -574,5 +593,9 @@ public class SearchRequest extends ActionRequest<SearchRequest> implements Indic
out.writeGenericValue(templateParams);
}
}
if (out.getVersion().onOrAfter(Version.V_1_4_0)) {
out.writeOptionalBoolean(queryCache);
}
}
}

View File

@ -1068,6 +1068,16 @@ public class SearchRequestBuilder extends ActionRequestBuilder<SearchRequest, Se
return this;
}
/**
* Sets if this request should use the query cache or not, assuming that it can (for
* example, if "now" is used, it will never be cached). By default (not set, or null,
* will default to the index level setting if query cache is enabled or not).
*/
public SearchRequestBuilder setQueryCache(Boolean queryCache) {
request.queryCache(queryCache);
return this;
}
/**
* Sets the source builder to be used with this request. Note, any operations done
* on this require builder before are discarded as this internal builder replaces

View File

@ -187,7 +187,12 @@ public class IndicesQueryCache extends AbstractComponent implements RemovalListe
if (index == null) { // in case we didn't yet have the cluster state, or it just got deleted
return false;
}
if (!index.settings().getAsBoolean(INDEX_CACHE_QUERY_ENABLED, Boolean.FALSE)) {
// if not explicitly set in the request, use the index setting, if not, use the request
if (request.queryCache() == null) {
if (!index.settings().getAsBoolean(INDEX_CACHE_QUERY_ENABLED, Boolean.FALSE)) {
return false;
}
} else if (!request.queryCache()) {
return false;
}
// if the reader is not a directory reader, we can't get the version from it
@ -199,7 +204,6 @@ public class IndicesQueryCache extends AbstractComponent implements RemovalListe
if (context.nowInMillisUsed()) {
return false;
}
// TODO allow to have a queryCache level flag on the request as well
return true;
}

View File

@ -109,6 +109,7 @@ public class RestSearchAction extends BaseRestHandler {
searchRequest.extraSource(parseSearchSource(request));
searchRequest.searchType(request.param("search_type"));
searchRequest.queryCache(request.paramAsBoolean("query_cache", null));
String scroll = request.param("scroll");
if (scroll != null) {

View File

@ -78,6 +78,7 @@ public class ShardSearchRequest extends TransportRequest implements IndicesReque
private String templateName;
private ScriptService.ScriptType templateType;
private Map<String, String> templateParams;
private Boolean queryCache;
private long nowInMillis;
@ -101,6 +102,7 @@ public class ShardSearchRequest extends TransportRequest implements IndicesReque
this.scroll = searchRequest.scroll();
this.types = searchRequest.types();
this.useSlowScroll = useSlowScroll;
this.queryCache = searchRequest.queryCache();
}
public ShardSearchRequest(ShardRouting shardRouting, int numberOfShards, SearchType searchType) {
@ -221,6 +223,10 @@ public class ShardSearchRequest extends TransportRequest implements IndicesReque
return useSlowScroll;
}
public Boolean queryCache() {
return this.queryCache;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
@ -255,6 +261,10 @@ public class ShardSearchRequest extends TransportRequest implements IndicesReque
// This means that this request was send from a 1.0.x or 1.1.x node and we need to fallback to slow scroll.
useSlowScroll = in.getVersion().before(ParsedScrollId.SCROLL_SEARCH_AFTER_MINIMUM_VERSION);
}
if (in.getVersion().onOrAfter(Version.V_1_4_0)) {
queryCache = in.readOptionalBoolean();
}
}
@Override
@ -299,5 +309,9 @@ public class ShardSearchRequest extends TransportRequest implements IndicesReque
if (out.getVersion().onOrAfter(ParsedScrollId.SCROLL_SEARCH_AFTER_MINIMUM_VERSION)) {
out.writeBoolean(useSlowScroll);
}
if (out.getVersion().onOrAfter(Version.V_1_4_0)) {
out.writeOptionalBoolean(queryCache);
}
}
}

View File

@ -236,6 +236,25 @@ public class IndexStatsTests extends ElasticsearchIntegrationTest {
client().admin().indices().prepareClearCache().setQueryCache(true).get(); // clean the cache
assertThat(client().admin().indices().prepareStats("idx").setQueryCache(true).get().getTotal().getQueryCache().getMemorySizeInBytes(), equalTo(0l));
// test explicit request parameter
assertThat(client().prepareSearch("idx").setSearchType(SearchType.COUNT).setQueryCache(false).get().getHits().getTotalHits(), equalTo((long) numDocs));
assertThat(client().admin().indices().prepareStats("idx").setQueryCache(true).get().getTotal().getQueryCache().getMemorySizeInBytes(), equalTo(0l));
assertThat(client().prepareSearch("idx").setSearchType(SearchType.COUNT).setQueryCache(true).get().getHits().getTotalHits(), equalTo((long) numDocs));
assertThat(client().admin().indices().prepareStats("idx").setQueryCache(true).get().getTotal().getQueryCache().getMemorySizeInBytes(), greaterThan(0l));
// set the index level setting to false, and see that the reverse works
client().admin().indices().prepareClearCache().setQueryCache(true).get(); // clean the cache
assertAcked(client().admin().indices().prepareUpdateSettings("idx").setSettings(ImmutableSettings.builder().put(IndicesQueryCache.INDEX_CACHE_QUERY_ENABLED, false)));
assertThat(client().prepareSearch("idx").setSearchType(SearchType.COUNT).get().getHits().getTotalHits(), equalTo((long) numDocs));
assertThat(client().admin().indices().prepareStats("idx").setQueryCache(true).get().getTotal().getQueryCache().getMemorySizeInBytes(), equalTo(0l));
assertThat(client().prepareSearch("idx").setSearchType(SearchType.COUNT).setQueryCache(true).get().getHits().getTotalHits(), equalTo((long) numDocs));
assertThat(client().admin().indices().prepareStats("idx").setQueryCache(true).get().getTotal().getQueryCache().getMemorySizeInBytes(), greaterThan(0l));
}
@Test