@Test
  public void testTextSingleValuedNoDV() {

    SchemaField field = new SchemaField("field", new StrField(), 0, null);

    // default is FC, UIF rewrites to FCS for mincount = 0
    // TODO should it rewrite to FC instead?

    assertEquals(SimpleFacets.FacetMethod.FC, SimpleFacets.selectFacetMethod(field, null, 0));
    assertEquals(
        SimpleFacets.FacetMethod.ENUM,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.ENUM, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FC, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FCS,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.UIF, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FCS,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FCS, 0));
    assertEquals(SimpleFacets.FacetMethod.FC, SimpleFacets.selectFacetMethod(field, null, 1));
    assertEquals(
        SimpleFacets.FacetMethod.ENUM,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.ENUM, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FC, 1));
    assertEquals(
        SimpleFacets.FacetMethod.UIF,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.UIF, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FCS,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FCS, 1));
  }
  @Test
  public void testNumericSingleValuedDV() {

    SchemaField field = new SchemaField("field", new TrieIntField(), DOC_VALUES, null);

    // default is FCS, can't use ENUM due to trie-field terms, FC rewrites to FCS for efficiency

    assertEquals(SimpleFacets.FacetMethod.FCS, SimpleFacets.selectFacetMethod(field, null, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FCS,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.ENUM, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FCS,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FC, 0));
    assertEquals(
        SimpleFacets.FacetMethod.UIF,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.UIF, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FCS,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FCS, 0));
    assertEquals(SimpleFacets.FacetMethod.FCS, SimpleFacets.selectFacetMethod(field, null, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FCS,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.ENUM, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FCS,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FC, 1));
    assertEquals(
        SimpleFacets.FacetMethod.UIF,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.UIF, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FCS,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FCS, 1));
  }
  @Test
  public void testTextMultiValuedNoDV() {

    SchemaField field = new SchemaField("field", new StrField(), MULTIVALUED, null);

    // default is FC, can't use FCS for multivalued fields, UIF rewrites to FC for mincount = 0

    assertEquals(SimpleFacets.FacetMethod.FC, SimpleFacets.selectFacetMethod(field, null, 0));
    assertEquals(
        SimpleFacets.FacetMethod.ENUM,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.ENUM, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FCS, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.UIF, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FC, 0));
    assertEquals(SimpleFacets.FacetMethod.FC, SimpleFacets.selectFacetMethod(field, null, 1));
    assertEquals(
        SimpleFacets.FacetMethod.ENUM,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.ENUM, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FCS, 1));
    assertEquals(
        SimpleFacets.FacetMethod.UIF,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.UIF, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FC, 1));
  }
  @Test
  public void testTextMultiValuedDV() {

    SchemaField field = new SchemaField("field", new StrField(), DOC_VALUES ^ MULTIVALUED, null);

    // default is FC, can't use FCS because of multivalues

    assertEquals(SimpleFacets.FacetMethod.FC, SimpleFacets.selectFacetMethod(field, null, 0));
    assertEquals(
        SimpleFacets.FacetMethod.ENUM,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.ENUM, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FCS, 0));
    assertEquals(
        SimpleFacets.FacetMethod.UIF,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.UIF, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FC, 0));
    assertEquals(SimpleFacets.FacetMethod.FC, SimpleFacets.selectFacetMethod(field, null, 1));
    assertEquals(
        SimpleFacets.FacetMethod.ENUM,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.ENUM, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FCS, 1));
    assertEquals(
        SimpleFacets.FacetMethod.UIF,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.UIF, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FC, 1));
  }
  @Test
  public void testTextSingleValuedDV() {

    SchemaField field = new SchemaField("field", new StrField(), DOC_VALUES, null);

    // default is FC, otherwise just uses the passed-in method

    assertEquals(SimpleFacets.FacetMethod.FC, SimpleFacets.selectFacetMethod(field, null, 0));
    assertEquals(
        SimpleFacets.FacetMethod.ENUM,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.ENUM, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FC, 0));
    assertEquals(
        SimpleFacets.FacetMethod.UIF,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.UIF, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FCS,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FCS, 0));
    assertEquals(SimpleFacets.FacetMethod.FC, SimpleFacets.selectFacetMethod(field, null, 1));
    assertEquals(
        SimpleFacets.FacetMethod.ENUM,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.ENUM, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FC, 1));
    assertEquals(
        SimpleFacets.FacetMethod.UIF,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.UIF, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FCS,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FCS, 1));
  }
  @Test
  public void testNumericMultiValuedNoDV() {

    SchemaField field = new SchemaField("field", new TrieIntField(), MULTIVALUED, null);

    // only works with FC for mincount = 0, UIF for count > 1 is fine

    assertEquals(SimpleFacets.FacetMethod.FC, SimpleFacets.selectFacetMethod(field, null, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.ENUM, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FCS, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.UIF, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FC, 0));
    assertEquals(SimpleFacets.FacetMethod.FC, SimpleFacets.selectFacetMethod(field, null, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.ENUM, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FCS, 1));
    assertEquals(
        SimpleFacets.FacetMethod.UIF,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.UIF, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FC, 1));
  }
  @Test
  public void testBooleanDefaults() {

    // BoolField defaults to ENUM

    SchemaField field = new SchemaField("field", new BoolField(), 0, null);
    assertEquals(SimpleFacets.FacetMethod.ENUM, SimpleFacets.selectFacetMethod(field, null, 0));
    assertEquals(SimpleFacets.FacetMethod.ENUM, SimpleFacets.selectFacetMethod(field, null, 1));
  }
  @Test
  public void testNumericMultiValuedDV() {

    SchemaField field =
        new SchemaField("field", new TrieIntField(), DOC_VALUES ^ MULTIVALUED, null);

    // default is FC, can't use ENUM due to trie-field terms, can't use FCS because of multivalues

    // default value is FC
    assertEquals(SimpleFacets.FacetMethod.FC, SimpleFacets.selectFacetMethod(field, null, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.ENUM, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FCS, 0));
    assertEquals(
        SimpleFacets.FacetMethod.UIF,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.UIF, 0));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FC, 0));
    assertEquals(SimpleFacets.FacetMethod.FC, SimpleFacets.selectFacetMethod(field, null, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.ENUM, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FCS, 1));
    assertEquals(
        SimpleFacets.FacetMethod.UIF,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.UIF, 1));
    assertEquals(
        SimpleFacets.FacetMethod.FC,
        SimpleFacets.selectFacetMethod(field, SimpleFacets.FacetMethod.FC, 1));
  }
  /** finalizes result: computes missing count if applicable */
  static NamedList<Integer> finalize(
      NamedList<Integer> res,
      SolrIndexSearcher searcher,
      SchemaField schemaField,
      DocSet docs,
      int missingCount,
      boolean missing)
      throws IOException {
    if (missing) {
      if (missingCount < 0) {
        missingCount = SimpleFacets.getFieldMissingCount(searcher, docs, schemaField.getName());
      }
      res.add(null, missingCount);
    }

    return res;
  }
  public static NamedList<Integer> getCounts(
      SolrIndexSearcher searcher,
      DocSet docs,
      String fieldName,
      int offset,
      int limit,
      int mincount,
      boolean missing,
      String sort,
      String prefix,
      String contains,
      boolean ignoreCase)
      throws IOException {
    SchemaField schemaField = searcher.getSchema().getField(fieldName);
    FieldType ft = schemaField.getType();
    NamedList<Integer> res = new NamedList<>();

    // TODO: remove multiValuedFieldCache(), check dv type / uninversion type?
    final boolean multiValued = schemaField.multiValued() || ft.multiValuedFieldCache();

    final SortedSetDocValues si; // for term lookups only
    OrdinalMap ordinalMap = null; // for mapping per-segment ords to global ones
    if (multiValued) {
      si = searcher.getLeafReader().getSortedSetDocValues(fieldName);
      if (si instanceof MultiSortedSetDocValues) {
        ordinalMap = ((MultiSortedSetDocValues) si).mapping;
      }
    } else {
      SortedDocValues single = searcher.getLeafReader().getSortedDocValues(fieldName);
      si = single == null ? null : DocValues.singleton(single);
      if (single instanceof MultiSortedDocValues) {
        ordinalMap = ((MultiSortedDocValues) single).mapping;
      }
    }
    if (si == null) {
      return finalize(res, searcher, schemaField, docs, -1, missing);
    }
    if (si.getValueCount() >= Integer.MAX_VALUE) {
      throw new UnsupportedOperationException(
          "Currently this faceting method is limited to " + Integer.MAX_VALUE + " unique terms");
    }

    final BytesRefBuilder prefixRef;
    if (prefix == null) {
      prefixRef = null;
    } else if (prefix.length() == 0) {
      prefix = null;
      prefixRef = null;
    } else {
      prefixRef = new BytesRefBuilder();
      prefixRef.copyChars(prefix);
    }

    int startTermIndex, endTermIndex;
    if (prefix != null) {
      startTermIndex = (int) si.lookupTerm(prefixRef.get());
      if (startTermIndex < 0) startTermIndex = -startTermIndex - 1;
      prefixRef.append(UnicodeUtil.BIG_TERM);
      endTermIndex = (int) si.lookupTerm(prefixRef.get());
      assert endTermIndex < 0;
      endTermIndex = -endTermIndex - 1;
    } else {
      startTermIndex = -1;
      endTermIndex = (int) si.getValueCount();
    }

    final int nTerms = endTermIndex - startTermIndex;
    int missingCount = -1;
    final CharsRefBuilder charsRef = new CharsRefBuilder();
    if (nTerms > 0 && docs.size() >= mincount) {

      // count collection array only needs to be as big as the number of terms we are
      // going to collect counts for.
      final int[] counts = new int[nTerms];

      Filter filter = docs.getTopFilter();
      List<LeafReaderContext> leaves = searcher.getTopReaderContext().leaves();
      for (int subIndex = 0; subIndex < leaves.size(); subIndex++) {
        LeafReaderContext leaf = leaves.get(subIndex);
        DocIdSet dis =
            filter.getDocIdSet(leaf, null); // solr docsets already exclude any deleted docs
        DocIdSetIterator disi = null;
        if (dis != null) {
          disi = dis.iterator();
        }
        if (disi != null) {
          if (multiValued) {
            SortedSetDocValues sub = leaf.reader().getSortedSetDocValues(fieldName);
            if (sub == null) {
              sub = DocValues.emptySortedSet();
            }
            final SortedDocValues singleton = DocValues.unwrapSingleton(sub);
            if (singleton != null) {
              // some codecs may optimize SORTED_SET storage for single-valued fields
              accumSingle(counts, startTermIndex, singleton, disi, subIndex, ordinalMap);
            } else {
              accumMulti(counts, startTermIndex, sub, disi, subIndex, ordinalMap);
            }
          } else {
            SortedDocValues sub = leaf.reader().getSortedDocValues(fieldName);
            if (sub == null) {
              sub = DocValues.emptySorted();
            }
            accumSingle(counts, startTermIndex, sub, disi, subIndex, ordinalMap);
          }
        }
      }

      if (startTermIndex == -1) {
        missingCount = counts[0];
      }

      // IDEA: we could also maintain a count of "other"... everything that fell outside
      // of the top 'N'

      int off = offset;
      int lim = limit >= 0 ? limit : Integer.MAX_VALUE;

      if (sort.equals(FacetParams.FACET_SORT_COUNT)
          || sort.equals(FacetParams.FACET_SORT_COUNT_LEGACY)) {
        int maxsize = limit > 0 ? offset + limit : Integer.MAX_VALUE - 1;
        maxsize = Math.min(maxsize, nTerms);
        LongPriorityQueue queue =
            new LongPriorityQueue(Math.min(maxsize, 1000), maxsize, Long.MIN_VALUE);

        int min = mincount - 1; // the smallest value in the top 'N' values
        for (int i = (startTermIndex == -1) ? 1 : 0; i < nTerms; i++) {
          int c = counts[i];
          if (contains != null) {
            final BytesRef term = si.lookupOrd(startTermIndex + i);
            if (!SimpleFacets.contains(term.utf8ToString(), contains, ignoreCase)) {
              continue;
            }
          }
          if (c > min) {
            // NOTE: we use c>min rather than c>=min as an optimization because we are going in
            // index order, so we already know that the keys are ordered.  This can be very
            // important if a lot of the counts are repeated (like zero counts would be).

            // smaller term numbers sort higher, so subtract the term number instead
            long pair = (((long) c) << 32) + (Integer.MAX_VALUE - i);
            boolean displaced = queue.insert(pair);
            if (displaced) min = (int) (queue.top() >>> 32);
          }
        }

        // if we are deep paging, we don't have to order the highest "offset" counts.
        int collectCount = Math.max(0, queue.size() - off);
        assert collectCount <= lim;

        // the start and end indexes of our list "sorted" (starting with the highest value)
        int sortedIdxStart = queue.size() - (collectCount - 1);
        int sortedIdxEnd = queue.size() + 1;
        final long[] sorted = queue.sort(collectCount);

        for (int i = sortedIdxStart; i < sortedIdxEnd; i++) {
          long pair = sorted[i];
          int c = (int) (pair >>> 32);
          int tnum = Integer.MAX_VALUE - (int) pair;
          final BytesRef term = si.lookupOrd(startTermIndex + tnum);
          ft.indexedToReadable(term, charsRef);
          res.add(charsRef.toString(), c);
        }

      } else {
        // add results in index order
        int i = (startTermIndex == -1) ? 1 : 0;
        if (mincount <= 0 && contains == null) {
          // if mincount<=0 and we're not examining the values for contains, then
          // we won't discard any terms and we know exactly where to start.
          i += off;
          off = 0;
        }

        for (; i < nTerms; i++) {
          int c = counts[i];
          if (c < mincount) continue;
          BytesRef term = null;
          if (contains != null) {
            term = si.lookupOrd(startTermIndex + i);
            if (!SimpleFacets.contains(term.utf8ToString(), contains, ignoreCase)) {
              continue;
            }
          }
          if (--off >= 0) continue;
          if (--lim < 0) break;
          if (term == null) {
            term = si.lookupOrd(startTermIndex + i);
          }
          ft.indexedToReadable(term, charsRef);
          res.add(charsRef.toString(), c);
        }
      }
    }

    return finalize(res, searcher, schemaField, docs, missingCount, missing);
  }