MultiGet - TuPengXiong/TuPengXiong.github.io GitHub Wiki
package tpx.elasticsearch;
import java.util.ArrayList;
import java.util.List;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.client.transport.TransportClient;
public class MultiGet {
public static void main(String[] args) {
MultiGet multiGet = new MultiGet();
Index index = new Index();
index.createTransportClient();
printResponse(multiGet.multiGet(index.client));
index.closeTransportClient();
}
public List<GetResponse> multiGet(TransportClient client) {
List<GetResponse> responses = new ArrayList<GetResponse>();
MultiGetResponse multiGetItemResponses = client.prepareMultiGet()
.add("tpx", "testBuilder", "1")
.add("twitter", "tweet", "2", "3", "4")
.add("another", "type", "foo")
.get();
for (MultiGetItemResponse itemResponse : multiGetItemResponses) {
GetResponse response = itemResponse.getResponse();
if (response!=null && response.isExists()) {
responses.add(response);
}
}
return responses;
}
public static void printResponse( List<GetResponse> responses){
for (GetResponse response : responses) {
System.out.println(response.getSourceAsString());
}
}
}