protected String findSortField(
      SearchParameters searchParameters,
      ClosingIndexSearcher searcher,
      String field,
      Locale sortLocale) {
    // find best field match

    MLAnalysisMode analysisMode = getLuceneConfig().getDefaultMLSearchAnalysisMode();
    HashSet<String> allowableLocales = new HashSet<String>();
    for (Locale l : MLAnalysisMode.getLocales(analysisMode, sortLocale, false)) {
      allowableLocales.add(l.toString());
    }

    String sortField = field;

    for (Object current : searcher.getReader().getFieldNames(FieldOption.INDEXED)) {
      String currentString = (String) current;
      if (currentString.startsWith(field) && currentString.endsWith(".sort")) {
        String fieldLocale =
            currentString.substring(field.length() + 1, currentString.length() - 5);
        if (allowableLocales.contains(fieldLocale)) {
          if (fieldLocale.equals(sortLocale.toString())) {
            sortField = currentString;
            break;
          } else if (sortLocale.toString().startsWith(fieldLocale)) {
            if (sortField.equals(field) || (currentString.length() < sortField.length())) {
              sortField = currentString;
            }
          } else if (fieldLocale.startsWith(sortLocale.toString())) {
            if (sortField.equals(field) || (currentString.length() < sortField.length())) {
              sortField = currentString;
            }
          }
        }
      }
    }
    return sortField;
  }
  public List<Pair<String, Integer>> getTopTerms(String field, int count) {
    ClosingIndexSearcher searcher = null;
    try {
      LinkedList<Pair<String, Integer>> answer = new LinkedList<Pair<String, Integer>>();
      searcher = getSearcher(indexer);
      IndexReader reader = searcher.getIndexReader();
      TermEnum terms = reader.terms(new Term(field, ""));
      do {
        Term term = terms.term();
        if (term != null) {
          if (!term.field().equals(field)) {
            break;
          }
          int freq = terms.docFreq();
          Pair<String, Integer> pair =
              new Pair<String, Integer>(term.text(), Integer.valueOf(freq));
          if (answer.size() < count) {
            if (answer.size() == 0) {
              answer.add(pair);
            } else if (answer.get(answer.size() - 1).getSecond().compareTo(pair.getSecond()) >= 0) {
              answer.add(pair);
            } else {
              for (ListIterator<Pair<String, Integer>> it = answer.listIterator();
                  it.hasNext(); /**/ ) {
                Pair<String, Integer> test = it.next();
                if (test.getSecond().compareTo(pair.getSecond()) < 0) {
                  it.previous();
                  it.add(pair);
                  break;
                }
              }
            }
          } else if (answer.get(count - 1).getSecond().compareTo(pair.getSecond()) < 0) {
            for (ListIterator<Pair<String, Integer>> it = answer.listIterator();
                it.hasNext(); /**/ ) {
              Pair<String, Integer> test = it.next();
              if (test.getSecond().compareTo(pair.getSecond()) < 0) {
                it.previous();
                it.add(pair);
                break;
              }
            }
            answer.removeLast();
          } else {
            // off the end
          }
        }
      } while (terms.next());
      terms.close();
      return answer;

    } catch (IOException e) {
      throw new SearcherException(e);
    } finally {
      if (searcher != null) {
        try {
          searcher.close();
        } catch (IOException e) {
          throw new SearcherException(e);
        }
      }
    }
  }