Added more unit like test that verifies that has_child is never cached even if wrapped by another filter.

This commit is contained in:
Martijn van Groningen 2014-01-24 12:03:00 +01:00
parent 2f32908193
commit 2d58603e81
6 changed files with 100 additions and 2 deletions

View File

@ -32,6 +32,7 @@ import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Injector; import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.inject.ModulesBuilder; import org.elasticsearch.common.inject.ModulesBuilder;
import org.elasticsearch.common.inject.util.Providers; import org.elasticsearch.common.inject.util.Providers;
import org.elasticsearch.common.lucene.search.AndFilter;
import org.elasticsearch.common.lucene.search.CachedFilter; import org.elasticsearch.common.lucene.search.CachedFilter;
import org.elasticsearch.common.lucene.search.NoCacheFilter; import org.elasticsearch.common.lucene.search.NoCacheFilter;
import org.elasticsearch.common.lucene.search.XBooleanFilter; import org.elasticsearch.common.lucene.search.XBooleanFilter;
@ -47,12 +48,14 @@ import org.elasticsearch.index.engine.IndexEngineModule;
import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.MapperServiceModule; import org.elasticsearch.index.mapper.MapperServiceModule;
import org.elasticsearch.index.query.functionscore.FunctionScoreModule; import org.elasticsearch.index.query.functionscore.FunctionScoreModule;
import org.elasticsearch.index.search.child.TestSearchContext;
import org.elasticsearch.index.settings.IndexSettingsModule; import org.elasticsearch.index.settings.IndexSettingsModule;
import org.elasticsearch.index.similarity.SimilarityModule; import org.elasticsearch.index.similarity.SimilarityModule;
import org.elasticsearch.indices.fielddata.breaker.CircuitBreakerService; import org.elasticsearch.indices.fielddata.breaker.CircuitBreakerService;
import org.elasticsearch.indices.fielddata.breaker.DummyCircuitBreakerService; import org.elasticsearch.indices.fielddata.breaker.DummyCircuitBreakerService;
import org.elasticsearch.indices.query.IndicesQueriesModule; import org.elasticsearch.indices.query.IndicesQueriesModule;
import org.elasticsearch.script.ScriptModule; import org.elasticsearch.script.ScriptModule;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.test.ElasticsearchTestCase; import org.elasticsearch.test.ElasticsearchTestCase;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.threadpool.ThreadPoolModule; import org.elasticsearch.threadpool.ThreadPoolModule;
@ -109,6 +112,8 @@ public class IndexQueryParserFilterCachingTests extends ElasticsearchTestCase {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/query/mapping.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/query/mapping.json");
injector.getInstance(MapperService.class).merge("person", new CompressedString(mapping), true); injector.getInstance(MapperService.class).merge("person", new CompressedString(mapping), true);
String childMapping = copyToStringFromClasspath("/org/elasticsearch/index/query/child-mapping.json");
injector.getInstance(MapperService.class).merge("child", new CompressedString(childMapping), true);
injector.getInstance(MapperService.class).documentMapper("person").parse(new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/query/data.json"))); injector.getInstance(MapperService.class).documentMapper("person").parse(new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/query/data.json")));
queryParser = injector.getInstance(IndexQueryParserService.class); queryParser = injector.getInstance(IndexQueryParserService.class);
} }
@ -152,6 +157,26 @@ public class IndexQueryParserFilterCachingTests extends ElasticsearchTestCase {
parsedQuery = queryParser.parse(query).query(); parsedQuery = queryParser.parse(query).query();
assertThat(parsedQuery, instanceOf(ConstantScoreQuery.class)); assertThat(parsedQuery, instanceOf(ConstantScoreQuery.class));
assertThat(((ConstantScoreQuery) parsedQuery).getFilter(), instanceOf(CachedFilter.class)); assertThat(((ConstantScoreQuery) parsedQuery).getFilter(), instanceOf(CachedFilter.class));
try {
SearchContext.setCurrent(new TestSearchContext());
query = copyToStringFromClasspath("/org/elasticsearch/index/query/has-child.json");
parsedQuery = queryParser.parse(query).query();
assertThat(parsedQuery, instanceOf(ConstantScoreQuery.class));
assertThat(((ConstantScoreQuery) parsedQuery).getFilter(), instanceOf(NoCacheFilter.class));
query = copyToStringFromClasspath("/org/elasticsearch/index/query/and-filter-cache.json");
parsedQuery = queryParser.parse(query).query();
assertThat(parsedQuery, instanceOf(ConstantScoreQuery.class));
assertThat(((ConstantScoreQuery) parsedQuery).getFilter(), instanceOf(CachedFilter.class));
query = copyToStringFromClasspath("/org/elasticsearch/index/query/has-child-in-and-filter-cached.json");
parsedQuery = queryParser.parse(query).query();
assertThat(parsedQuery, instanceOf(ConstantScoreQuery.class));
assertThat(((ConstantScoreQuery) parsedQuery).getFilter(), instanceOf(AndFilter.class));
} finally {
SearchContext.removeCurrent();
}
} }
} }

View File

@ -0,0 +1,21 @@
{
"filtered":{
"filter":{
"and":{
"filters":[
{
"term":{
"name.first":"shay1"
}
},
{
"term":{
"name.first":"shay4"
}
}
],
"_cache" : true
}
}
}
}

View File

@ -0,0 +1,12 @@
{
"child":{
"properties":{
"field":{
"type":"string"
}
},
"_parent" : {
"type" : "person"
}
}
}

View File

@ -0,0 +1,19 @@
{
"filtered":{
"filter":{
"and" : {
"filters" : [
{
"has_child" : {
"type" : "child",
"query" : {
"match_all" : {}
}
}
}
],
"_cache" : true
}
}
}
}

View File

@ -0,0 +1,13 @@
{
"filtered":{
"filter":{
"has_child" : {
"type" : "child",
"query" : {
"match_all" : {}
},
"_cache" : true
}
}
}
}

View File

@ -63,7 +63,7 @@ import org.elasticsearch.search.suggest.SuggestionSearchContext;
import java.util.List; import java.util.List;
class TestSearchContext extends SearchContext { public class TestSearchContext extends SearchContext {
final CacheRecycler cacheRecycler; final CacheRecycler cacheRecycler;
final PageCacheRecycler pageCacheRecycler; final PageCacheRecycler pageCacheRecycler;
@ -74,7 +74,7 @@ class TestSearchContext extends SearchContext {
ContextIndexSearcher searcher; ContextIndexSearcher searcher;
int size; int size;
TestSearchContext(CacheRecycler cacheRecycler, PageCacheRecycler pageCacheRecycler, IdCache idCache, IndexService indexService, FilterCache filterCache) { public TestSearchContext(CacheRecycler cacheRecycler, PageCacheRecycler pageCacheRecycler, IdCache idCache, IndexService indexService, FilterCache filterCache) {
this.cacheRecycler = cacheRecycler; this.cacheRecycler = cacheRecycler;
this.pageCacheRecycler = pageCacheRecycler; this.pageCacheRecycler = pageCacheRecycler;
this.idCache = idCache; this.idCache = idCache;
@ -82,6 +82,14 @@ class TestSearchContext extends SearchContext {
this.filterCache = filterCache; this.filterCache = filterCache;
} }
public TestSearchContext() {
this.cacheRecycler = null;
this.pageCacheRecycler = null;
this.idCache = null;
this.indexService = null;
this.filterCache = null;
}
@Override @Override
public boolean clearAndRelease() { public boolean clearAndRelease() {
return false; return false;