文档-主键查询
# GetRequest
创建Test07_Doc_Search
package com.stt.demo;
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;
public class Test07_Doc_Search {
public static void main(String[] args) throws IOException {
// 创建客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 创建请求对象
GetRequest request = new GetRequest();
// 配置主键查询
request.index("user").id("1001");
// 发送请求
GetResponse response = client.get(request, RequestOptions.DEFAULT);
// 查看结果
System.out.println("_index:" + response.getIndex());
System.out.println("_type:" + response.getType());
System.out.println("_id:" + response.getId());
System.out.println("_source:" + response.getSource());
// 关闭客户端
client.close();
}
}
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
33
34
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
33
34
返回
_index:user
_type:_doc
_id:1001
_source:{sex=女, name=zhangsan, age=30}
1
2
3
4
2
3
4
Last Updated: 2022/02/05, 15:58:51