diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java index cf673e9fd72..6fb0f05e385 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java @@ -21,6 +21,7 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.Fieldable; +import org.apache.lucene.search.Filter; import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.common.util.concurrent.ThreadSafe; @@ -78,6 +79,11 @@ public interface DocumentMapper { */ Analyzer searchAnalyzer(); + /** + * A filter based on the type of the field. + */ + Filter typeFilter(); + /** * Parses the source into a parsed document. * diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentDocumentMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentDocumentMapper.java index 69b3dec91bc..1d09a881354 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentDocumentMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentDocumentMapper.java @@ -21,8 +21,10 @@ package org.elasticsearch.index.mapper.xcontent; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.Document; +import org.apache.lucene.search.Filter; import org.elasticsearch.common.Preconditions; import org.elasticsearch.common.collect.ImmutableMap; +import org.elasticsearch.common.lucene.search.TermFilter; import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentFactory; @@ -187,6 +189,8 @@ public class XContentDocumentMapper implements DocumentMapper, ToXContent { private final List fieldMapperListeners = newArrayList(); + private final Filter typeFilter; + private final Object mutex = new Object(); public XContentDocumentMapper(String index, XContentObjectMapper rootObjectMapper, @@ -216,6 +220,8 @@ public class XContentDocumentMapper implements DocumentMapper, ToXContent { this.indexAnalyzer = indexAnalyzer; this.searchAnalyzer = searchAnalyzer; + this.typeFilter = new TermFilter(typeMapper().term(type)); + // if we are not enabling all, set it to false on the root object, (and on all the rest...) if (!allFieldMapper.enabled()) { this.rootObjectMapper.includeInAll(allFieldMapper.enabled()); @@ -300,6 +306,10 @@ public class XContentDocumentMapper implements DocumentMapper, ToXContent { return this.searchAnalyzer; } + @Override public Filter typeFilter() { + return this.typeFilter; + } + @Override public DocumentFieldMappers mappers() { return this.fieldMappers; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/support/QueryParsers.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/support/QueryParsers.java index 2dbde183be6..6d279a0f971 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/support/QueryParsers.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/support/QueryParsers.java @@ -20,7 +20,6 @@ package org.elasticsearch.index.query.support; import org.apache.lucene.search.*; -import org.elasticsearch.common.lucene.search.TermFilter; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.xcontent.QueryParseContext; @@ -45,11 +44,7 @@ public final class QueryParsers { return query; } DocumentMapper docMapper = smartFieldMappers.docMapper(); - - Filter typeFilter = new TermFilter(docMapper.typeMapper().term(docMapper.type())); - typeFilter = parseContext.cacheFilterIfPossible(typeFilter); - - return new FilteredQuery(query, typeFilter); + return new FilteredQuery(query, parseContext.cacheFilterIfPossible(docMapper.typeFilter())); } public static Filter wrapSmartNameFilter(Filter filter, @Nullable MapperService.SmartNameFieldMappers smartFieldMappers, @@ -63,10 +58,7 @@ public final class QueryParsers { DocumentMapper docMapper = smartFieldMappers.docMapper(); BooleanFilter booleanFilter = new BooleanFilter(); - Filter typeFilter = new TermFilter(docMapper.typeMapper().term(docMapper.type())); - typeFilter = parseContext.cacheFilterIfPossible(typeFilter); - - booleanFilter.add(new FilterClause(typeFilter, BooleanClause.Occur.MUST)); + booleanFilter.add(new FilterClause(parseContext.cacheFilterIfPossible(docMapper.typeFilter()), BooleanClause.Occur.MUST)); booleanFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST)); // don't cache the boolean filter... diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/shard/service/InternalIndexShard.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/shard/service/InternalIndexShard.java index 54f136a12e6..44419fda5b7 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/shard/service/InternalIndexShard.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/shard/service/InternalIndexShard.java @@ -30,7 +30,6 @@ import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.Lucene; -import org.elasticsearch.common.lucene.search.TermFilter; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; @@ -490,9 +489,7 @@ public class InternalIndexShard extends AbstractIndexShardComponent implements I if (docMapper == null) { throw new TypeMissingException(shardId.index(), type); } - Filter typeFilter = new TermFilter(docMapper.typeMapper().term(docMapper.type())); - typeFilter = indexCache.filter().cache(typeFilter); - query = new FilteredQuery(query, typeFilter); + query = new FilteredQuery(query, indexCache.filter().cache(docMapper.typeFilter())); } else { BooleanFilter booleanFilter = new BooleanFilter(); for (String type : types) { @@ -500,9 +497,7 @@ public class InternalIndexShard extends AbstractIndexShardComponent implements I if (docMapper == null) { throw new TypeMissingException(shardId.index(), type); } - Filter typeFilter = new TermFilter(docMapper.typeMapper().term(docMapper.type())); - typeFilter = indexCache.filter().cache(typeFilter); - booleanFilter.add(new FilterClause(typeFilter, BooleanClause.Occur.SHOULD)); + booleanFilter.add(new FilterClause(indexCache.filter().cache(docMapper.typeFilter()), BooleanClause.Occur.SHOULD)); } query = new FilteredQuery(query, booleanFilter); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/FacetsPhase.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/FacetsPhase.java index fadc96c1c1a..689527c412d 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/FacetsPhase.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/FacetsPhase.java @@ -25,7 +25,6 @@ import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.common.collect.Lists; import org.elasticsearch.common.lucene.search.NoopCollector; import org.elasticsearch.common.lucene.search.Queries; -import org.elasticsearch.common.lucene.search.TermFilter; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.search.SearchParseElement; import org.elasticsearch.search.SearchPhase; @@ -66,16 +65,12 @@ public class FacetsPhase implements SearchPhase { if (context.types().length == 1) { String type = context.types()[0]; DocumentMapper docMapper = context.mapperService().documentMapper(type); - Filter typeFilter = new TermFilter(docMapper.typeMapper().term(docMapper.type())); - typeFilter = context.filterCache().cache(typeFilter); - query = new FilteredQuery(query, typeFilter); + query = new FilteredQuery(query, context.filterCache().cache(docMapper.typeFilter())); } else { BooleanFilter booleanFilter = new BooleanFilter(); for (String type : context.types()) { DocumentMapper docMapper = context.mapperService().documentMapper(type); - Filter typeFilter = new TermFilter(docMapper.typeMapper().term(docMapper.type())); - typeFilter = context.filterCache().cache(typeFilter); - booleanFilter.add(new FilterClause(typeFilter, BooleanClause.Occur.SHOULD)); + booleanFilter.add(new FilterClause(context.filterCache().cache(docMapper.typeFilter()), BooleanClause.Occur.SHOULD)); } query = new FilteredQuery(query, booleanFilter); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetCollector.java index d0943ca7a56..8c33871a904 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetCollector.java @@ -21,7 +21,6 @@ package org.elasticsearch.search.facets.geodistance; import org.apache.lucene.index.IndexReader; import org.elasticsearch.common.lucene.geo.GeoDistance; -import org.elasticsearch.common.lucene.search.TermFilter; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldData; @@ -83,7 +82,7 @@ public class GeoDistanceFacetCollector extends AbstractFacetCollector { // add type filter if there is exact doc mapper associated with it if (smartMappers.hasDocMapper()) { - setFilter(context.filterCache().cache(new TermFilter(smartMappers.docMapper().typeMapper().term(smartMappers.docMapper().type())))); + setFilter(context.filterCache().cache(smartMappers.docMapper().typeFilter())); } this.indexLatFieldName = smartMappers.mapper().names().indexName(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/HistogramFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/HistogramFacetCollector.java index 376368fb937..2414d4d6301 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/HistogramFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/HistogramFacetCollector.java @@ -20,7 +20,6 @@ package org.elasticsearch.search.facets.histogram; import org.apache.lucene.index.IndexReader; -import org.elasticsearch.common.lucene.search.TermFilter; import org.elasticsearch.common.trove.TLongDoubleHashMap; import org.elasticsearch.common.trove.TLongLongHashMap; import org.elasticsearch.index.cache.field.data.FieldDataCache; @@ -73,7 +72,7 @@ public class HistogramFacetCollector extends AbstractFacetCollector { // add type filter if there is exact doc mapper associated with it if (smartMappers.hasDocMapper()) { - setFilter(context.filterCache().cache(new TermFilter(smartMappers.docMapper().typeMapper().term(smartMappers.docMapper().type())))); + setFilter(context.filterCache().cache(smartMappers.docMapper().typeFilter())); } FieldMapper mapper = smartMappers.mapper(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/KeyValueHistogramFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/KeyValueHistogramFacetCollector.java index 5ee2acba00c..40490fa84a3 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/KeyValueHistogramFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/histogram/KeyValueHistogramFacetCollector.java @@ -20,7 +20,6 @@ package org.elasticsearch.search.facets.histogram; import org.apache.lucene.index.IndexReader; -import org.elasticsearch.common.lucene.search.TermFilter; import org.elasticsearch.common.trove.TLongDoubleHashMap; import org.elasticsearch.common.trove.TLongLongHashMap; import org.elasticsearch.index.cache.field.data.FieldDataCache; @@ -78,7 +77,7 @@ public class KeyValueHistogramFacetCollector extends AbstractFacetCollector { // add type filter if there is exact doc mapper associated with it if (smartMappers.hasDocMapper()) { - setFilter(context.filterCache().cache(new TermFilter(smartMappers.docMapper().typeMapper().term(smartMappers.docMapper().type())))); + setFilter(context.filterCache().cache(smartMappers.docMapper().typeFilter())); } keyIndexFieldName = smartMappers.mapper().names().indexName(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/KeyValueRangeFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/KeyValueRangeFacetCollector.java index 1224a3d42f3..ad609ad4394 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/KeyValueRangeFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/KeyValueRangeFacetCollector.java @@ -20,7 +20,6 @@ package org.elasticsearch.search.facets.range; import org.apache.lucene.index.IndexReader; -import org.elasticsearch.common.lucene.search.TermFilter; import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldData; import org.elasticsearch.index.field.data.NumericFieldData; @@ -68,7 +67,7 @@ public class KeyValueRangeFacetCollector extends AbstractFacetCollector { // add type filter if there is exact doc mapper associated with it if (smartMappers.hasDocMapper()) { - setFilter(context.filterCache().cache(new TermFilter(smartMappers.docMapper().typeMapper().term(smartMappers.docMapper().type())))); + setFilter(context.filterCache().cache(smartMappers.docMapper().typeFilter())); } keyIndexFieldName = smartMappers.mapper().names().indexName(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/RangeFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/RangeFacetCollector.java index e1df9055db9..104acf83b6b 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/RangeFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/range/RangeFacetCollector.java @@ -20,7 +20,6 @@ package org.elasticsearch.search.facets.range; import org.apache.lucene.index.IndexReader; -import org.elasticsearch.common.lucene.search.TermFilter; import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldData; import org.elasticsearch.index.field.data.NumericFieldData; @@ -64,7 +63,7 @@ public class RangeFacetCollector extends AbstractFacetCollector { // add type filter if there is exact doc mapper associated with it if (smartMappers.hasDocMapper()) { - setFilter(context.filterCache().cache(new TermFilter(smartMappers.docMapper().typeMapper().term(smartMappers.docMapper().type())))); + setFilter(context.filterCache().cache(smartMappers.docMapper().typeFilter())); } indexFieldName = smartMappers.mapper().names().indexName(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/statistical/StatisticalFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/statistical/StatisticalFacetCollector.java index 7806ec6e999..9e579f97714 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/statistical/StatisticalFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/statistical/StatisticalFacetCollector.java @@ -20,7 +20,6 @@ package org.elasticsearch.search.facets.statistical; import org.apache.lucene.index.IndexReader; -import org.elasticsearch.common.lucene.search.TermFilter; import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldData; import org.elasticsearch.index.field.data.NumericFieldData; @@ -61,7 +60,7 @@ public class StatisticalFacetCollector extends AbstractFacetCollector { // add type filter if there is exact doc mapper associated with it if (smartMappers.hasDocMapper()) { - setFilter(context.filterCache().cache(new TermFilter(smartMappers.docMapper().typeMapper().term(smartMappers.docMapper().type())))); + setFilter(context.filterCache().cache(smartMappers.docMapper().typeFilter())); } indexFieldName = smartMappers.mapper().names().indexName(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/TermsFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/TermsFacetCollector.java index 102491545ec..131ed31390f 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/TermsFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/TermsFacetCollector.java @@ -24,7 +24,6 @@ import org.elasticsearch.common.collect.BoundedTreeSet; import org.elasticsearch.common.collect.ImmutableList; import org.elasticsearch.common.collect.ImmutableSet; import org.elasticsearch.common.collect.Maps; -import org.elasticsearch.common.lucene.search.TermFilter; import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.common.trove.TObjectIntHashMap; import org.elasticsearch.common.trove.TObjectIntIterator; @@ -93,7 +92,7 @@ public class TermsFacetCollector extends AbstractFacetCollector { } else { // add type filter if there is exact doc mapper associated with it if (smartMappers.hasDocMapper()) { - setFilter(context.filterCache().cache(new TermFilter(smartMappers.docMapper().typeMapper().term(smartMappers.docMapper().type())))); + setFilter(context.filterCache().cache(smartMappers.docMapper().typeFilter())); } this.indexFieldName = smartMappers.mapper().names().indexName(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/query/QueryPhase.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/query/QueryPhase.java index 04cf9d9ea1c..488ff8b3d97 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/query/QueryPhase.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/query/QueryPhase.java @@ -22,7 +22,6 @@ package org.elasticsearch.search.query; import org.apache.lucene.search.*; import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.lucene.search.TermFilter; import org.elasticsearch.common.lucene.search.function.BoostScoreFunction; import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery; import org.elasticsearch.index.Index; @@ -86,9 +85,7 @@ public class QueryPhase implements SearchPhase { if (docMapper == null) { throw new TypeMissingException(new Index(searchContext.shardTarget().index()), type); } - Filter typeFilter = new TermFilter(docMapper.typeMapper().term(docMapper.type())); - typeFilter = searchContext.filterCache().cache(typeFilter); - query = new FilteredQuery(query, typeFilter); + query = new FilteredQuery(query, searchContext.filterCache().cache(docMapper.typeFilter())); } else { BooleanFilter booleanFilter = new BooleanFilter(); for (String type : searchContext.types()) { @@ -96,9 +93,7 @@ public class QueryPhase implements SearchPhase { if (docMapper == null) { throw new TypeMissingException(new Index(searchContext.shardTarget().index()), type); } - Filter typeFilter = new TermFilter(docMapper.typeMapper().term(docMapper.type())); - typeFilter = searchContext.filterCache().cache(typeFilter); - booleanFilter.add(new FilterClause(typeFilter, BooleanClause.Occur.SHOULD)); + booleanFilter.add(new FilterClause(searchContext.filterCache().cache(docMapper.typeFilter()), BooleanClause.Occur.SHOULD)); } query = new FilteredQuery(query, booleanFilter); }