Skip to content

Commit

Permalink
[Query Cache] Add a request level flag to control query cache
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kimchy authored and areek committed Sep 8, 2014
1 parent 36f4dba commit 2a66f2c
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 2 deletions.
4 changes: 4 additions & 0 deletions rest-api-spec/api/search.json
Expand Up @@ -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"
}
}
},
Expand Down
Expand Up @@ -137,6 +137,8 @@ public MultiSearchRequest add(BytesReference data, boolean contentUnsafe, @Nulla
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)) {
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/elasticsearch/action/search/SearchRequest.java
Expand Up @@ -81,6 +81,7 @@ public class SearchRequest extends ActionRequest<SearchRequest> implements Indic

private BytesReference extraSource;
private boolean extraSourceUnsafe;
private Boolean queryCache;

private Scroll scroll;

Expand Down Expand Up @@ -493,6 +494,20 @@ public SearchRequest scroll(String keepAlive) {
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);
Expand Down Expand Up @@ -533,6 +548,10 @@ public void readFrom(StreamInput in) throws IOException {
templateParams = (Map<String, String>) in.readGenericValue();
}
}

if (in.getVersion().onOrAfter(Version.V_1_4_0)) {
queryCache = in.readOptionalBoolean();
}
}

@Override
Expand Down Expand Up @@ -574,5 +593,9 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeGenericValue(templateParams);
}
}

if (out.getVersion().onOrAfter(Version.V_1_4_0)) {
out.writeOptionalBoolean(queryCache);
}
}
}
Expand Up @@ -1068,6 +1068,16 @@ public SearchRequestBuilder setTemplateSource(BytesReference source) {
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
Expand Down
Expand Up @@ -187,7 +187,12 @@ public boolean canCache(ShardSearchRequest request, SearchContext context) {
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
Expand All @@ -199,7 +204,6 @@ public boolean canCache(ShardSearchRequest request, SearchContext context) {
if (context.nowInMillisUsed()) {
return false;
}
// TODO allow to have a queryCache level flag on the request as well
return true;
}

Expand Down
Expand Up @@ -109,6 +109,7 @@ public static SearchRequest parseSearchRequest(RestRequest request) {

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) {
Expand Down
Expand Up @@ -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;

Expand All @@ -101,6 +102,7 @@ public ShardSearchRequest(SearchRequest searchRequest, ShardRouting shardRouting
this.scroll = searchRequest.scroll();
this.types = searchRequest.types();
this.useSlowScroll = useSlowScroll;
this.queryCache = searchRequest.queryCache();
}

public ShardSearchRequest(ShardRouting shardRouting, int numberOfShards, SearchType searchType) {
Expand Down Expand Up @@ -221,6 +223,10 @@ public boolean useSlowScroll() {
return useSlowScroll;
}

public Boolean queryCache() {
return this.queryCache;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
Expand Down Expand Up @@ -255,6 +261,10 @@ public void readFrom(StreamInput in) throws IOException {
// 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
Expand Down Expand Up @@ -299,5 +309,9 @@ public void writeTo(StreamOutput out, boolean asKey) throws IOException {
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);
}
}
}
19 changes: 19 additions & 0 deletions src/test/java/org/elasticsearch/indices/stats/IndexStatsTests.java
Expand Up @@ -236,6 +236,25 @@ public void run() {

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
Expand Down

0 comments on commit 2a66f2c

Please sign in to comment.