/**
   * Prints the results for the given search request, using the specified destination.
   *
   * @param pw PrintWriter the destination where to save the results.
   * @param q SearchRequest the object encapsulating the query and the results.
   */
  public void printResults(
      final PrintWriter pw,
      final SearchRequest q,
      String method,
      String iteration,
      int _RESULTS_LENGTH)
      throws IOException {
    final ResultSet set = q.getResultSet();
    final String metaIndexDocumentKey =
        ApplicationSetup.getProperty("trec.querying.outputformat.docno.meta.key", "docno");
    final double[] scores = set.getScores();
    if (set.getResultSize() == 0) {
      logger.warn("No results retrieved for query " + q.getQueryID());
      return;
    }
    String[] docnos = obtainDocnos(metaIndexDocumentKey, q, set);

    final int maximum =
        _RESULTS_LENGTH > set.getResultSize() || _RESULTS_LENGTH == 0
            ? set.getResultSize()
            : _RESULTS_LENGTH;
    logger.debug("Writing " + maximum + " results for query " + q.getQueryID());

    // if the minimum number of documents is more than the
    // number of documents in the results, aw.length, then
    // set minimum = aw.length

    // if (minimum > set.getResultSize())
    // minimum = set.getResultSize();
    // final String iteration = ITERATION + "0";
    final String queryIdExpanded = q.getQueryID() + " " + iteration + " ";
    final String methodExpanded = " " + method + ApplicationSetup.EOL;
    StringBuilder sbuffer = new StringBuilder();
    // the results are ordered in desceding order
    // with respect to the score.
    int limit = 10000;
    int counter = 0;
    for (int i = 0; i < maximum; i++) {
      if (scores[i] == Double.NEGATIVE_INFINITY) continue;
      sbuffer.append(queryIdExpanded);
      sbuffer.append(docnos[i]);
      sbuffer.append(" ");
      sbuffer.append(i);
      sbuffer.append(" ");
      sbuffer.append(scores[i]);
      sbuffer.append(methodExpanded);
      counter++;
      if (counter % limit == 0) {
        pw.write(sbuffer.toString());
        sbuffer = null;
        sbuffer = new StringBuilder();
        pw.flush();
      }
    }
    pw.write(sbuffer.toString());
    pw.flush();
  }
 /**
  * According to the given parameters, it sets up the correct matching class.
  *
  * @param queryId String the query identifier to use.
  * @param query String the query to process.
  * @param cParameter double the value of the parameter to use.
  */
 public void processQuery(String queryId, String query, double cParameter) {
   SearchRequest srq = queryingManager.newSearchRequest(queryId, query);
   srq.setControl("c", Double.toString(cParameter));
   srq.addMatchingModel(mModel, wModel);
   matchingCount++;
   queryingManager.runPreProcessing(srq);
   queryingManager.runMatching(srq);
   queryingManager.runPostProcessing(srq);
   queryingManager.runPostFilters(srq);
   try {
     printResults(resultFile, srq);
   } catch (IOException ioe) {
     logger.error("Problem displaying results", ioe);
   }
 }
  /**
   * Prints the results
   *
   * @param pw PrintWriter the file to write the results to.
   * @param q SearchRequest the search request to get results from.
   */
  public void printResults(PrintWriter pw, SearchRequest q) throws IOException {
    ResultSet set = q.getResultSet();
    int[] docids = set.getDocids();
    double[] scores = set.getScores();
    int minimum = RESULTS_LENGTH;
    // if the minimum number of documents is more than the
    // number of documents in the results, aw.length, then
    // set minimum = aw.length
    if (minimum > set.getResultSize()) minimum = set.getResultSize();
    if (verbose)
      if (set.getResultSize() > 0)
        pw.write("\n\tDisplaying 1-" + set.getResultSize() + " results\n");
      else pw.write("\n\tNo results\n");
    if (set.getResultSize() == 0) return;

    int metaKeyId = 0;
    final int metaKeyCount = metaKeys.length;
    String[][] docNames = new String[metaKeyCount][];
    for (String metaIndexDocumentKey : metaKeys) {
      if (set.hasMetaItems(metaIndexDocumentKey)) {
        docNames[metaKeyId] = set.getMetaItems(metaIndexDocumentKey);
      } else {
        final MetaIndex metaIndex = index.getMetaIndex();
        docNames[metaKeyId] = metaIndex.getItems(metaIndexDocumentKey, docids);
      }
      metaKeyId++;
    }

    StringBuilder sbuffer = new StringBuilder();
    // the results are ordered in asceding order
    // with respect to the score. For example, the
    // document with the highest score has score
    // score[scores.length-1] and its docid is
    // docid[docids.length-1].
    int start = 0;
    int end = minimum;
    for (int i = start; i < end; i++) {
      if (scores[i] <= 0d) continue;
      sbuffer.append(i);
      sbuffer.append(" ");
      for (metaKeyId = 0; metaKeyId < metaKeyCount; metaKeyId++) {
        sbuffer.append(docNames[metaKeyId][i]);
        sbuffer.append(" ");
      }
      sbuffer.append(docids[i]);
      sbuffer.append(" ");
      sbuffer.append(scores[i]);
      sbuffer.append('\n');
    }
    // System.out.println(sbuffer.toString());
    pw.write(sbuffer.toString());
    pw.flush();
    // pw.write("finished outputting\n");
  }