# How to fetch field content from elasticsearch using java API

**URL:** <https://discuss.elastic.co/t/how-to-fetch-field-content-from-elasticsearch-using-java-api/224823>\
**Category:** Elasticsearch\
**Created:** [March 24, 2020, 11:49am UTC](https://discuss.elastic.co/t/how-to-fetch-field-content-from-elasticsearch-using-java-api/224823 "2020-03-24T11:49:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![samanvi\_sweety](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samanvi_sweety/32/56190_2.png) [@samanvi\_sweety](https://discuss.elastic.co/u/samanvi_sweety)\
**Post date:** [March 24, 2020, 11:49am UTC](https://discuss.elastic.co/t/how-to-fetch-field-content-from-elasticsearch-using-java-api/224823/1 "2020-03-24T11:49:18Z")

</div>

Hie team,  
I am trying to fetch content of "message" field which is of type binary,but it returns null.  
In elasticsearch data is stored as below

```auto
{
  "_index": "twitter",
  "_type": "_doc",
  "_id": "56",
  "_version": 1,
  "_seq_no": 0,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "message": "JVBERi0xLjcNCg0KNCAwIG9iag0KPDwNCi9FIDYwNDQyDQovSCBbIDM......"
  }
}

```

My java code looks as below

```auto
GetRequest getRequest = new GetRequest("twitter","_doc", "56");
getRequest.fetchSourceContext(FetchSourceContext.FETCH_SOURCE);
getRequest.storedFields("message");
GetResponse getResponse = client().get(getRequest, RequestOptions.DEFAULT);
String message = getResponse.getField("message").getValue();
System.out.println("msg\t"+message); 

```

It throws null pointer exception

When I am trying to execute via postman `http://localhost:9200/twitter/_doc/56?stored_fields` . It is just returning

```auto
{
    "_index": "twitter",
    "_type": "_doc",
    "_id": "56",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true
}

```

How to fetch content of message ?

---

<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:** [March 24, 2020, 12:21pm UTC](https://discuss.elastic.co/t/how-to-fetch-field-content-from-elasticsearch-using-java-api/224823/2 "2020-03-24T12:21:54Z")

</div>

What is the mapping?

---

<div class="post-metadata">

**Author:** ![samanvi\_sweety](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samanvi_sweety/32/56190_2.png) [@samanvi\_sweety](https://discuss.elastic.co/u/samanvi_sweety)\
**Post date:** [March 25, 2020, 4:40am UTC](https://discuss.elastic.co/t/how-to-fetch-field-content-from-elasticsearch-using-java-api/224823/3 "2020-03-25T04:40:48Z")

</div>

Sorry I didn't get you.  
You mean how did I map messaage?  
Below is what I did

```auto
	String filePath="C:\\Users\\xyz.pdf";
	String encodedfile = null;
	
	File file = new File(filePath);
	try {
	    FileInputStream fileInputStreamReader = new FileInputStream(file);
	    byte[] bytes = new byte[(int) file.length()];
	    fileInputStreamReader.read(bytes);
	    encodedfile = new String(Base64.getEncoder().encodeToString(bytes));
	} catch (IOException e) {
	    e.printStackTrace();
	}
	
	System.out.println(encodedfile);
	//creating index
	CreateIndexRequest request = new CreateIndexRequest("twitter");
	CreateIndexResponse createIndexResponse = client().indices().create(request, RequestOptions.DEFAULT);
    System.out.print(createIndexResponse);
    
	
  
    //mapping properties
    PutMappingRequest request2 = new PutMappingRequest("twitter");
    
    request2.source(
	        "{\n" +
	        " \"properties\": {\n" +
	        " \"message\": {\n" +
	        " \"type\": \"binary\"\n" +
	        " }\n" +
	        " }\n" +
	        "}", 
	        XContentType.JSON);
    AcknowledgedResponse putMappingResponse = client().indices().putMapping(request2, RequestOptions.DEFAULT);
   

	IndexRequest request3 = new IndexRequest("twitter","_doc","56"); 
	 request3.source("{\n" + " \"message\": " +"\""+ encodedfile +"\""+ "}", XContentType.JSON);
	
	IndexResponse createIndexResponse1= client().index(request3, RequestOptions.DEFAULT);
 

```

---

<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:** [March 25, 2020, 5:45am UTC](https://discuss.elastic.co/t/how-to-fetch-field-content-from-elasticsearch-using-java-api/224823/4 "2020-03-25T05:45:30Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Please provide it as a Kibana script which can be run in the Dev Console not as Java code. Once we will have a working script, we can translate it to java.

---

<div class="post-metadata">

**Author:** ![samanvi\_sweety](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samanvi_sweety/32/56190_2.png) [@samanvi\_sweety](https://discuss.elastic.co/u/samanvi_sweety)\
**Post date:** [March 25, 2020, 11:16am UTC](https://discuss.elastic.co/t/how-to-fetch-field-content-from-elasticsearch-using-java-api/224823/5 "2020-03-25T11:16:58Z")

</div>

Here is what I have done  
1)created an index named "twitter" and provided properties

```auto
PUT twitter
{
  "mappings": {
    "properties": {
      "message": {
        "type": "binary"
      }
    }
  }
}

```

OUTPUT:-

```auto
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "twitter"
}

```

2)Passing the data to message field

```auto
PUT twitter/_doc/56
{
  "message": "JVBERi0xLjcNCg0KNCAwIG9iag0KPDwNCi9FIDYwNDQyDQovSCBbIDM......" 
}

```

OUTPUT:-

```auto
{
    "_index": "twitter",
    "_type": "_doc",
    "_id": "56",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

```

To retrieve the data

```auto
GET twitter/_doc/56

```

OUTPUT:-

```auto
{
  "_index": "twitter",
  "_type": "_doc",
  "_id": "56",
  "_version": 1,
  "_seq_no": 0,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "message": "JVBERi0xLjcNCg0KNCAwIG9iag0KPDwNCi9FIDYwNDQyDQovSCBbIDM......"
  }
}

```

Everything is going smooth.Now i would to extract the content of message field using java by using GET API

```auto
GetRequest getRequest = new GetRequest("twitter","_doc", "56");
getRequest.fetchSourceContext(FetchSourceContext.FETCH_SOURCE);
getRequest.storedFields("message");
GetResponse getResponse = client().get(getRequest, RequestOptions.DEFAULT);
String message = getResponse.getField("message").getValue();

```

But here it doesn't fetch the data under message field. It returns null.  
I even tried using but it also doesn't return anything.

```auto
"GetResponse response = client.prepareGet("twitter", "_doc","56").setFields("message").execute().actionGet();"

```

Please I really need help on this.

---

<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:** [March 25, 2020, 12:40pm UTC](https://discuss.elastic.co/t/how-to-fetch-field-content-from-elasticsearch-using-java-api/224823/6 "2020-03-25T12:40:44Z")

</div>

You did not try to run:

```auto
GET twitter/_doc/56?stored_fields=message

```

Otherwise, you'd have seen that it does not produce any field.

So here is the fix:

```auto
DELETE twitter
PUT twitter
{
  "mappings": {
    "properties": {
      "message": {
        "type": "binary",
        "store": true
      }
    }
  }
}
PUT twitter/_doc/56
{
  "message": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=" 
}
GET twitter/_doc/56?stored_fields=message

```

This gives:

```auto
{
  "_index" : "twitter",
  "_type" : "_doc",
  "_id" : "56",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "fields" : {
    "message" : [
      "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="
    ]
  }
}

```

As you can see in the documentation ([Binary field type | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/binary.html)), store is not enabled by default:

> | parameter | description |
> | --- | --- |
> | [`doc_values`](https://www.elastic.co/guide/en/elasticsearch/reference/current/doc-values.html) | Should the field be stored on disk in a column-stride fashion, so that it can later be used for sorting, aggregations, or scripting? Accepts `true` or `false` (default). |
> | [`store`](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-store.html) | Whether the field value should be stored and retrievable separately from the [`_source`](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-source-field.html) field. Accepts `true` or `false` (default). |

So this can not produce any content unless you fix the mapping:

```
getRequest.storedFields("message");

```

---

<div class="post-metadata">

**Author:** ![samanvi\_sweety](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samanvi_sweety/32/56190_2.png) [@samanvi\_sweety](https://discuss.elastic.co/u/samanvi_sweety)\
**Post date:** [March 25, 2020, 1:19pm UTC](https://discuss.elastic.co/t/how-to-fetch-field-content-from-elasticsearch-using-java-api/224823/7 "2020-03-25T13:19:31Z")

</div>

> [@dadoonet](#):
>
> So here is the fix:
> 
> ```auto
> DELETE twitter
> PUT twitter
> {
> "mappings": {
> "properties": {
> "message": {
> "type": "binary",
> "store": true
> }
> }
> }
> }
> PUT twitter/_doc/56
> {
> "message": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=" 
> }
> GET twitter/_doc/56?stored_fields=message
> 
> ```
> 
> This gives:
> 
> ```auto
> {
> "_index" : "twitter",
> "_type" : "_doc",
> "_id" : "56",
> "_version" : 1,
> "_seq_no" : 0,
> "_primary_term" : 1,
> "found" : true,
> "fields" : {
> "message" : [
> "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="
> ]
> }
> }
> 
> ```
> 
> As you can see in the documentation ([Binary field type | Elasticsearch Guide [8.15] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/binary.html)), store is not enabled by default:
> 
> > | parameter | description |
> > | --- | --- |
> > | [`doc_values`](https://www.elastic.co/guide/en/elasticsearch/reference/current/doc-values.html) | Should the field be stored on disk in a column-stride fashion, so that it can later be used for sorting, aggregations, or scripting? Accepts `true` or `false` (default). |
> > | [`store`](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-store.html) | Whether the field value should be stored and retrievable separately from the [`_source`](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-source-field.html) field. Accepts `true` or `false` (default). |
> 
> So this can not produce any content unless you fix the mapping:
> 
> ```auto
> getRequest.storedFields("message");
> 
> ```

Thanku very very much.It got resolved 🙂

---

<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:** [April 22, 2020, 1:19pm UTC](https://discuss.elastic.co/t/how-to-fetch-field-content-from-elasticsearch-using-java-api/224823/8 "2020-04-22T13:19:35Z")

</div>

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