文章目录
根据id 查询文档
- 根据id 查询文档
- 根据id文档删除
查询user索引下id为1002文档 . 使用GetRequest
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
/**
* @author:
* 创建时间:2022/1/11 21:05
*/
public class ESTest_Doc_Get {
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = null;
// 创建ES客户端
try {
esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
);
GetRequest request = new GetRequest();
request.index("user").id("1002");
GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
System.out.println("查询user索引下id为1002文档的结果: " + response.getSourceAsString());
} finally {
if (esClient != null) {
// 关闭ES客户端
esClient.close();
}
}
}
}
查询结果如下: 与用http请求直接查询相同.
传入DeleteRequest
请求, 获取DeleteResponse
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
/**
* @author:
* 创建时间:2022/1/11 21:05
*/
public class ESTest_Doc_Delete {
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = null;
// 创建ES客户端
try {
esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1", 9200, HttpHost.DEFAULT_SCHEME_NAME))
);
DeleteRequest request = new DeleteRequest();
request.index("user").id("1002");
DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);
System.out.println("删除user索引下id为1002文档的结果: " + response.toString());
} finally {
if (esClient != null) {
// 关闭ES客户端
esClient.close();
}
}
}
}
结果打印如下 , 成功进行删除
删除user索引下id为1002文档的结果: DeleteResponse[index=user,type=_doc,id=1002,version=10,result=deleted,shards=ShardInfo{total=2, successful=1, failures=[]}]
发送请求也查询不到数据.