import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; public class ElasticsearchExample { public static void main(String[] args) throws Exception { RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http")); RestHighLevelClient client = new RestHighLevelClient(builder); IndexRequest request = new IndexRequest("my_index"); request.id("1"); String jsonString = "{" + "\"name\":\"John\"," + "\"age\":30" + "}"; request.source(jsonString, XContentType.JSON); IndexResponse response = client.index(request); String index = response.getIndex(); String id = response.getId(); System.out.println("Index: " + index); System.out.println("Id: " + id); client.close(); } }


上一篇:
下一篇:
切换中文