请帮我写一个 java 连接 ELASTICSEARCH的示例,包含增删改查
时间: 2024-02-24 17:59:21 浏览: 155
elasticsearch 增删改查实例
5星 · 资源好评率100%
好的,以下是一个简单的 Java 连接 Elasticsearch 并进行增删改查的示例代码:
```java
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
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.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class ElasticsearchDemo {
private RestHighLevelClient client;
public ElasticsearchDemo() {
// TODO 连接 Elasticsearch 的代码
}
// 新增文档
public void addDocument(String index, String type, String id, Map<String, Object> source) throws IOException {
IndexRequest request = new IndexRequest(index, type, id)
.source(source);
IndexResponse response = client.index(request);
System.out.println(response);
}
// 新增文档(使用 XContentBuilder)
public void addDocument(String index, String type, String id, XContentBuilder builder) throws IOException {
IndexRequest request = new IndexRequest(index, type, id)
.source(builder);
IndexResponse response = client.index(request);
System.out.println(response);
}
// 修改文档
public void updateDocument(String index, String type, String id, Map<String, Object> source) throws IOException {
UpdateRequest request = new UpdateRequest(index, type, id)
.doc(source);
UpdateResponse response = client.update(request);
System.out.println(response);
}
// 修改文档(使用 XContentBuilder)
public void updateDocument(String index, String type, String id, XContentBuilder builder) throws IOException {
UpdateRequest request = new UpdateRequest(index, type, id)
.doc(builder);
UpdateResponse response = client.update(request);
System.out.println(response);
}
// 删除文档
public void deleteDocument(String index, String type, String id) throws IOException {
DeleteRequest request = new DeleteRequest(index, type, id);
DeleteResponse response = client.delete(request);
System.out.println(response);
}
// 查询文档
public void getDocument(String index, String type, String id) throws IOException {
GetRequest request = new GetRequest(index, type, id);
GetResponse response = client.get(request);
System.out.println(response);
}
// 复合查询
public void searchDocuments(String index) throws IOException {
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.matchQuery("name", "john"))
.from(0)
.size(5)
.sort("age", SortOrder.DESC)
.fetchSource(new String[] {"name", "age"}, null);
SearchResponse response = client.search(new SearchRequest(index)
.source(builder));
System.out.println(response);
}
public static void main(String[] args) throws IOException {
ElasticsearchDemo demo = new ElasticsearchDemo();
Map<String, Object> source = new HashMap<>();
source.put("name", "john");
source.put("age", 20);
demo.addDocument("test", "user", "1", source);
demo.getDocument("test", "user", "1");
demo.updateDocument("test", "user", "1", XContentFactory.jsonBuilder()
.startObject()
.field("name", "tom")
.endObject());
demo.deleteDocument("test", "user", "1");
demo.searchDocuments("test");
}
}
```
请注意,这只是一个简单的示例,实际使用时需要根据具体场景进行修改和完善。
阅读全文