protected IndexResponse indexItem( DocTableInfo tableInfo, ShardUpsertRequest request, ShardUpsertRequest.Item item, ShardId shardId, boolean tryInsertFirst, int retryCount) throws ElasticsearchException { try { IndexRequest indexRequest; if (tryInsertFirst) { // try insert first without fetching the document try { indexRequest = new IndexRequest(prepareInsert(tableInfo, request, item), request); } catch (IOException e) { throw ExceptionsHelper.convertToElastic(e); } } else { indexRequest = new IndexRequest(prepareUpdate(tableInfo, request, item, shardId), request); } return indexAction.execute(indexRequest).actionGet(); } catch (Throwable t) { if (t instanceof VersionConflictEngineException && retryCount < item.retryOnConflict()) { return indexItem(tableInfo, request, item, shardId, false, retryCount + 1); } else if (tryInsertFirst && item.updateAssignments() != null && t instanceof DocumentAlreadyExistsException) { // insert failed, document already exists, try update return indexItem(tableInfo, request, item, shardId, false, 0); } else { throw t; } } }