Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a request level flag to control Query Cache #7167

Merged
merged 1 commit into from Aug 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to like migrating such code to ParseFied when adding features. It avoids typos in the camelcase name that wouldn't be caught by tests for example

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