TestDemo.java 1.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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;

import java.io.IOException;

/**
 * 普通项目查询es,获取数据
 *
 * @author : qinyingjie
 * @version : 2.2.0
 * @date : 2023/5/10 11:50
 */
public class TestDemo {
    public static void main(String[] args) throws IOException {
        //1获取连接客户端
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("47.119.160.231", 9200, "http")
        ));
        //2构建请求
        GetRequest getRequest = new GetRequest("book", "1");
        //3执行
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        //4获取结果
        System.out.println(getResponse.getId());
        System.out.println(getResponse.getVersion());
        System.out.println(getResponse.getSourceAsString());
    }
}