# When I tried to upsert using UpdateRequest object with type as "UPDATE", I met reject message

**URL:** <https://discuss.elastic.co/t/when-i-tried-to-upsert-using-updaterequest-object-with-type-as-update-i-met-reject-message/225826>\
**Category:** Elasticsearch\
**Created:** [March 31, 2020, 9:20am UTC](https://discuss.elastic.co/t/when-i-tried-to-upsert-using-updaterequest-object-with-type-as-update-i-met-reject-message/225826 "2020-03-31T09:20:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kooin-Shin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kooin-shin/32/65436_2.png) [@Kooin-Shin](https://discuss.elastic.co/u/Kooin-Shin)\
**Post date:** [March 31, 2020, 9:20am UTC](https://discuss.elastic.co/t/when-i-tried-to-upsert-using-updaterequest-object-with-type-as-update-i-met-reject-message/225826/1 "2020-03-31T09:20:13Z")

</div>

First of all, I did tried to upsert to Elastic Search with document. So I used org.elasticsearch.action.update.UpdateRequest object to upsert process.  
The code is blow.

```auto
String type = map.remove("<%=UPDATE%>")+"";
String index = map.remove("<%=INDEX%>")+"";
String id = map.remove("<%=ID%>")+"";

<%=cid%>_request.add(new org.elasticsearch.action.update.UpdateRequest(index, type, id).doc(map, org.elasticsearch.common.xcontent.XContentType.JSON).docAsUpsert(<%=DOC_AS_UPSERT%>));

```

The problem is that when I had put the document without index mapping, it was so successfully working but what if index mapping was exist before or created, it put out message as blow

```auto
ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Rejecting mapping update to [test-es1] as the final mapping would have more than 1 type: [_doc, UPDATE]]]

```

and failed to upsert.

When I had been removing second parameter of UpdateRequest object, everything was so good working. Successful code is down below.

```auto
String type = map.remove("<%=TYPE%>")+"";
String index = map.remove("<%=INDEX%>")+"";
String id = map.remove("<%=ID%>")+"";

<%=cid%>_request.add(new org.elasticsearch.action.update.UpdateRequest(index, id).doc(map, org.elasticsearch.common.xcontent.XContentType.JSON).docAsUpsert(<%=DOC_AS_UPSERT%>));

```

---

<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 31, 2020, 9:33am UTC](https://discuss.elastic.co/t/when-i-tried-to-upsert-using-updaterequest-object-with-type-as-update-i-met-reject-message/225826/2 "2020-03-31T09:33:11Z")

</div>

Welcome!

It's unclear to me what exactly is sent to elasticsearch. At least it seems that you are using `UPDATE` instead of `update` in case it makes sense.

Could you share what is sent to elasticsearch? Not Java code please.

---

<div class="post-metadata">

**Author:** ![Kooin-Shin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kooin-shin/32/65436_2.png) [@Kooin-Shin](https://discuss.elastic.co/u/Kooin-Shin)\
**Post date:** [April 1, 2020, 12:41am UTC](https://discuss.elastic.co/t/when-i-tried-to-upsert-using-updaterequest-object-with-type-as-update-i-met-reject-message/225826/3 "2020-04-01T00:41:22Z")

</div>

> [@Kooin-Shin](#):
>
> UpdateRequest

what I mean is when I created 'UpdateRequest' object to upsert with type as seconds parameter of constructor, Elastic Search server responded reject message out.  
But after I removed type parameter on UpdateRequest object, everything is OK. see below.

new UpdateRequest(index, type, id) -----------\> It's out reject message.  
new UpdateRequest(index, id) ------------\> It's OK.

Elastic Search's reject message is below.

ElasticsearchException[Elasticsearch exception [type=illegal\_argument\_exception, reason=Rejecting mapping update to [test-es1] as the final mapping would have more than 1 type: [\_doc, UPDATE]]]

---

<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:** [April 1, 2020, 2:53am UTC](https://discuss.elastic.co/t/when-i-tried-to-upsert-using-updaterequest-object-with-type-as-update-i-met-reject-message/225826/4 "2020-04-01T02:53:01Z")

</div>

Is there a way for us to reproduce the problem? Could you share a full code which helps to reproduce what is happening?

Which language are you using?

---

<div class="post-metadata">

**Author:** ![Kooin-Shin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kooin-shin/32/65436_2.png) [@Kooin-Shin](https://discuss.elastic.co/u/Kooin-Shin)\
**Post date:** [April 1, 2020, 8:00am UTC](https://discuss.elastic.co/t/when-i-tried-to-upsert-using-updaterequest-object-with-type-as-update-i-met-reject-message/225826/5 "2020-04-01T08:00:25Z")

</div>

OS : Windows  
Language : Java 1.8  
ElasticSearch Client lib version : 7.1.5

Full source code below.

```auto
	public void qElasticSearchBlukRequest() throws IOException {
		///begin_start
		///<%
		CodeGeneratorArgument codeGenArgument = null;
		INode node = null;
		String cid = null;
		
		String USE_AUTH = "false";
		String USER = "";
		String PASSWD = "";		
		String PROTOCOL = "http";		
		String HOST = "localhost";		
		String PORT = "9200";
		String REQUEST_TYPE = "UPDATE";
		String BULK_COUNT = "10000";
		String FLUSH_TIMEOUT = "1";
		String INDEX = "simple100";
		String ID_COLUMN = "id";
		String ACTIVE_SHARDS = "ALL";
		String TIMEOUT = "3";
		boolean DIE_ON_ERROR = false;		
		///%>
		
		///*<%=*//*%>*/
		boolean $_USE_AUTH = Boolean.parseBoolean(/*<%=*/USE_AUTH/*%>*/);
		String $_USER = /*<%=*/USER/*%>*/;
		String $_PASSWD = /*<%=*/PASSWD/*%>*/;		
		String $_PROTOCOL = /*<%=*/PROTOCOL/*%>*/;		
		String $_HOST = /*<%=*/HOST/*%>*/;		
		int $_PORT = Integer.parseInt(/*<%=*/PORT/*%>*/);		
		String $_REQUEST_TYPE = /*<%=*/REQUEST_TYPE/*%>*/;
		int $_BULK_COUNT = Integer.parseInt(/*<%=*/BULK_COUNT/*%>*/);
		int $_FLUSH_TIMEOUT = Integer.parseInt(/*<%=*/FLUSH_TIMEOUT/*%>*/);
		String $_INDEX = /*<%=*/INDEX/*%>*/;
		String $_ID_COLUMN = /*<%=*/ID_COLUMN/*%>*/;
		String $_ACTIVE_SHARDS = /*<%=*/ACTIVE_SHARDS/*%>*/;
		int $_TIMEOUT = Integer.parseInt(/*<%=*/TIMEOUT/*%>*/);
		boolean $_DIE_ON_ERROR = /*<%=*/DIE_ON_ERROR/*%>*/;		
		
		long $_row_count = 0;
		HashMap $_bulkMap = new HashMap();
		ObjectMapper $_mapper = new ObjectMapper();
		
		RestClientBuilder $_builder = null;
		RestHighLevelClient $_client = null;
		if($_USE_AUTH) {
			final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
			credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials($_USER, $_PASSWD));
			$_builder = RestClient.builder(new HttpHost($_HOST, $_PORT, $_PROTOCOL)).setHttpClientConfigCallback(new HttpClientConfigCallback() {													
						        @Override
						        public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
						            return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
						        }
						    });
			$_client = new RestHighLevelClient($_builder);
		} else {
			$_builder = RestClient.builder(new HttpHost($_HOST, $_PORT, $_PROTOCOL));
			$_client = new RestHighLevelClient($_builder);
		}

		if($_FLUSH_TIMEOUT > 0) {
			Timer $_timer = new Timer();
			$_timer.schedule(new TimerTask() {
				@Override
				public void run() {
				}
			}, $_FLUSH_TIMEOUT*1000, $_FLUSH_TIMEOUT*1000);
		}		
		///begin_end
		
		///main_start
		String $_id = "0";
		String $_json = "[\"BooBoo\", \"HaHa\", \"HiHi\"]";
		$_bulkMap.put("aaa", $_mapper.readValue($_json, ArrayList.class));
		System.out.println($_mapper.readValue($_json, ArrayList.class));
		$_row_count++;
		
		BulkRequest $_request = null;
		try {
			$_request = new BulkRequest();
			$_request.timeout(TimeValue.timeValueMinutes(2));
			if($_REQUEST_TYPE.equals("INDEX")) {
				$_request.add(new IndexRequest($_INDEX).id($_id).source($_bulkMap, XContentType.JSON));
			} else if($_REQUEST_TYPE.equals("UPDATE")) {
				$_request.add(new UpdateRequest($_INDEX, $_REQUEST_TYPE, $_id).doc($_bulkMap, XContentType.JSON).upsert($_bulkMap));
				//Below line is working well.
				//$_request.add(new UpdateRequest($_INDEX, $_id).doc($_bulkMap, XContentType.JSON).upsert($_bulkMap));
			} else if($_REQUEST_TYPE.equals("DELETE")) {
				$_request.add(new DeleteRequest($_INDEX, $_id));
			} else {
				throw new RuntimeException("Request type is wrong: "+$_REQUEST_TYPE);
			}
		} catch(Exception e) {
			if($_DIE_ON_ERROR) {
				throw e;
			}
			e.printStackTrace();
		}
		try {
			BulkResponse $_res = $_client.bulk($_request, RequestOptions.DEFAULT);		
			for(BulkItemResponse $_item : $_res.getItems()) {
				if($_item.isFailed()) {
					System.out.println($_item.getFailureMessage());
					///main_end		
					///end_start
				}
			}			
			globalMap.put(/*<%=*/cid/*%>*/+"_FLUSH", false);
		} catch(Exception e) {
			if($_DIE_ON_ERROR) {
				throw e;
			}
			e.printStackTrace();
		}			
		$_client.close();
		System.out.println("############# END BULK ############");
		///end_end
	}

```

 ![캡처](https://us1.discourse-cdn.com/elastic/original/3X/1/5/158b3084abe632f170a6f50d68967a5f925d0846.png)

---

<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:** [April 1, 2020, 8:14am UTC](https://discuss.elastic.co/t/when-i-tried-to-upsert-using-updaterequest-object-with-type-as-update-i-met-reject-message/225826/6 "2020-04-01T08:14:39Z")

</div>

What is the elasticsearch server version?

---

<div class="post-metadata">

**Author:** ![Kooin-Shin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kooin-shin/32/65436_2.png) [@Kooin-Shin](https://discuss.elastic.co/u/Kooin-Shin)\
**Post date:** [April 1, 2020, 8:35am UTC](https://discuss.elastic.co/t/when-i-tried-to-upsert-using-updaterequest-object-with-type-as-update-i-met-reject-message/225826/7 "2020-04-01T08:35:53Z")

</div>

Elastic Search server version is 7.5.2.

---

<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:** [April 1, 2020, 9:25am UTC](https://discuss.elastic.co/t/when-i-tried-to-upsert-using-updaterequest-object-with-type-as-update-i-met-reject-message/225826/8 "2020-04-01T09:25:39Z")

</div>

Could you try with the same version of the client?

---

<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 29, 2020, 9:25am UTC](https://discuss.elastic.co/t/when-i-tried-to-upsert-using-updaterequest-object-with-type-as-update-i-met-reject-message/225826/9 "2020-04-29T09:25:42Z")

</div>

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