elasticsearch java类document
时间: 2023-08-22 17:10:28 浏览: 49
在 Elasticsearch 的 Java 客户端中,有一个名为 `org.elasticsearch.client.document.DocumentClient` 的类,用于执行与文档相关的操作。这个类提供了一系列方法,可以用来索引、获取、更新和删除文档。下面是一个简单的示例:
```java
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
public class DocumentExample {
private RestHighLevelClient client;
public DocumentExample(RestHighLevelClient client) {
this.client = client;
}
public void indexDocument(String index, String id, String json) throws IOException {
IndexRequest request = new IndexRequest(index)
.id(id)
.source(json, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response);
}
public void getDocument(String index, String id) throws IOException {
GetRequest request = new GetRequest(index, id);
GetResponse response = client.get(request, RequestOptions.DEFAULT);
System.out.println(response);
}
public void updateDocument(String index, String id, String json) throws IOException {
UpdateRequest request = new UpdateRequest(index, id)
.doc(json, XContentType.JSON);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println(response);
}
public void deleteDocument(String index, String id) throws IOException {
DeleteRequest request = new DeleteRequest(index, id);
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println(response);
}
}
```
以上示例展示了如何使用 `DocumentClient` 类来执行索引、获取、更新和删除文档的操作。你可以根据自己的需求调用这些方法,并根据需要处理相应的响应。请注意,需要先创建一个 `RestHighLevelClient` 实例,并将其传递给 `DocumentExample` 的构造函数中,以便与 Elasticsearch 集群进行交互。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)