From 2fa017db11b2c18e33fe2770fd6621c076f619f0 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Fri, 5 Oct 2012 18:21:58 +0200 Subject: [PATCH] Terms filter: Add `or` and `or_nocache` execution modes closes #2306 --- .../index/query/TermsFilterParser.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main/java/org/elasticsearch/index/query/TermsFilterParser.java b/src/main/java/org/elasticsearch/index/query/TermsFilterParser.java index 269a795190b..bc4aad4a2bc 100644 --- a/src/main/java/org/elasticsearch/index/query/TermsFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/TermsFilterParser.java @@ -25,6 +25,7 @@ import org.apache.lucene.search.Filter; import org.apache.lucene.search.XTermsFilter; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.search.AndFilter; +import org.elasticsearch.common.lucene.search.OrFilter; import org.elasticsearch.common.lucene.search.TermFilter; import org.elasticsearch.common.lucene.search.XBooleanFilter; import org.elasticsearch.common.xcontent.XContentParser; @@ -194,6 +195,38 @@ public class TermsFilterParser implements FilterParser { if (cache == null || cache) { filter = parseContext.cacheFilter(filter, cacheKey); } + } else if ("or".equals(execution)) { + List filters = Lists.newArrayList(); + if (fieldMapper != null) { + for (String term : terms) { + filters.add(parseContext.cacheFilter(fieldMapper.fieldFilter(term, parseContext), null)); + } + } else { + for (String term : terms) { + filters.add(parseContext.cacheFilter(new TermFilter(new Term(fieldName, term)), null)); + } + } + filter = new OrFilter(filters); + // only cache if explicitly told to, since we cache inner filters + if (cache != null && cache) { + filter = parseContext.cacheFilter(filter, cacheKey); + } + } else if ("or_nocache".equals(execution)) { + List filters = Lists.newArrayList(); + if (fieldMapper != null) { + for (String term : terms) { + filters.add(fieldMapper.fieldFilter(term, parseContext)); + } + } else { + for (String term : terms) { + filters.add(new TermFilter(new Term(fieldName, term))); + } + } + filter = new OrFilter(filters); + // cache the whole filter by default, or if explicitly told to + if (cache == null || cache) { + filter = parseContext.cacheFilter(filter, cacheKey); + } } else { throw new QueryParsingException(parseContext.index(), "bool filter execution value [" + execution + "] not supported"); }