public TermsIntFacetCollector(
      String facetName,
      String fieldName,
      int size,
      TermsFacet.ComparatorType comparatorType,
      boolean allTerms,
      SearchContext context,
      ImmutableSet<String> excluded,
      String scriptLang,
      String script,
      Map<String, Object> params) {
    super(facetName);
    this.fieldDataCache = context.fieldDataCache();
    this.size = size;
    this.comparatorType = comparatorType;
    this.numberOfShards = context.numberOfShards();

    MapperService.SmartNameFieldMappers smartMappers = context.mapperService().smartName(fieldName);
    if (smartMappers == null || !smartMappers.hasMapper()) {
      throw new ElasticSearchIllegalArgumentException(
          "Field ["
              + fieldName
              + "] doesn't have a type, can't run terms int facet collector on it");
    } else {
      // add type filter if there is exact doc mapper associated with it
      if (smartMappers.hasDocMapper()) {
        setFilter(context.filterCache().cache(smartMappers.docMapper().typeFilter()));
      }

      if (smartMappers.mapper().fieldDataType() != FieldDataType.DefaultTypes.INT) {
        throw new ElasticSearchIllegalArgumentException(
            "Field ["
                + fieldName
                + "] is not of int type, can't run terms int facet collector on it");
      }

      this.indexFieldName = smartMappers.mapper().names().indexName();
      this.fieldDataType = smartMappers.mapper().fieldDataType();
    }

    if (script != null) {
      this.script = context.scriptService().search(context.lookup(), scriptLang, script, params);
    } else {
      this.script = null;
    }

    if (this.script == null && excluded.isEmpty()) {
      aggregator = new StaticAggregatorValueProc(CacheRecycler.popIntIntMap());
    } else {
      aggregator = new AggregatorValueProc(CacheRecycler.popIntIntMap(), excluded, this.script);
    }

    if (allTerms) {
      try {
        for (IndexReader reader : context.searcher().subReaders()) {
          IntFieldData fieldData =
              (IntFieldData) fieldDataCache.cache(fieldDataType, reader, indexFieldName);
          fieldData.forEachValue(aggregator);
        }
      } catch (Exception e) {
        throw new FacetPhaseExecutionException(facetName, "failed to load all terms", e);
      }
    }
  }
 @Override
 protected void doCollect(int doc) throws IOException {
   fieldData.forEachValueInDoc(doc, aggregator);
 }