@Override
  public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
    long totalHits = response.getHits().totalHits();
    List<T> results = new ArrayList<T>();
    for (SearchHit hit : response.getHits()) {
      if (hit != null) {
        T result = null;
        if (!Strings.isNullOrEmpty(hit.sourceAsString())) {
          result = mapEntity(hit.sourceAsString(), clazz);
        } else {
          result = mapEntity(hit.getFields().values(), clazz);
        }
        setPersistentEntityId(result, hit.getId(), clazz);
        results.add(result);
      }
    }
    List<FacetResult> facets = new ArrayList<FacetResult>();
    if (response.getFacets() != null) {
      for (Facet facet : response.getFacets()) {
        FacetResult facetResult = DefaultFacetMapper.parse(facet);
        if (facetResult != null) {
          facets.add(facetResult);
        }
      }
    }

    return new FacetedPageImpl<T>(results, pageable, totalHits, facets);
  }
Example #2
0
  private void InitTasks() throws Exception {
    QueryBuilder builder = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("_id", "_meta"));

    SearchRequestBuilder search = client.prepareSearch("_river");
    search.setTypes(river.riverName().name());
    search.setQuery(builder);
    SearchResponse resp = search.execute().actionGet();

    int hitCount = 0;
    for (SearchHit hit : resp.hits().getHits()) {
      logger.info(
          "Task Manager: Query response hits[ "
              + Integer.toString(hitCount)
              + "]: "
              + hit.sourceAsString());
      hitCount++;

      Map<String, Object> sourceMap = hit.sourceAsMap();
      Map<String, Object> my = (Map<String, Object>) sourceMap.get("my");
      ArrayList arr = (ArrayList) my.get("tasks");
      for (Object taskObj : arr) {
        Task newTask = new Task((Map<String, String>) taskObj, client, river.riverName().name());
        taskArr.add(newTask);
        taskMap.put(newTask.id(), newTask);
      }
    }

    currentTaskIndex = 0;
    currentTask = (Task) taskArr.get(currentTaskIndex);
  }
    @Override
    public void onResponse(final SearchResponse response) {
      if (!isConnected()) {
        onFailure(new DfContentException("Disconnected."));
        return;
      }

      final String scrollId = response.getScrollId();
      if (isFirstScan()) {
        client
            .prepareSearchScroll(scrollId)
            .setScroll(RequestUtil.getScroll(request))
            .execute(this);
        return;
      }

      final SearchHits hits = response.getHits();

      final int size = hits.getHits().length;
      currentCount += size;
      logger.info(
          "scrollId: "
              + scrollId
              + ", totalHits: "
              + hits.totalHits()
              + ", hits: "
              + size
              + ", current: "
              + currentCount);

      try {
        for (final SearchHit hit : hits) {
          final String index = bulkIndex == null ? hit.index() : bulkIndex;
          final String type = bulkType == null ? hit.type() : bulkType;
          final String operation =
              "{\"index\":{\"_index\":\""
                  + index
                  + "\",\"_type\":\""
                  + type
                  + "\",\"_id\":\""
                  + hit.id()
                  + "\"}}";
          final String source = hit.sourceAsString();
          writer.append(operation).append('\n');
          writer.append(source).append('\n');
        }

        if (size == 0 || scrollId == null) {
          // end
          writer.flush();
          close();
          listener.onResponse(null);
        } else {
          client
              .prepareSearchScroll(scrollId)
              .setScroll(RequestUtil.getScroll(request))
              .execute(this);
        }
      } catch (final Exception e) {
        onFailure(e);
      }
    }