public void updateIndex(
      int id, MavenServerSettings settings, MavenServerProgressIndicator indicator)
      throws MavenServerIndexerException, MavenServerProcessCanceledException, RemoteException {
    IndexingContext index = getIndex(id);

    try {
      if (isLocal(index)) {
        File repository = index.getRepository();

        if (repository != null && repository.exists()) {
          indicator.setIndeterminate(true);
          try {
            myIndexer.scan(index, new MyScanningListener(indicator), false);
          } finally {
            indicator.setIndeterminate(false);
          }
        }
      } else {
        IndexUpdateRequest request = new IndexUpdateRequest(index);
        Maven2ServerEmbedderImpl embedder = Maven2ServerEmbedderImpl.create(settings);
        try {
          request.setResourceFetcher(
              new Maven2ServerIndexFetcher(
                  index.getRepositoryId(),
                  index.getRepositoryUrl(),
                  embedder.getComponent(WagonManager.class),
                  new TransferListenerAdapter(indicator) {
                    @Override
                    protected void downloadProgress(long downloaded, long total) {
                      super.downloadProgress(downloaded, total);
                      try {
                        myIndicator.setFraction(((double) downloaded) / total);
                      } catch (RemoteException e) {
                        throw new RuntimeRemoteException(e);
                      }
                    }

                    @Override
                    public void transferCompleted(TransferEvent event) {
                      super.transferCompleted(event);
                      try {
                        myIndicator.setText2("Processing indices...");
                      } catch (RemoteException e) {
                        throw new RuntimeRemoteException(e);
                      }
                    }
                  }));
          myUpdater.fetchAndUpdateIndex(request);
        } finally {
          embedder.release();
        }
      }
    } catch (RuntimeRemoteException e) {
      throw e.getCause();
    } catch (ProcessCanceledException e) {
      throw new MavenServerProcessCanceledException();
    } catch (Exception e) {
      throw new MavenServerIndexerException(wrapException(e));
    }
  }
  protected int searchFlat(
      AbstractSearchRequest req,
      Collection<ArtifactInfo> result,
      IndexingContext context,
      Query query,
      int from,
      int aiCount)
      throws IOException {
    Hits hits =
        context
            .getIndexSearcher()
            .search(query, new Sort(new SortField(ArtifactInfo.UINFO, SortField.STRING)));

    if (hits == null || hits.length() == 0) {
      return 0;
    }

    if (req.isHitLimited() && hits.length() > req.getResultHitLimit()) {
      return AbstractSearchResponse.LIMIT_EXCEEDED;
    }

    int hitCount = hits.length();

    int start = 0; // from == FlatSearchRequest.UNDEFINED ? 0 : from;

    // we have to pack the results as long: a) we have found aiCount ones b) we depleted hits
    for (int i = start; i < hits.length(); i++) {
      Document doc = hits.doc(i);

      ArtifactInfo artifactInfo = IndexUtils.constructArtifactInfo(doc, context);

      if (artifactInfo != null) {
        artifactInfo.repository = context.getRepositoryId();

        artifactInfo.context = context.getId();

        result.add(artifactInfo);

        if (req.isHitLimited() && result.size() > req.getResultHitLimit()) {
          // we hit limit, back out now !!
          return AbstractSearchResponse.LIMIT_EXCEEDED;
        }
      }
    }

    return hitCount;
  }
  protected int searchGrouped(
      AbstractSearchRequest req,
      Map<String, ArtifactInfoGroup> result,
      Grouping grouping,
      IndexingContext context,
      Query query)
      throws IOException {
    Hits hits =
        context
            .getIndexSearcher()
            .search(query, new Sort(new SortField(ArtifactInfo.UINFO, SortField.STRING)));

    if (hits != null && hits.length() != 0) {
      int hitCount = hits.length();

      for (int i = 0; i < hits.length(); i++) {
        ArtifactInfo artifactInfo = IndexUtils.constructArtifactInfo(hits.doc(i), context);

        if (artifactInfo != null) {
          artifactInfo.repository = context.getRepositoryId();

          artifactInfo.context = context.getId();

          if (!grouping.addArtifactInfo(result, artifactInfo)) {
            // fix the hitCount accordingly
            hitCount--;
          }
        }
      }

      if (req.isHitLimited() && hits.length() > req.getResultHitLimit()) {
        return AbstractSearchResponse.LIMIT_EXCEEDED;
      }

      return hitCount;
    } else {
      return 0;
    }
  }