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

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.

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

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.

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%>));

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.

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]]]

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?

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

Full source code below.

	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
	}

What is the elasticsearch server version?

Elastic Search server version is 7.5.2.

Could you try with the same version of the client?

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