使用 java 高级休息客户端将 Id 列表作为 Multi-Get 请求的参数传递
Pass list of Id as a parameter of Multi-Get request using java high level rest client
我正在尝试使用 java 高级 rest-client 获取索引中存在的所有文档的列表。
样本索引数据为-
PUT /my-index/_doc/1
{
"account_number": 1,
"balance": 28838
}
PUT /my-index/_doc/2
{
"account_number": 1,
"balance": 28838
}
PUT /my-index/_doc/3
{
"account_number": 1,
"balance": 28838
}
要通过 ID 检索多个 JSON 文档,我使用 multi-get API,如下所示
GET /_mget
{
"ids": [
"2",
"3",
"4"
]
}
搜索结果是
{
"docs": [
{
"_index": "my-index",
"_type": "_doc",
"_id": "2",
"_version": 1,
"_seq_no": 4,
"_primary_term": 4,
"found": true,
"_source": {
"account_number": 2,
"balance": 28838
}
},
{
"_index": "my-index",
"_type": "_doc",
"_id": "3",
"_version": 2,
"_seq_no": 5,
"_primary_term": 4,
"found": true,
"_source": {
"account_number": 3,
"balance": 28838
}
},
{
"_index": "my-index",
"_type": "_doc",
"_id": "4",
"found": false
}
]
}
现在,我需要解析 multi-get 请求生成的响应并获取在索引中找到的所有 ID 的列表。
Java代码
我能够获得 ID 列表,即 [2,3]
(这是预期的结果)。但是如下面的 java 代码所示,我每次都使用 for
循环从 ids
列表中添加一个元素(文档 ID)。因此每次都会创建一个新的 multi-get 请求。
public List<Integer> verifyDocuments(final VerifyScrollRequest request) throws IOException {
RestHighLevelClient es7Client = buildES7Client(request.getEs7Node(), request.getEs7Port());
List<String> ids = new ArrayList<>();
ids.add("2");
ids.add("3");
ids.add("4");
List<Integer> documents = new ArrayList<>();
MultiGetRequest getRequest = new MultiGetRequest();
for (int i = 0; i < ids.size(); i++) {
String element = ids.get(i);
getRequest.add(new MultiGetRequest.Item(request.getEs7IndexName(), element));
MultiGetResponse response = es7Client.mget(getRequest, RequestOptions.DEFAULT);
if (response.getResponses()[i].getResponse().isExists()) {
documents.add(Integer.parseInt(element));
}
}
return documents;
}
有没有办法将完整的Id列表作为参数传递给multi-get请求,让multi-get请求只创建一次?
您不需要在每次迭代时都发送调用 es7Client.mget()
。我会这样做:
public List<Integer> verifyDocuments(final VerifyScrollRequest request) throws IOException {
RestHighLevelClient es7Client = buildES7Client(request.getEs7Node(), request.getEs7Port());
// build the list of IDs
List<String> ids = new ArrayList<>();
ids.add("2");
ids.add("3");
ids.add("4");
List<Integer> documents = new ArrayList<>();
// build the mget request with all IDs
MultiGetRequest getRequest = new MultiGetRequest();
for (int i = 0; i < ids.size(); i++) {
String element = ids.get(i);
getRequest.add(new MultiGetRequest.Item(request.getEs7IndexName(), element));
}
// call mget
MultiGetResponse response = es7Client.mget(getRequest, RequestOptions.DEFAULT);
// iterate over the resulting documents
for (int i = 0; i < ids.size(); i++) {
if (response.getResponses()[i].getResponse().isExists()) {
documents.add(Integer.parseInt(element));
}
}
return documents;
}
我正在尝试使用 java 高级 rest-client 获取索引中存在的所有文档的列表。
样本索引数据为-
PUT /my-index/_doc/1
{
"account_number": 1,
"balance": 28838
}
PUT /my-index/_doc/2
{
"account_number": 1,
"balance": 28838
}
PUT /my-index/_doc/3
{
"account_number": 1,
"balance": 28838
}
要通过 ID 检索多个 JSON 文档,我使用 multi-get API,如下所示
GET /_mget
{
"ids": [
"2",
"3",
"4"
]
}
搜索结果是
{
"docs": [
{
"_index": "my-index",
"_type": "_doc",
"_id": "2",
"_version": 1,
"_seq_no": 4,
"_primary_term": 4,
"found": true,
"_source": {
"account_number": 2,
"balance": 28838
}
},
{
"_index": "my-index",
"_type": "_doc",
"_id": "3",
"_version": 2,
"_seq_no": 5,
"_primary_term": 4,
"found": true,
"_source": {
"account_number": 3,
"balance": 28838
}
},
{
"_index": "my-index",
"_type": "_doc",
"_id": "4",
"found": false
}
]
}
现在,我需要解析 multi-get 请求生成的响应并获取在索引中找到的所有 ID 的列表。
Java代码
我能够获得 ID 列表,即 [2,3]
(这是预期的结果)。但是如下面的 java 代码所示,我每次都使用 for
循环从 ids
列表中添加一个元素(文档 ID)。因此每次都会创建一个新的 multi-get 请求。
public List<Integer> verifyDocuments(final VerifyScrollRequest request) throws IOException {
RestHighLevelClient es7Client = buildES7Client(request.getEs7Node(), request.getEs7Port());
List<String> ids = new ArrayList<>();
ids.add("2");
ids.add("3");
ids.add("4");
List<Integer> documents = new ArrayList<>();
MultiGetRequest getRequest = new MultiGetRequest();
for (int i = 0; i < ids.size(); i++) {
String element = ids.get(i);
getRequest.add(new MultiGetRequest.Item(request.getEs7IndexName(), element));
MultiGetResponse response = es7Client.mget(getRequest, RequestOptions.DEFAULT);
if (response.getResponses()[i].getResponse().isExists()) {
documents.add(Integer.parseInt(element));
}
}
return documents;
}
有没有办法将完整的Id列表作为参数传递给multi-get请求,让multi-get请求只创建一次?
您不需要在每次迭代时都发送调用 es7Client.mget()
。我会这样做:
public List<Integer> verifyDocuments(final VerifyScrollRequest request) throws IOException {
RestHighLevelClient es7Client = buildES7Client(request.getEs7Node(), request.getEs7Port());
// build the list of IDs
List<String> ids = new ArrayList<>();
ids.add("2");
ids.add("3");
ids.add("4");
List<Integer> documents = new ArrayList<>();
// build the mget request with all IDs
MultiGetRequest getRequest = new MultiGetRequest();
for (int i = 0; i < ids.size(); i++) {
String element = ids.get(i);
getRequest.add(new MultiGetRequest.Item(request.getEs7IndexName(), element));
}
// call mget
MultiGetResponse response = es7Client.mget(getRequest, RequestOptions.DEFAULT);
// iterate over the resulting documents
for (int i = 0; i < ids.size(); i++) {
if (response.getResponses()[i].getResponse().isExists()) {
documents.add(Integer.parseInt(element));
}
}
return documents;
}