diff --git a/src/main/java/org/elasticsearch/search/facet/terms/TermsFacetParser.java b/src/main/java/org/elasticsearch/search/facet/terms/TermsFacetParser.java index 25cac946126..b6be22f425d 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/TermsFacetParser.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/TermsFacetParser.java @@ -53,10 +53,13 @@ import java.util.regex.Pattern; */ public class TermsFacetParser extends AbstractComponent implements FacetParser { + private final int ordinalsCacheAbove; + @Inject public TermsFacetParser(Settings settings) { super(settings); InternalTermsFacet.registerStreams(); + this.ordinalsCacheAbove = componentSettings.getAsInt("ordinals_cache_above", 10000); // above 40k we want to cache } @Override @@ -176,7 +179,7 @@ public class TermsFacetParser extends AbstractComponent implements FacetParser { if (script != null || "map".equals(executionHint)) { return new TermsStringFacetExecutor(indexFieldData, size, comparatorType, allTerms, context, excluded, pattern, searchScript); } else if (indexFieldData instanceof IndexFieldData.WithOrdinals) { - return new TermsStringOrdinalsFacetExecutor((IndexFieldData.WithOrdinals) indexFieldData, size, comparatorType, allTerms, context, excluded, pattern); + return new TermsStringOrdinalsFacetExecutor((IndexFieldData.WithOrdinals) indexFieldData, size, comparatorType, allTerms, context, excluded, pattern, ordinalsCacheAbove); } else { return new TermsStringFacetExecutor(indexFieldData, size, comparatorType, allTerms, context, excluded, pattern, searchScript); } diff --git a/src/main/java/org/elasticsearch/search/facet/terms/strings/TermsStringOrdinalsFacetExecutor.java b/src/main/java/org/elasticsearch/search/facet/terms/strings/TermsStringOrdinalsFacetExecutor.java index 94c2aca318e..54882e8962f 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/strings/TermsStringOrdinalsFacetExecutor.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/strings/TermsStringOrdinalsFacetExecutor.java @@ -54,17 +54,19 @@ public class TermsStringOrdinalsFacetExecutor extends FacetExecutor { private final int minCount; private final ImmutableSet excluded; private final Matcher matcher; + final int ordinalsCacheAbove; final List aggregators; long missing; long total; public TermsStringOrdinalsFacetExecutor(IndexFieldData.WithOrdinals indexFieldData, int size, TermsFacet.ComparatorType comparatorType, boolean allTerms, SearchContext context, - ImmutableSet excluded, Pattern pattern) { + ImmutableSet excluded, Pattern pattern, int ordinalsCacheAbove) { this.indexFieldData = indexFieldData; this.size = size; this.comparatorType = comparatorType; this.numberOfShards = context.numberOfShards(); + this.ordinalsCacheAbove = ordinalsCacheAbove; if (excluded == null || excluded.isEmpty()) { this.excluded = null; @@ -136,7 +138,9 @@ public class TermsStringOrdinalsFacetExecutor extends FacetExecutor { } for (ReaderAggregator aggregator : aggregators) { - CacheRecycler.pushIntArray(aggregator.counts); + if (aggregator.counts.length > ordinalsCacheAbove) { + CacheRecycler.pushIntArray(aggregator.counts); + } } return new InternalStringTermsFacet(facetName, comparatorType, size, Arrays.asList(list), missing, total); @@ -174,7 +178,9 @@ public class TermsStringOrdinalsFacetExecutor extends FacetExecutor { for (ReaderAggregator aggregator : aggregators) { - CacheRecycler.pushIntArray(aggregator.counts); + if (aggregator.counts.length > ordinalsCacheAbove) { + CacheRecycler.pushIntArray(aggregator.counts); + } } return new InternalStringTermsFacet(facetName, comparatorType, size, ordered, missing, total); @@ -197,7 +203,7 @@ public class TermsStringOrdinalsFacetExecutor extends FacetExecutor { } } values = indexFieldData.load(context).getBytesValues(); - current = new ReaderAggregator(values); + current = new ReaderAggregator(values, ordinalsCacheAbove); } @Override @@ -231,11 +237,15 @@ public class TermsStringOrdinalsFacetExecutor extends FacetExecutor { int total; private final int maxOrd; - public ReaderAggregator(BytesValues.WithOrdinals values) { + public ReaderAggregator(BytesValues.WithOrdinals values, int ordinalsCacheLimit) { this.values = values; this.maxOrd = values.ordinals().getMaxOrd(); - this.counts = CacheRecycler.popIntArray(maxOrd); + if (maxOrd > ordinalsCacheLimit) { + this.counts = CacheRecycler.popIntArray(maxOrd); + } else { + this.counts = new int[maxOrd]; + } } @Override