@Override
 public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
   DocSet boundingBoxDocSet = null;
   if (boundingBoxFilter != null) {
     DocIdSet docIdSet = boundingBoxFilter.getDocIdSet(reader);
     if (docIdSet == null) {
       return null;
     }
     boundingBoxDocSet = DocSets.convert(reader, docIdSet);
   }
   final GeoPointFieldData fieldData =
       (GeoPointFieldData) fieldDataCache.cache(GeoPointFieldDataType.TYPE, reader, fieldName);
   GeoDistanceRangeDocSet distDocSet =
       new GeoDistanceRangeDocSet(
           reader.maxDoc(),
           fieldData,
           fixedSourceDistance,
           distanceBoundingCheck,
           inclusiveLowerPoint,
           inclusiveUpperPoint);
   if (boundingBoxDocSet == null) {
     return distDocSet;
   } else {
     return new AndDocSet(ImmutableList.of(boundingBoxDocSet, distDocSet));
   }
 }
 @Override
 protected void doSetNextReader(IndexReader reader, int docBase) throws IOException {
   fieldData = (ShortFieldData) fieldDataCache.cache(fieldDataType, reader, indexFieldName);
   if (script != null) {
     script.setNextReader(reader);
   }
 }
 @Override
 protected void doSetNextReader(IndexReader reader, int docBase) throws IOException {
   if (current != null) {
     missing += current.counts[0];
     if (current.values.length > 1) {
       aggregators.add(current);
     }
   }
   fieldData = (LongFieldData) fieldDataCache.cache(fieldDataType, reader, indexFieldName);
   current = new ReaderAggregator(fieldData);
 }
  public TermsShortFacetCollector(
      String facetName,
      String fieldName,
      int size,
      TermsFacet.ComparatorType comparatorType,
      boolean allTerms,
      SearchContext context,
      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 short 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.SHORT) {
        throw new ElasticSearchIllegalArgumentException(
            "Field ["
                + fieldName
                + "] is not of short type, can't run terms short facet collector on it");
      }

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

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

    if (this.script == null) {
      aggregator = new StaticAggregatorValueProc(popFacets());
    } else {
      aggregator = new AggregatorValueProc(popFacets(), this.script);
    }

    if (allTerms) {
      try {
        for (IndexReader reader : context.searcher().subReaders()) {
          ShortFieldData fieldData =
              (ShortFieldData) fieldDataCache.cache(fieldDataType, reader, indexFieldName);
          fieldData.forEachValue(aggregator);
        }
      } catch (Exception e) {
        throw new FacetPhaseExecutionException(facetName, "failed to load all terms", e);
      }
    }
  }