elasticsearch-java icon indicating copy to clipboard operation
elasticsearch-java copied to clipboard

Retrieving ids: REST request succeeds but not using Java Client

Open bgrieder opened this issue 3 years ago • 1 comments

Java API client version

8.5

Java version

8

Elasticsearch Version

8.5.3

Problem description

I am trying to retrieve all (elasticsearch) Ids of an index using the following code.

    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);

There are no results in the response. However, the printed request is

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

which works perfectly fine when run directly as a REST request.

Any idea how to do it using the Java client?

The REST response is

{
  "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
      },
      ...
    ]
  }
}

bgrieder avatar Dec 30 '22 09:12 bgrieder

Hello, thank you for the report! Not sure why your request is not returning results, here's how I wrote it with the java client:

        SearchResponse<Object> res = esClient.search(s -> s
                .size(10000)
                .index("my_index")
                .source(sr -> sr.fetch(false))
            ,Object.class);

        List<String> allIds = res.hits().hits().stream().map(Hit::id).toList();

which seems to be working fine, let me know if this is still an issue for you.

l-trotta avatar Mar 22 '24 16:03 l-trotta