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

null value in sort hit response causes parsing failure - throws nullpointerexception

Open jetpack1116 opened this issue 2 years ago • 4 comments

Java API client version

7.17.6

Java version

17

Elasticsearch Version

7.17.6

Problem description

I am migrating elasticsearch from old to new elasticsearch java client.

I encounter this problem wherein I issue a request query, then the response has null values. The code below is how I request:

    SearchRequest request = requestBuilder.build();
    SearchResponse<ObjectNode> response;
    try {
      response = elasticCLient.search(request, ObjectNode.class);
    } catch (final IOException e) {
      throw Exception(e);
    }

As you can see it uses ObjectNode.class Problem arises when the response has null value. A null pointer exception happens which I think the response cannot be serialized because of the null value.

The response when viewed in kibana is like this:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "robots_20220613111538",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "path" : "C:/storage/1",
          "name" : "barbie.jpg",
          "collection" : [ ]
        },
        "sort" : [
          null,
          "barbie.jp",
          1664377265000
        ]
      },
      {
        "_index" : "robots_20220613111538",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "path" : C:/storage/1",
          "name" : "hellokitty.jpg",
          "collection" : [ ]
        },
        "sort" : [
          null,
          "hellokitty.jpg",
          1664377266000
        ]
      }
    ]
  }
}

How to deal with this kind of response?

Are there other ways instead of using ObjectNode.class ?

Any help is greatly appreciated.

jetpack1116 avatar Sep 29 '22 04:09 jetpack1116

This causes the exception because of the null value

This is a sort field response from a query

"sort" : [
          null,
          "barbie.jp",
          1664377265000
        ]

jetpack1116 avatar Sep 29 '22 04:09 jetpack1116

This could possibly the cause?

This list is with elements nulls and the serialization broken.

https://github.com/elastic/elasticsearch-java/blob/8e4db1dea8897d3acfca72ab501be7c0bca91e5b/java-client/src/main/java/co/elastic/clients/elasticsearch/core/search/Hit.java#L763

jetpack1116 avatar Sep 29 '22 04:09 jetpack1116

This was the fix in 7.16.2

But it still throws NullPointerException when hits.toString() is called

Currently I am using 7.17.6

image

jetpack1116 avatar Sep 30 '22 01:09 jetpack1116

image

jetpack1116 avatar Sep 30 '22 01:09 jetpack1116

Hello, thank you for reporting this! I have tried reproducing the issue using an updated version of the 7.17 client, and it seems to be working fine for me. Could you let me know if updating the client solves this issue? Thanks again!

l-trotta avatar Feb 23 '24 11:02 l-trotta

Is there a fix for it?

imjuoy avatar Jun 14 '24 18:06 imjuoy

@imjuoy which version are you using? version 2.3.0 of this client doesn't exist

l-trotta avatar Jun 17 '24 12:06 l-trotta