Exemple #1
0
  public static void main(String[] args) {
    Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "TSB-QA").build();
    // on startup
    Client client =
        new TransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress("10.0.1.68", 9300))
            .addTransportAddress(new InetSocketTransportAddress("10.0.1.69", 9300));

    SearchRequestBuilder srb1 =
        client.prepareSearch().setQuery(QueryBuilders.queryString("TSB-QA")).setSize(1);
    SearchRequestBuilder srb2 =
        client.prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1);

    MultiSearchResponse sr = client.prepareMultiSearch().add(srb1).add(srb2).execute().actionGet();

    // You will get all individual responses from
    // MultiSearchResponse#getResponses()
    long nbHits = 0;
    for (MultiSearchResponse.Item item : sr.getResponses()) {
      SearchResponse response = item.getResponse();
      System.out.println(response.getHits().getTotalHits());
      nbHits += response.getHits().getTotalHits();
    }
    System.out.println(nbHits);
  }
 public final ImmutableList<SearchResponse> getModels(
     final Logger logger,
     final Marker logMarker,
     final ImmutableList<SearchRequestBuilder> requestBuilders)
     throws IndexNotFoundException, IoExceptionT {
   checkNotNull(requestBuilders);
   if (requestBuilders.isEmpty()) {
     return ImmutableList.of();
   } else if (requestBuilders.size() == 1) {
     return ImmutableList.of(getModels(logger, logMarker, requestBuilders.get(0)));
   }
   final MultiSearchRequestBuilder multiSearchRequestBuilder = client.prepareMultiSearch();
   for (final SearchRequestBuilder requestBuilder : requestBuilders) {
     multiSearchRequestBuilder.add(requestBuilder);
   }
   logger.debug(logMarker, "searching index {} with {}", indexName, requestBuilders);
   MultiSearchResponse response;
   try {
     response = multiSearchRequestBuilder.execute().actionGet();
   } catch (final ElasticsearchException e) {
     throw exceptionFactory.newIoException(e, "error executing bulk request");
   }
   checkState(response.getResponses().length == requestBuilders.size());
   final ImmutableList.Builder<SearchResponse> responsesBuilder = ImmutableList.builder();
   for (int i = 0; i < requestBuilders.size(); i++) {
     final SearchRequestBuilder requestBuilder = requestBuilders.get(i);
     final Item item = response.getResponses()[i];
     if (item.isFailure()) {
       logger.error(
           logMarker,
           "error searching models on index {} with {}: {}",
           indexName,
           requestBuilder,
           item.getFailureMessage());
       throw exceptionFactory.newIoException(item.getFailureMessage());
     }
     responsesBuilder.add(item.getResponse());
   }
   return responsesBuilder.build();
 }