@Service
public class DocApi implements Const {
@Autowired
RestHighLevelClient client;
public void info() throws IOException {
MainResponse response = client.info(RequestOptions.DEFAULT);
ClusterName clusterName = response.getClusterName();
String clusterUuid = response.getClusterUuid();
String nodeName = response.getNodeName();
Version version = response.getVersion();
Build build = response.getBuild();
}
public IndexResponse index(int n) throws IOException {
IndexRequest request = new IndexRequest(INDEX, TYPE, "1")
.source("user", "kimchy", "postDate", new Date(), "message", "trying out Elasticsearch");
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
// client.indexAsync(request, RequestOptions.DEFAULT, new MyActionListener());
String index = indexResponse.getIndex();
String type = indexResponse.getType();
String id = indexResponse.getId();
long version = indexResponse.getVersion();
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
} else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
}
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
}
if (shardInfo.getFailed() > 0) {
for (ReplicationResponse.ShardInfo.Failure failure :
shardInfo.getFailures()) {
String reason = failure.reason();
}
}
return indexResponse;
}
public void get() throws IOException {
GetRequest request = new GetRequest(INDEX, TYPE, "1");
// request.storedFields("message");
GetResponse getResponse = client.get(request, RequestOptions.DEFAULT);
// String msg = getResponse.getField("message").getValue();
String message = getResponse.getSourceAsMap().get("message").toString();
}
public void exist() throws IOException {
GetRequest getRequest = new GetRequest(INDEX, TYPE, "1");
getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
}
public DeleteResponse delete() throws IOException {
DeleteRequest request = new DeleteRequest(INDEX, TYPE, "1");
DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
String index = deleteResponse.getIndex();
String type = deleteResponse.getType();
String id = deleteResponse.getId();
long version = deleteResponse.getVersion();
ReplicationResponse.ShardInfo shardInfo = deleteResponse.getShardInfo();
if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
}
if (shardInfo.getFailed() > 0) {
for (ReplicationResponse.ShardInfo.Failure failure :
shardInfo.getFailures()) {
String reason = failure.reason();
}
}
return deleteResponse;
}
public void bulk() throws IOException {
BulkRequest request = new BulkRequest();
request.add(new IndexRequest(INDEX, TYPE, "1").source(XContentType.JSON, "user", "foo"));
request.add(new IndexRequest(INDEX, TYPE, "2").source(XContentType.JSON, "user", "bar"));
request.add(new IndexRequest(INDEX, TYPE, "3").source(XContentType.JSON, "user", "baz"));
BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
for (BulkItemResponse bulkItemResponse : bulkResponse) {
DocWriteResponse itemResponse = bulkItemResponse.getResponse();
if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.INDEX
|| bulkItemResponse.getOpType() == DocWriteRequest.OpType.CREATE) {
IndexResponse indexResponse = (IndexResponse) itemResponse;
} else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.UPDATE) {
UpdateResponse updateResponse = (UpdateResponse) itemResponse;
} else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.DELETE) {
DeleteResponse deleteResponse = (DeleteResponse) itemResponse;
}
}
}
public MultiGetResponse multget() throws IOException {
MultiGetRequest request = new MultiGetRequest();
request.add(new MultiGetRequest.Item(INDEX, TYPE, "1"));
request.add(new MultiGetRequest.Item(INDEX, TYPE, "2"));
request.add(new MultiGetRequest.Item(INDEX, TYPE, "3"));
MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
MultiGetItemResponse firstItem = response.getResponses()[0];
Assert.isNull(firstItem.getFailure(), "should be no failure");
GetResponse firstGet = firstItem.getResponse();
String index = firstItem.getIndex();
String type = firstItem.getType();
String id = firstItem.getId();
if (firstGet.isExists()) {
long version = firstGet.getVersion();
String sourceAsString = firstGet.getSourceAsString();
Map<String, Object> sourceAsMap = firstGet.getSourceAsMap();
byte[] sourceAsBytes = firstGet.getSourceAsBytes();
} else {
}
return response;
}
public void close() throws IOException {
client.close();
}
}