# How to index and store pdf file in elastic search using spring boot?

**URL:** <https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289>\
**Category:** Elasticsearch\
**Created:** [February 21, 2020, 4:04am UTC](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289 "2020-02-21T04:04:02Z")\
**Posts on this page:** 12\
**Page:** 3

<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 16, 2020, 8:36am UTC](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289/45 "2020-03-16T08:36:39Z")

</div>

> [@dadoonet](#):
>
> GET my\_index/\_doc/1

Hi David,  
I am trying to extract the content that is stored under \_source.  
I came across GetSourceRequest in [https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-document-get-source.html](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-document-get-source.html)  
But it is no longer present in  
`org.elasticsearch.client.core`  
From where Can I import GetSourceRequest?

---

<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 16, 2020, 9:03am UTC](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289/46 "2020-03-16T09:03:18Z")

</div>

I believe this is a new one which will become available in the 7.7 version.  
In the meantime you can use the GET document API and get the source with it.

See [https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.x/java-rest-high-document-get.html#java-rest-high-document-get-response](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.x/java-rest-high-document-get.html#java-rest-high-document-get-response)

---

<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 20, 2020, 9:39am UTC](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289/47 "2020-03-20T09:39:39Z")

</div>

Hi David,  
I tried using Get API .When I am trying to convert into a file and trying to open ,it is showing as below and i am unable to open it.

```auto
Error
Failed to load PDF document.

```

Here is what I have tried:

```auto
@GetMapping("/getfile")
public void getdata() throws IOException
{
	
	String result="C:\\Users\\Documents\\pdfs\\x.pdf";
	GetRequest getRequest = new GetRequest("twitter","_doc", "56");
	GetResponse getResponse = client().get(getRequest, RequestOptions.DEFAULT);
	byte[] sourceAsBytes =getResponse.getSourceAsBytes();
 	File resultfile = new File(result);
    FileOutputStream fos = new FileOutputStream(resultfile);
    fos.write(sourceAsBytes);
    fos.flush();
    fos.close();	
}

```

I dont know where it is going wrong.

```auto
String sourceAsString = getResponse.getSourceAsString(); 

```

When I am trying to fetch source as string program terminates here.

---

<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 20, 2020, 10:06am UTC](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289/48 "2020-03-20T10:06:01Z")

</div>

You need to read the source, then access to the field where you stored the document in BASE64, then decode the BASE64 and generate the binary from this.

---

<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 20, 2020, 10:22am UTC](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289/49 "2020-03-20T10:22:54Z")

</div>

> [@dadoonet](#):
>
> then access to the field

How to access the field?

---

<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 20, 2020, 10:24am UTC](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289/50 "2020-03-20T10:24:46Z")

</div>

> [@samanvi\_sweety](#):
>
> byte sourceAsBytes =getResponse.getSourceAsBytes();

Does this mean reading the source?

---

<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 20, 2020, 10:40am UTC](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289/51 "2020-03-20T10:40:37Z")

</div>

From [https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-get.html](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-get.html)

If you stored the field itself (in the mapping) the easiest way is probably (assuming a 7.6 version):

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

```

If you did not:

```auto
GetRequest getRequest = new GetRequest("twitter", "56"); 
GetResponse getResponse = client.get(request, RequestOptions.DEFAULT);
if (getResponse.isExists()) {
    Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
    // This will contain your BASE64 content probably 
    String message = sourceAsMap.get("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 24, 2020, 2:26am UTC](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289/52 "2020-03-24T02:26:26Z")

</div>

> [@dadoonet](#):
>
> GetRequest getRequest = new GetRequest("twitter", "56");  
> GetResponse getResponse = client.get(request, RequestOptions.DEFAULT);  
> if (getResponse.isExists()) {  
> Map\<String, Object\> sourceAsMap = getResponse.getSourceAsMap();  
> // This will contain your BASE64 content probably String message = sourceAsMap.get("message");  
> }

Hi David,  
I am using 7.4 version.When I tried using the above code

```auto
Map<String, Object> sourceAsMap = getResponse.getSourceAsMap(); 

```

I dont know why when I am trying to ftech anything in the form of string, program doesn't return anything.

So As you mentioned the different approach by using 7.6 ,I installed 7.6 version and when I am trying to use the new version , it is showing

```auto
master not discovered or elected yet, an election requires a node with id 
[eJcn6jOSRw65wy4liDC1VA], have discovered [{10INLPC0SP3TF}
{1Farf6X-R1u0LM6KNgh4Kw}{4zM2-RhjSwaQxt15w3To9A}{127.0.0.1}{127.0.0.1:9300}

```

How to fix the above problem?

---

<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, 2:41am UTC](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289/53 "2020-03-24T02:41:34Z")

</div>

7.4 or 7.6 should behave the same.

> [@samanvi\_sweety](#):
>
> So As you mentioned the different approach by using 7.6 ,I installed 7.6 version and when I am trying to use the new version , it is showing
> 
> ```auto
> master not discovered or elected yet, an election requires a node with id 
> [eJcn6jOSRw65wy4liDC1VA], have discovered [{10INLPC0SP3TF}
> {1Farf6X-R1u0LM6KNgh4Kw}{4zM2-RhjSwaQxt15w3To9A}{127.0.0.1}{127.0.0.1:9300}
> 
> ```
> 
> How to fix the above problem?

It's a totally different problem. Please open a new question to fix this specific problem. And share details about what you did exactly, including configuration files, logs...

---

<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, 3:07am UTC](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289/54 "2020-03-24T03:07:36Z")

</div>

> [@dadoonet](#):
>
> String message = getResponse.getField("message").getValue();

If i am using the above function to fetch data in field message , it is showing

```auto
{
    "timestamp": "2020-03-24T05:46:40.284+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "No message available",
    "path": "/es/getfile"
}

```

In Elasticsearch it is stored in below format

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

```

Under field "message" pdf is stored.but if i am trying to fetch the field message ,it return null.  
Does this have anything to do with version?

---

<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, 3:08am UTC](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289/56 "2020-03-24T03:08:20Z")

</div>

> [@dadoonet](#):
>
> It's a totally different problem. Please open a new question to fix this specific problem. And share details about what you did exactly, including configuration files, logs.

ok sure

---

<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 21, 2020, 3:08am UTC](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289/57 "2020-04-21T03:08:24Z")

</div>

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

[Previous page](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289.md?page=2)
