ElasticSearchJava 8.5:检索所有ID

xxls0lw8  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(173)

我尝试使用下面的代码检索索引的所有(elasticsearch)Id。

SearchRequest sr = SearchRequest.of(r -> r
        .index("my_index")
        .source(s -> s.fetch(false))
        .size(10000));

    System.out.println("Request: " + sr);

    final SearchResponse<MyDoc> response;
    try {
        response = this.esClient.search(sr, MyDoc.class);
    } catch (ElasticsearchException | IOException e) {
        throw new MyException("fetch all ids: request failed: " + e.getMessage(), e);
    }

    System.out.println("Response: " + response);

响应中没有结果。但是,打印的请求

POST /my_index/_search?typed_keys=true {"_source":false,"size":10000}

它在作为REST请求直接运行时工作得非常好。
你知道如何使用Java客户端吗?
REST响应为

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 999,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "my_index",
        "_id": "2LHJSP6dTjXuEM2vsEDyxdG4Y7HPzXL15tFvrkyZm8xn",
        "_score": 1
      },
      {
        "_index": "my_index",
        "_id": "A8RCf2mV4qeWvLxSfzKNX418E734uEifCenoCAiM3syB",
        "_score": 1
      },
      ...
    ]
  }
}
vkc1a9a2

vkc1a9a21#

也许这对你有用:

SearchRequest sr = SearchRequest.of(r -> r
    .index("idx_name")
    .source(s -> s.fetch(false))
    .size(10000));

System.out.println("Request: " + sr);

try {
  var response = this.esClient.search(sr, Void.class);
  response.hits().hits().forEach(hit -> {
    System.out.println("ID: " + hit.id());
  });
} catch (ElasticsearchException | IOException e) {
}

相关问题