New Elasticsearch Java API: Source is null when search is execute

I want to implement this command using the new ES Java API client but the result shows source as null

POST /test_event_202207/_search?typed_keys=true
{
  "sort":[{"time":{"order":"desc"}}]
}

If I excecute this in Kibana, source is not null.

Does anyboby encounter this kind of problem?

Below is my code:

       SearchRequest searchRequest = SearchRequest.of(r -> r
        .index("test_event_202207")
        .sort(s->s.field(f->f.field("time").order(SortOrder.Desc)))
      );

      final SearchResponse response;
      try {
        response = esClient.search(searchRequest, Void.class);
      } catch (final IOException e) {
        throw new UncheckedIOException(e);
      }

       List<Hit> listSearchHit = response.hits().hits();
      Optional<Hit> searchHit = listSearchHit.stream().findFirst();

When you check the searchHit variable, it is null....

Hi,

I'm thinking your problem is around the use of Void.class. The intention of that parameter is so you can hydrate a POJO with the data from your search. That said, I tried a tweaked version of your example with my codebase and it doesn't compile. It expects List<Hit> listSearchHit.

If I change to List<Hit> and look at the returned value, there are no fields with any data populated.

Try creating a simple class with just your time field and replace Void.class with MyClass.class and see what happens.

1 Like

@bweston

Thank you!

I got your point and I was led to the right direction.
It should be ObjectNode.class since it returns a json string.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.