Skip to content

Commit

Permalink
Added support for the _cache and _cache_key options to the `has_c…
Browse files Browse the repository at this point in the history
…hild` and `has_parent` filters.

Closes #2900
  • Loading branch information
martijnvg committed Apr 16, 2013
1 parent ef5b741 commit 9a1c034
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 5 deletions.
Expand Up @@ -32,6 +32,8 @@ public class HasChildFilterBuilder extends BaseFilterBuilder {
private final QueryBuilder queryBuilder;
private String childType;
private String filterName;
private Boolean cache;
private String cacheKey;

public HasChildFilterBuilder(String type, QueryBuilder queryBuilder) {
this.childType = type;
Expand All @@ -53,6 +55,23 @@ public HasChildFilterBuilder filterName(String filterName) {
return this;
}

/**
* Should the filter be cached or not. Defaults to <tt>false</tt>.
*/
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.
*/
public HasChildFilterBuilder cacheKey(String cacheKey) {
this.cacheKey = cacheKey;
return this;
}

@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(HasChildFilterParser.NAME);
Expand All @@ -67,6 +86,12 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
if (filterName != null) {
builder.field("_name", filterName);
}
if (cache != null) {
builder.field("_cache", cache);
}
if (cacheKey != null) {
builder.field("_cache_key", cacheKey);
}
builder.endObject();
}
}
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.common.lucene.search.XConstantScoreQuery;
import org.elasticsearch.common.lucene.search.XFilteredQuery;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.cache.filter.support.CacheKeyFilter;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.search.child.HasChildFilter;
import org.elasticsearch.search.internal.SearchContext;
Expand Down Expand Up @@ -56,6 +57,8 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
boolean queryFound = false;
String childType = null;

boolean cache = false;
CacheKeyFilter.Key cacheKey = null;
String filterName = null;
String currentFieldName = null;
XContentParser.Token token;
Expand Down Expand Up @@ -93,6 +96,10 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
throw new QueryParsingException(parseContext.index(), "the [_scope] support in [has_child] filter has been removed, use a filter as a facet_filter in the relevant global facet");
} else if ("_name".equals(currentFieldName)) {
filterName = parser.text();
} else if ("_cache".equals(currentFieldName)) {
cache = parser.booleanValue();
} else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) {
cacheKey = new CacheKeyFilter.Key(parser.text());
} else {
throw new QueryParsingException(parseContext.index(), "[has_child] filter does not support [" + currentFieldName + "]");
}
Expand Down Expand Up @@ -124,10 +131,15 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar

HasChildFilter childFilter = HasChildFilter.create(query, parentType, childType, searchContext);
searchContext.addRewrite(childFilter);
Filter filter = childFilter;

if (cache) {
filter = parseContext.cacheFilter(filter, cacheKey);
}

if (filterName != null) {
parseContext.addNamedFilter(filterName, childFilter);
parseContext.addNamedFilter(filterName, filter);
}
return childFilter;
return filter;
}
}
Expand Up @@ -32,6 +32,8 @@ 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
Expand All @@ -53,11 +55,31 @@ public HasParentFilterBuilder(String parentType, FilterBuilder parentFilter) {
this.filterBuilder = parentFilter;
}

/**
* Sets the filter name for the filter that can be used when searching for matched_filters per hit.
*/
public HasParentFilterBuilder filterName(String filterName) {
this.filterName = filterName;
return this;
}

/**
* Should the filter be cached or not. Defaults to <tt>false</tt>.
*/
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.
*/
public HasParentFilterBuilder cacheKey(String cacheKey) {
this.cacheKey = cacheKey;
return this;
}

@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(HasParentFilterParser.NAME);
Expand All @@ -72,6 +94,12 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
if (filterName != null) {
builder.field("_name", filterName);
}
if (cache != null) {
builder.field("_cache", cache);
}
if (cacheKey != null) {
builder.field("_cache_key", cacheKey);
}
builder.endObject();
}
}
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.common.lucene.search.XConstantScoreQuery;
import org.elasticsearch.common.lucene.search.XFilteredQuery;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.cache.filter.support.CacheKeyFilter;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.search.child.HasParentFilter;
import org.elasticsearch.search.internal.SearchContext;
Expand Down Expand Up @@ -56,6 +57,8 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
boolean queryFound = false;
String parentType = null;

boolean cache = false;
CacheKeyFilter.Key cacheKey = null;
String filterName = null;
String currentFieldName = null;
XContentParser.Token token;
Expand Down Expand Up @@ -92,6 +95,10 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
throw new QueryParsingException(parseContext.index(), "the [_scope] support in [has_parent] filter has been removed, use a filter as a facet_filter in the relevant global facet");
} else if ("_name".equals(currentFieldName)) {
filterName = parser.text();
} else if ("_cache".equals(currentFieldName)) {
cache = parser.booleanValue();
} else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) {
cacheKey = new CacheKeyFilter.Key(parser.text());
} else {
throw new QueryParsingException(parseContext.index(), "[has_parent] filter does not support [" + currentFieldName + "]");
}
Expand Down Expand Up @@ -120,11 +127,16 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar

HasParentFilter parentFilter = HasParentFilter.create(query, parentType, searchContext);
searchContext.addRewrite(parentFilter);
Filter filter = parentFilter;

if (cache) {
filter = parseContext.cacheFilter(filter, cacheKey);
}

if (filterName != null) {
parseContext.addNamedFilter(filterName, parentFilter);
parseContext.addNamedFilter(filterName, filter);
}
return parentFilter;
return filter;
}

}
Expand Up @@ -31,7 +31,6 @@
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.bytes.HashedBytesArray;
import org.elasticsearch.common.lucene.docset.MatchDocIdSet;
import org.elasticsearch.common.lucene.search.NoopCollector;
import org.elasticsearch.index.cache.id.IdReaderTypeCache;
import org.elasticsearch.search.internal.SearchContext;

Expand All @@ -54,6 +53,36 @@ protected HasChildFilter(Query childQuery, String parentType, String childType,
this.childQuery = childQuery;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}

HasChildFilter that = (HasChildFilter) obj;
if (!childQuery.equals(that.childQuery)) {
return false;
}
if (!childType.equals(that.childType)) {
return false;
}
if (!parentType.equals(that.parentType)) {
return false;
}
return true;
}

@Override
public int hashCode() {
int result = childQuery.hashCode();
result = 31 * result + parentType.hashCode();
result = 31 * result + childType.hashCode();
return result;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Expand Down
Expand Up @@ -52,6 +52,32 @@ public abstract class HasParentFilter extends Filter implements SearchContext.Re
this.context = context;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}

HasParentFilter that = (HasParentFilter) obj;
if (!parentQuery.equals(that.parentQuery)) {
return false;
}
if (!parentType.equals(that.parentType)) {
return false;
}
return true;
}

@Override
public int hashCode() {
int result = parentQuery.hashCode();
result = 31 * result + parentType.hashCode();
return result;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Expand Down

0 comments on commit 9a1c034

Please sign in to comment.