# ElasticSearch Java API 8.5.0 fields are missing from indexed document

**URL:** https://discuss.elastic.co/t/elasticsearch-java-api-8-5-0-fields-are-missing-from-indexed-document/318576
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [November 9, 2022, 5:53pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-8-5-0-fields-are-missing-from-indexed-document/318576 "2022-11-09T17:53:55Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![AKDAS\_System](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akdas_system/32/98091_2.png) [@AKDAS\_System](https://discuss.elastic.co/u/AKDAS_System)
#### Post date: [November 9, 2022, 5:53pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-8-5-0-fields-are-missing-from-indexed-document/318576/1 "2022-11-09T17:53:55Z")

</div>

Hello. We've been migrating from ES 7 version to 8.5 and discovered this weird behavior.  
In Kibana I can index a test document with a null field value and it will get indexed and will be present in the index.

```auto
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "status": {
        "type": "text"
      },
      "docType": {
        "type": "text"
      }
    }
  }
}
PUT my-index-000001/_doc/1
{
  "status": null,
  "docType":"Contract"
}
GET my-index-000001/_doc/1

```

Field "status" is present in document 1 and has a null value:

```auto
{
  "_index": "my-index-000001",
  "_id": "1",
  "_version": 1,
  "_seq_no": 0,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "status": null,
    "docType": "War"
  }
}

```

When I index a second document with a null docType using Java API it doesn't get indexed:

```auto
@Data
public class Document {
    private String status;
    private String docType;
}

```

```auto
Document doc = new Document();
doc.setStatus("new");

ElasticsearchClient esClient = getClient();
esClient.index(i -> i.index("my-index-000001").id("2").document(doc));

```

This is how the document looks like in the index:

```auto
{
  "_index": "my-index-000001",
  "_id": "2",
  "_version": 1,
  "_seq_no": 1,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "status": "new"
  }
}

```

docType is missing.  
I get that we can't search null fields but we can't even index them and include in JSON? If so, I would have expected the same behavior in Kibana and Java API. Am I missing something in my Java code? Is it possible to include null fields in the indexed documents? I need a unified JSON structure for all my indexed documents, searching on nullable fields is not needed.

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [November 9, 2022, 10:50pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-8-5-0-fields-are-missing-from-indexed-document/318576/2 "2022-11-09T22:50:38Z")

</div>

I don't know java and the client, but if you aren't including the value in the document. Generally, Elasticsearch can't index a field, even if null, if it isn't told about it.

---

<div class="post-metadata">

### Author: ![AKDAS\_System](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akdas_system/32/98091_2.png) [@AKDAS\_System](https://discuss.elastic.co/u/AKDAS_System)
#### Post date: [November 10, 2022, 8:01am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-8-5-0-fields-are-missing-from-indexed-document/318576/3 "2022-11-10T08:01:01Z")

</div>

Thank you for your quick response. It does make perfect sense but it also means that the default object serializer that Elasticsearch Java API uses doesn't serialize nulls. In our case it is a problem so for now our solution is to index JSON like so:

```auto
Gson gson = new GsonBuilder().serializeNulls().create();
String json = gson.toJson(object).replace('\'', '"');
esClient.index(i -> i
                        .index(indexName)
                        .id(docId)
                        .withJson(new StringReader(json ));

```

It would be nice to have the option to tell Elasticsearch to serialize nulls.  
Maybe there is such an option but I couldn't find it in the Java API documentation.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [November 10, 2022, 8:21am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-8-5-0-fields-are-missing-from-indexed-document/318576/4 "2022-11-10T08:21:58Z")

</div>

Why it's a problem? Could you explain? I'm curious.

---

<div class="post-metadata">

### Author: ![AKDAS\_System](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akdas_system/32/98091_2.png) [@AKDAS\_System](https://discuss.elastic.co/u/AKDAS_System)
#### Post date: [November 10, 2022, 8:47am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-8-5-0-fields-are-missing-from-indexed-document/318576/5 "2022-11-10T08:47:12Z")

</div>

We have two systems that are dependent on each other. One indexes documents and uses the index for search (how it's supposed to be used). In this case, our system wouldn't have a problem with not storing null values in the index but the other system uses this same index as a REST service to get documents by id. This other system expects a certain structure of the JSON document and fails if some fields are missing. This is the only reason we need to have null fields. I can't really justify this kind of set up but sadly we have to live with it and its limitations.

---

<div class="post-metadata">

### Author: ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)
#### Post date: [November 10, 2022, 9:08am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-8-5-0-fields-are-missing-from-indexed-document/318576/6 "2022-11-10T09:08:46Z")

</div>

There was indeed an issue that caused `null` values to be filtered out. [This has been fixed recently](https://github.com/elastic/elasticsearch-java/pull/417) and will be part of version 8.5.1 that will be released in the next few days.

---

<div class="post-metadata">

### Author: ![AKDAS\_System](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akdas_system/32/98091_2.png) [@AKDAS\_System](https://discuss.elastic.co/u/AKDAS_System)
#### Post date: [November 10, 2022, 9:22am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-8-5-0-fields-are-missing-from-indexed-document/318576/7 "2022-11-10T09:22:58Z")

</div>

Thank you, good to know, we'll wait for v8.5.1.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [November 10, 2022, 9:34am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-8-5-0-fields-are-missing-from-indexed-document/318576/8 "2022-11-10T09:34:26Z")

</div>

Got it. Thanks for the explanation.

---

<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: [December 8, 2022, 9:35am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-8-5-0-fields-are-missing-from-indexed-document/318576/9 "2022-12-08T09:35:22Z")

</div>

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