Added test that verifies that p/c filters never cache.

Made the has_parent & has_child filterbuilder's cache options a noop as well, like it is in the related parsers.

Relates to #4757
This commit is contained in:
Martijn van Groningen 2014-01-22 13:34:53 +01:00
parent f466b8f292
commit 6cdbdaf388
3 changed files with 99 additions and 26 deletions

View File

@ -31,8 +31,6 @@ public class HasChildFilterBuilder extends BaseFilterBuilder {
private final QueryBuilder queryBuilder;
private String childType;
private String filterName;
private Boolean cache;
private String cacheKey;
private Integer shortCircuitCutoff;
public HasChildFilterBuilder(String type, QueryBuilder queryBuilder) {
@ -56,19 +54,16 @@ public class HasChildFilterBuilder extends BaseFilterBuilder {
}
/**
* Should the filter be cached or not. Defaults to <tt>false</tt>.
* This is a noop since has_child can't be cached.
*/
public HasChildFilterBuilder cache(boolean cache) {
this.cache = cache;
return this;
}
/**
* Defines what should be used as key to represent this filter in the filter cache.
* By default the filter itself is used as key.
* This is a noop since has_child can't be cached.
*/
public HasChildFilterBuilder cacheKey(String cacheKey) {
this.cacheKey = cacheKey;
return this;
}
@ -95,12 +90,6 @@ public class HasChildFilterBuilder extends BaseFilterBuilder {
if (filterName != null) {
builder.field("_name", filterName);
}
if (cache != null) {
builder.field("_cache", cache);
}
if (cacheKey != null) {
builder.field("_cache_key", cacheKey);
}
if (shortCircuitCutoff != null) {
builder.field("short_circuit_cutoff", shortCircuitCutoff);
}

View File

@ -31,8 +31,6 @@ public class HasParentFilterBuilder extends BaseFilterBuilder {
private final FilterBuilder filterBuilder;
private final String parentType;
private String filterName;
private Boolean cache;
private String cacheKey;
/**
* @param parentType The parent type
@ -63,19 +61,16 @@ public class HasParentFilterBuilder extends BaseFilterBuilder {
}
/**
* Should the filter be cached or not. Defaults to <tt>false</tt>.
* This is a noop since has_parent can't be cached.
*/
public HasParentFilterBuilder cache(boolean cache) {
this.cache = cache;
return this;
}
/**
* Defines what should be used as key to represent this filter in the filter cache.
* By default the filter itself is used as key.
* This is a noop since has_parent can't be cached.
*/
public HasParentFilterBuilder cacheKey(String cacheKey) {
this.cacheKey = cacheKey;
return this;
}
@ -93,12 +88,6 @@ public class HasParentFilterBuilder extends BaseFilterBuilder {
if (filterName != null) {
builder.field("_name", filterName);
}
if (cache != null) {
builder.field("_cache", cache);
}
if (cacheKey != null) {
builder.field("_cache_key", cacheKey);
}
builder.endObject();
}
}

View File

@ -51,12 +51,15 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import static com.google.common.collect.Maps.newHashMap;
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.FilterBuilders.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction;
import static org.elasticsearch.search.facet.FacetBuilders.termsFacet;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.hamcrest.Matchers.*;
/**
@ -2280,6 +2283,98 @@ public class SimpleChildQuerySearchTests extends ElasticsearchIntegrationTest {
}
}
@Test
public void testValidateThatHasChildAndHasParentFilterAreNeverCached() throws Exception {
assertAcked(client().admin().indices().prepareCreate("test")
.setSettings(SETTING_NUMBER_OF_SHARDS, 1, SETTING_NUMBER_OF_REPLICAS, 0)
.addMapping("child", "_parent", "type=parent"));
ensureGreen();
client().prepareIndex("test", "parent", "1").setSource("field", "value")
.get();
client().prepareIndex("test", "child", "1").setParent("1").setSource("field", "value")
.setRefresh(true)
.get();
SearchResponse searchResponse = client().prepareSearch("test")
.setQuery(hasChildQuery("child", matchAllQuery()))
.get();
assertHitCount(searchResponse, 1l);
searchResponse = client().prepareSearch("test")
.setQuery(hasParentQuery("parent", matchAllQuery()))
.get();
assertHitCount(searchResponse, 1l);
// Internally the has_child and has_parent use filter for the type field, which end up in the filter cache,
// so by first checking how much they take by executing has_child and has_parent *query* we can set a base line
// for the filter cache size in this test.
IndicesStatsResponse statsResponse = client().admin().indices().prepareStats("test").clear().setFilterCache(true).get();
long initialCacheSize = statsResponse.getIndex("test").getTotal().getFilterCache().getMemorySizeInBytes();
searchResponse = client().prepareSearch("test")
.setQuery(QueryBuilders.filteredQuery(matchAllQuery(), FilterBuilders.hasChildFilter("child", matchAllQuery()).cache(true)))
.get();
assertHitCount(searchResponse, 1l);
searchResponse = client().prepareSearch("test")
.setQuery(QueryBuilders.filteredQuery(matchAllQuery(), FilterBuilders.hasParentFilter("parent", matchAllQuery()).cache(true)))
.get();
assertHitCount(searchResponse, 1l);
// filter cache should not contain any thing, b/c has_child and has_parent can't be cached.
statsResponse = client().admin().indices().prepareStats("test").clear().setFilterCache(true).get();
assertThat(statsResponse.getIndex("test").getTotal().getFilterCache().getMemorySizeInBytes(), equalTo(initialCacheSize));
searchResponse = client().prepareSearch("test")
.setQuery(QueryBuilders.filteredQuery(
matchAllQuery(),
FilterBuilders.boolFilter().cache(true)
.must(FilterBuilders.matchAllFilter())
.must(FilterBuilders.hasChildFilter("child", matchAllQuery()).cache(true))
))
.get();
assertHitCount(searchResponse, 1l);
searchResponse = client().prepareSearch("test")
.setQuery(QueryBuilders.filteredQuery(
matchAllQuery(),
FilterBuilders.boolFilter().cache(true)
.must(FilterBuilders.matchAllFilter())
.must(FilterBuilders.hasParentFilter("parent", matchAllQuery()).cache(true))
))
.get();
assertHitCount(searchResponse, 1l);
// filter cache should not contain any thing, b/c has_child and has_parent can't be cached.
statsResponse = client().admin().indices().prepareStats("test").clear().setFilterCache(true).get();
assertThat(statsResponse.getIndex("test").getTotal().getFilterCache().getMemorySizeInBytes(), equalTo(initialCacheSize));
searchResponse = client().prepareSearch("test")
.setQuery(QueryBuilders.filteredQuery(
matchAllQuery(),
FilterBuilders.boolFilter().cache(true)
.must(FilterBuilders.termFilter("field", "value").cache(true))
.must(FilterBuilders.hasChildFilter("child", matchAllQuery()).cache(true))
))
.get();
assertHitCount(searchResponse, 1l);
searchResponse = client().prepareSearch("test")
.setQuery(QueryBuilders.filteredQuery(
matchAllQuery(),
FilterBuilders.boolFilter().cache(true)
.must(FilterBuilders.termFilter("field", "value").cache(true))
.must(FilterBuilders.hasParentFilter("parent", matchAllQuery()).cache(true))
))
.get();
assertHitCount(searchResponse, 1l);
// filter cache should not contain any thing, b/c has_child and has_parent can't be cached.
statsResponse = client().admin().indices().prepareStats("test").clear().setFilterCache(true).get();
assertThat(statsResponse.getIndex("test").getTotal().getFilterCache().getMemorySizeInBytes(), greaterThan(initialCacheSize));
}
private static HasChildFilterBuilder hasChildFilter(String type, QueryBuilder queryBuilder) {
HasChildFilterBuilder hasChildFilterBuilder = FilterBuilders.hasChildFilter(type, queryBuilder);
hasChildFilterBuilder.setShortCircuitCutoff(randomInt(10));