# New Elasticsearch client - Search response cannot be serialized because of null value on sort fields

**URL:** <https://discuss.elastic.co/t/new-elasticsearch-client-search-response-cannot-be-serialized-because-of-null-value-on-sort-fields/315326>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [September 28, 2022, 6:35am UTC](https://discuss.elastic.co/t/new-elasticsearch-client-search-response-cannot-be-serialized-because-of-null-value-on-sort-fields/315326 "2022-09-28T06:35:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ALX\_DM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alx_dm/32/106314_2.png) [@ALX\_DM](https://discuss.elastic.co/u/ALX_DM)\
**Post date:** [September 28, 2022, 6:35am UTC](https://discuss.elastic.co/t/new-elasticsearch-client-search-response-cannot-be-serialized-because-of-null-value-on-sort-fields/315326/1 "2022-09-28T06:35:07Z")

</div>

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:

```auto
    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:

```auto
{
  "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.

---

<div class="post-metadata">

**Author:** ![ALX\_DM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alx_dm/32/106314_2.png) [@ALX\_DM](https://discuss.elastic.co/u/ALX_DM)\
**Post date:** [September 28, 2022, 6:50am UTC](https://discuss.elastic.co/t/new-elasticsearch-client-search-response-cannot-be-serialized-because-of-null-value-on-sort-fields/315326/2 "2022-09-28T06:50:47Z")

</div>

I think this causes the exception because of the `null` value

This is a sort field response from a query

> [@ALX\_DM](#):
>
> ```auto
> "sort" : [
> null,
> "barbie.jp",
> 1664377265000
> ]
> 
> ```

---

<div class="post-metadata">

**Author:** ![ALX\_DM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alx_dm/32/106314_2.png) [@ALX\_DM](https://discuss.elastic.co/u/ALX_DM)\
**Post date:** [September 28, 2022, 7:21am UTC](https://discuss.elastic.co/t/new-elasticsearch-client-search-response-cannot-be-serialized-because-of-null-value-on-sort-fields/315326/3 "2022-09-28T07:21:21Z")

</div>

Is it possible to ignore this `null` values of sort?

---

<div class="post-metadata">

**Author:** ![ALX\_DM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alx_dm/32/106314_2.png) [@ALX\_DM](https://discuss.elastic.co/u/ALX_DM)\
**Post date:** [September 28, 2022, 8:30am UTC](https://discuss.elastic.co/t/new-elasticsearch-client-search-response-cannot-be-serialized-because-of-null-value-on-sort-fields/315326/4 "2022-09-28T08:30:43Z")

</div>

could this be an a bug of the new elasticsearch java client API?

the old Rest High Level elasticsearch client can handle this kind of response....

Im using co.elastic.clients 7.17.4

@swallez

---

<div class="post-metadata">

**Author:** ![ALX\_DM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alx_dm/32/106314_2.png) [@ALX\_DM](https://discuss.elastic.co/u/ALX_DM)\
**Post date:** [September 28, 2022, 8:51am UTC](https://discuss.elastic.co/t/new-elasticsearch-client-search-response-cannot-be-serialized-because-of-null-value-on-sort-fields/315326/5 "2022-09-28T08:51:17Z")

</div>

this is in the debug watch

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/7/573f5fa0ef99ed2c1cdeaece557ccab3d4230823.png)

---

<div class="post-metadata">

**Author:** ![ALX\_DM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alx_dm/32/106314_2.png) [@ALX\_DM](https://discuss.elastic.co/u/ALX_DM)\
**Post date:** [September 28, 2022, 8:56am UTC](https://discuss.elastic.co/t/new-elasticsearch-client-search-response-cannot-be-serialized-because-of-null-value-on-sort-fields/315326/6 "2022-09-28T08:56:08Z")

</div>

this is the query used:

```auto
POST/robots/_search?typed_keys=true&ignore_unavailable=true{
  "_source": {
    "includes": [
      "id",
      "name",
      "path"
    ]
  },
  "from": 0,
  "query": {
    "bool": {
      "minimum_should_match": "1",
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "path.full": {
                    "value": "C:/robot_data"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 1000,
  "sort": [
    {
      "cmeta_str-0001.lowercase": {
        "missing": "_first",
        "order": "asc",
        "unmapped_type": "keyword"
      }
    },
    {
      "name.lowercase": {
        "order": "asc"
      }
    },
    {
      "importedAt": {
        "order": "desc"
      }
    }
  ],
  "track_total_hits": true
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [September 28, 2022, 9:47am UTC](https://discuss.elastic.co/t/new-elasticsearch-client-search-response-cannot-be-serialized-because-of-null-value-on-sort-fields/315326/7 "2022-09-28T09:47:13Z")

</div>

Hi @ALX_DM

I suspect [here](https://github.com/elastic/elasticsearch-java/blob/8e4db1dea8897d3acfca72ab501be7c0bca91e5b/java-client/src/main/java/co/elastic/clients/elasticsearch/core/search/Hit.java#L763). This list is with elements nulls and the serialization broken.

I recommend open a issue in Git.

---

<div class="post-metadata">

**Author:** ![ALX\_DM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alx_dm/32/106314_2.png) [@ALX\_DM](https://discuss.elastic.co/u/ALX_DM)\
**Post date:** [September 29, 2022, 1:03am UTC](https://discuss.elastic.co/t/new-elasticsearch-client-search-response-cannot-be-serialized-because-of-null-value-on-sort-fields/315326/8 "2022-09-29T01:03:50Z")

</div>

@RabBit_BR

Thank you for your reply.

I search in git and found this issue [null value in SearchResponse.hits.hits.sort causes parsing failure · Issue #66 · elastic/elasticsearch-java · GitHub](https://github.com/elastic/elasticsearch-java/issues/66)

It seems to be resolved in 7.16.2 according to this thread [Failure with sorts and new co.elastic.clients:elasticsearch-java client - #11 by swallez](https://discuss.elastic.co/t/failure-with-sorts-and-new-co-elastic-clients-elasticsearch-java-client/291511/11)

But I am using 7.17.4...it is still happening.

Hope @swallez can comment on this?

---

<div class="post-metadata">

**Author:** ![ALX\_DM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alx_dm/32/106314_2.png) [@ALX\_DM](https://discuss.elastic.co/u/ALX_DM)\
**Post date:** [September 29, 2022, 8:38am UTC](https://discuss.elastic.co/t/new-elasticsearch-client-search-response-cannot-be-serialized-because-of-null-value-on-sort-fields/315326/9 "2022-09-29T08:38:52Z")

</div>

i open an issue in git [null value in sort hit response causes parsing failure - throws nullpointerexception · Issue #412 · elastic/elasticsearch-java · GitHub](https://github.com/elastic/elasticsearch-java/issues/412)

---

<div class="post-metadata">

**Author:** ![ALX\_DM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alx_dm/32/106314_2.png) [@ALX\_DM](https://discuss.elastic.co/u/ALX_DM)\
**Post date:** [September 30, 2022, 1:31am UTC](https://discuss.elastic.co/t/new-elasticsearch-client-search-response-cannot-be-serialized-because-of-null-value-on-sort-fields/315326/10 "2022-09-30T01:31:28Z")

</div>

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](https://us1.discourse-cdn.com/elastic/original/3X/4/0/40a96eeb6de77cb4677dcd2ed8f5a6d88c41e2d6.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 28, 2022, 1:32am UTC](https://discuss.elastic.co/t/new-elasticsearch-client-search-response-cannot-be-serialized-because-of-null-value-on-sort-fields/315326/11 "2022-10-28T01:32:02Z")

</div>

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