# How to store .pdf or .txt file and search in it using Java

**URL:** <https://discuss.elastic.co/t/how-to-store-pdf-or-txt-file-and-search-in-it-using-java/304025>\
**Category:** Elasticsearch\
**Tags:** language-clients, ingest-pipeline, reindex\
**Created:** [May 5, 2022, 11:35am UTC](https://discuss.elastic.co/t/how-to-store-pdf-or-txt-file-and-search-in-it-using-java/304025 "2022-05-05T11:35:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [May 5, 2022, 11:35am UTC](https://discuss.elastic.co/t/how-to-store-pdf-or-txt-file-and-search-in-it-using-java/304025/1 "2022-05-05T11:35:49Z")

</div>

I am trying to save and search a .txt and .pdf file in Elasticsearch using Java code. Maybe i'm able to save the file but unable to fetch it.  
My controller

```auto
@GetMapping("/finals")
	public String trail1() throws IOException

	{
		RestHighLevelClient client = new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost", 9200, "http"), new HttpHost("localhost", 9201, "http")));

		String filePath = "C:\\Users\\Hp\\Desktop\\phonenumber.txt";
		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);
		
		return createIndexResponse1.toString();

	}

```

When I call /finals on postman, I get the error

```auto
{
    "timestamp": "2022-05-05T09:27:30.386+00:00",
    "status": 500,
    "error": "Internal Server Error",
    "path": "/finals"
}

```

And on STS I get this error

```auto
2022-05-05 16:55:09.697 INFO 5496 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2017 ms
2022-05-05 16:55:10.698 INFO 5496 --- [main] o.s.d.elasticsearch.support.VersionInfo : Version Spring Data Elasticsearch: 4.2.11
2022-05-05 16:55:10.700 INFO 5496 --- [main] o.s.d.elasticsearch.support.VersionInfo : Version Elasticsearch Client in build: 7.12.1
2022-05-05 16:55:10.700 INFO 5496 --- [main] o.s.d.elasticsearch.support.VersionInfo : Version Elasticsearch Client used: 7.12.1
2022-05-05 16:55:10.700 INFO 5496 --- [main] o.s.d.elasticsearch.support.VersionInfo : Version Elasticsearch cluster: 7.12.0
2022-05-05 16:55:11.927 INFO 5496 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8443 (https) with context path ''
2022-05-05 16:55:11.939 INFO 5496 --- [main] c.p.ElasticsearchSpringApplication : Started ElasticsearchSpringApplication in 5.115 seconds (JVM running for 6.022)
2022-05-05 16:55:42.638 INFO 5496 --- [nio-8443-exec-5] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-05-05 16:55:42.640 INFO 5496 --- [nio-8443-exec-5] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-05-05 16:55:42.644 INFO 5496 --- [nio-8443-exec-5] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
OTgyMjAwNDY0NQ==
org.elasticsearch.client.indices.CreateIndexResponse@c97539122022-05-05 16:55:43.378 ERROR 5496 --- [nio-8443-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: mapping type is missing;] with root cause

org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: mapping type is missing;
	at org.elasticsearch.action.ValidateActions.addValidationError(ValidateActions.java:15) ~[elasticsearch-7.12.1.jar:7.12.1]
	at org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest.validate(PutMappingRequest.java:107) ~[elasticsearch-7.12.1.jar:7.12.1]
	at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1620) ~[elasticsearch-rest-high-level-client-7.12.1.jar:7.12.1]
	at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1594) ~[elasticsearch-rest-high-level-client-7.12.1.jar:7.12.1]
	at org.elasticsearch.client.IndicesClient.putMapping(IndicesClient.java:376) ~[elasticsearch-rest-high-level-client-7.12.1.jar:7.12.1]
	at com.project.ElasticsearchSpringApplication.trail1(ElasticsearchSpringApplication.java:218) ~[classes/:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.19.jar:5.3.19]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) ~[tomcat-embed-core-9.0.62.jar:4.0.FR]
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.62.jar:4.0.FR]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.19.jar:5.3.19]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.19.jar:5.3.19]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.19.jar:5.3.19]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.19.jar:5.3.19]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.19.jar:5.3.19]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.19.jar:5.3.19]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

```

Also why it is showing two different versions of Elasticsearch in my STS console?

I am using Elasticsearch 7.12.0 and java 1.8 Spring 2.5.x

Please let me know if you need any additional information regarding this.

---

<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:** [May 5, 2022, 2:01pm UTC](https://discuss.elastic.co/t/how-to-store-pdf-or-txt-file-and-search-in-it-using-java/304025/2 "2022-05-05T14:01:08Z")

</div>

Hi!!

I believe it has something to do with your mapping.

Try something like this, I believe instantiating like this: "new IndexRequest("twitter", "\_doc", "56");" it is already deprecated.

```auto
    IndexRequest request = new IndexRequest("twitter");
    request.source(jsonIndexContent, XContentType.JSON);
    request.opType(OpType.INDEX);
    request.id("56");

```

---

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [May 6, 2022, 5:44am UTC](https://discuss.elastic.co/t/how-to-store-pdf-or-txt-file-and-search-in-it-using-java/304025/4 "2022-05-06T05:44:26Z")

</div>

I did it and now im getting the error `org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: mapping type is missing;2: mapping source is missing;`

---

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [May 6, 2022, 6:03am UTC](https://discuss.elastic.co/t/how-to-store-pdf-or-txt-file-and-search-in-it-using-java/304025/5 "2022-05-06T06:03:02Z")

</div>

My controller looks like

```auto
	@GetMapping("/finals")
	public String trail1() throws IOException

	{
		RestHighLevelClient client = new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost", 9200, "http"), new HttpHost("localhost", 9201, "http")));

		String filePath = "C:\\Users\\Hp\\Desktop\\phonenumber.txt";
		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);
		
		return createIndexResponse1.toString();

	}

```

And my console looks like

```auto

  . _______ _ _
 /\\ / ___'___ _ _(_)_ ____ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  ' | ____| .__ |_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot :: (v2.5.13)

2022-05-06 11:29:06.068 INFO 16596 --- [main] c.p.ElasticsearchSpringApplication : Starting ElasticsearchSpringApplication using Java 17.0.1 on DESKTOP-ADQ0I0S with PID 16596 (D:\STS Project\elasticsearch-spring\target\classes started by Hp in D:\STS Project\elasticsearch-spring)
2022-05-06 11:29:06.074 INFO 16596 --- [main] c.p.ElasticsearchSpringApplication : No active profile set, falling back to 1 default profile: "default"
2022-05-06 11:29:07.062 INFO 16596 --- [main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Elasticsearch repositories in DEFAULT mode.
2022-05-06 11:29:07.130 INFO 16596 --- [main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 63 ms. Found 1 Elasticsearch repository interfaces.
2022-05-06 11:29:07.145 INFO 16596 --- [main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Reactive Elasticsearch repositories in DEFAULT mode.
2022-05-06 11:29:07.151 INFO 16596 --- [main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 4 ms. Found 0 Reactive Elasticsearch repository interfaces.
2022-05-06 11:29:07.898 INFO 16596 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8443 (https)
2022-05-06 11:29:07.917 INFO 16596 --- [main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-05-06 11:29:07.917 INFO 16596 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.62]
2022-05-06 11:29:08.120 INFO 16596 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-05-06 11:29:08.120 INFO 16596 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1925 ms
2022-05-06 11:29:09.148 INFO 16596 --- [main] o.s.d.elasticsearch.support.VersionInfo : Version Spring Data Elasticsearch: 4.2.11
2022-05-06 11:29:09.149 INFO 16596 --- [main] o.s.d.elasticsearch.support.VersionInfo : Version Elasticsearch Client in build: 7.12.1
2022-05-06 11:29:09.149 INFO 16596 --- [main] o.s.d.elasticsearch.support.VersionInfo : Version Elasticsearch Client used: 7.12.1
2022-05-06 11:29:09.149 INFO 16596 --- [main] o.s.d.elasticsearch.support.VersionInfo : Version Elasticsearch cluster: 7.12.0
2022-05-06 11:29:10.222 INFO 16596 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8443 (https) with context path ''
2022-05-06 11:29:10.238 INFO 16596 --- [main] c.p.ElasticsearchSpringApplication : Started ElasticsearchSpringApplication in 4.9 seconds (JVM running for 5.83)
2022-05-06 11:30:45.404 INFO 16596 --- [nio-8443-exec-5] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-05-06 11:30:45.405 INFO 16596 --- [nio-8443-exec-5] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-05-06 11:30:45.406 INFO 16596 --- [nio-8443-exec-5] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
OTgyMjAwNDY0NQ==
2022-05-06 11:30:45.814 ERROR 16596 --- [nio-8443-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is [twitter/VXaJyXGFSuuWzBRY-UmVGw] ElasticsearchStatusException[Elasticsearch exception [type=resource_already_exists_exception, reason=index [twitter/VXaJyXGFSuuWzBRY-UmVGw] already exists]]] with root cause

org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=resource_already_exists_exception, reason=index [twitter/VXaJyXGFSuuWzBRY-UmVGw] already exists]
	at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:176) ~[elasticsearch-7.12.1.jar:7.12.1]
	at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1933) ~[elasticsearch-rest-high-level-client-7.12.1.jar:7.12.1]
	at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1910) ~[elasticsearch-rest-high-level-client-7.12.1.jar:7.12.1]
	at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1667) ~[elasticsearch-rest-high-level-client-7.12.1.jar:7.12.1]
	at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1639) ~[elasticsearch-rest-high-level-client-7.12.1.jar:7.12.1]
	at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1606) ~[elasticsearch-rest-high-level-client-7.12.1.jar:7.12.1]
	at org.elasticsearch.client.IndicesClient.create(IndicesClient.java:134) ~[elasticsearch-rest-high-level-client-7.12.1.jar:7.12.1]
	at com.project.ElasticsearchSpringApplication.trail1(ElasticsearchSpringApplication.java:214) ~[classes/:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.19.jar:5.3.19]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) ~[tomcat-embed-core-9.0.62.jar:4.0.FR]
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.19.jar:5.3.19]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.62.jar:4.0.FR]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.19.jar:5.3.19]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.19.jar:5.3.19]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.19.jar:5.3.19]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.19.jar:5.3.19]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.19.jar:5.3.19]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.19.jar:5.3.19]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
	at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
	Suppressed: org.elasticsearch.client.ResponseException: method [PUT], host [http://localhost:9200], URI [/twitter?master_timeout=30s&timeout=30s], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"resource_already_exists_exception","reason":"index [twitter/VXaJyXGFSuuWzBRY-UmVGw] already exists","index_uuid":"VXaJyXGFSuuWzBRY-UmVGw","index":"twitter"}],"type":"resource_already_exists_exception","reason":"index [twitter/VXaJyXGFSuuWzBRY-UmVGw] already exists","index_uuid":"VXaJyXGFSuuWzBRY-UmVGw","index":"twitter"},"status":400}
		at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:326) ~[elasticsearch-rest-client-7.12.0.jar:7.12.1]
		at org.elasticsearch.client.RestClient.performRequest(RestClient.java:296) ~[elasticsearch-rest-client-7.12.0.jar:7.12.1]
		at org.elasticsearch.client.RestClient.performRequest(RestClient.java:270) ~[elasticsearch-rest-client-7.12.0.jar:7.12.1]
		at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1654) ~[elasticsearch-rest-high-level-client-7.12.1.jar:7.12.1]
		... 54 common frames omitted

```

---

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [May 6, 2022, 6:04am UTC](https://discuss.elastic.co/t/how-to-store-pdf-or-txt-file-and-search-in-it-using-java/304025/6 "2022-05-06T06:04:26Z")

</div>

I took help from this post  
[https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289/46](https://discuss.elastic.co/t/how-to-index-and-store-pdf-file-in-elastic-search-using-spring-boot/220289/46)

---

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [May 6, 2022, 6:28am UTC](https://discuss.elastic.co/t/how-to-store-pdf-or-txt-file-and-search-in-it-using-java/304025/7 "2022-05-06T06:28:13Z")

</div>

> [@RabBit\_BR](#):
>
> ```auto
> IndexRequest request = new IndexRequest("twitter");
> request.source(jsonIndexContent, XContentType.JSON);
> request.opType(OpType.INDEX);
> request.id("56");
> 
> ```

I deleted the twitter index and called finals again, and now im getting the error again `org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: mapping type is missing; `

---

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [May 6, 2022, 10:07am UTC](https://discuss.elastic.co/t/how-to-store-pdf-or-txt-file-and-search-in-it-using-java/304025/8 "2022-05-06T10:07:05Z")

</div>

My issue is resolved, thank you.

---

<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:** [May 6, 2022, 11:51am UTC](https://discuss.elastic.co/t/how-to-store-pdf-or-txt-file-and-search-in-it-using-java/304025/9 "2022-05-06T11:51:14Z")

</div>

Great!!

I would send that:

Import:

> import org.elasticsearch.client.indices.PutMappingRequest;

```auto
// mapping properties
    PutMappingRequest putMappingRequest = new PutMappingRequest("twitter");
    putMappingRequest.source("{\n" + " \"properties\": {\n" + " \"message\": {\n" + " \"type\": \"binary\"\n"
        + " }\n" + " }\n" + "}", XContentType.JSON);
    AcknowledgedResponse acknowledgedResponse = client.indices().putMapping(putMappingRequest, RequestOptions.DEFAULT);
    System.out.println("Put Mapping response : " + acknowledgedResponse.isAcknowledged());

```

---

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [May 9, 2022, 11:03am UTC](https://discuss.elastic.co/t/how-to-store-pdf-or-txt-file-and-search-in-it-using-java/304025/10 "2022-05-09T11:03:21Z")

</div>

Thank you.

---

<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:** [June 6, 2022, 11:03am UTC](https://discuss.elastic.co/t/how-to-store-pdf-or-txt-file-and-search-in-it-using-java/304025/11 "2022-06-06T11:03:40Z")

</div>

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