a filter based on the type of the document is built in several places in the code, which is wasteful. Instead, create a filter based on the type on the document mapper level, and reuse it where applicable.

This commit is contained in:
kimchy 2010-08-10 18:34:49 +03:00
parent 5f986ef422
commit 7977edd5db
13 changed files with 31 additions and 45 deletions

View File

@ -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.
*

View File

@ -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<FieldMapperListener> 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;
}

View File

@ -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...

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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);
}