Skip to main content

Data Writes

Use dbVisitor to insert, update, and delete data with Elasticsearch REST commands. See Query Operations for queries and entity mapping.

Executing Write Commands

JdbcTemplate jdbc = new JdbcTemplate(dataSource);

jdbc.executeUpdate(
"POST /user_info/_doc\\?refresh=wait_for {\"uid\": ?, \"name\": ?}",
new Object[] { "1001", "mali" });

jdbc.executeUpdate(
"POST /user_info/_update_by_query\\?refresh=true {\"query\":{\"term\":{\"uid\": ?}},\"script\":{\"source\":\"ctx._source.name = params.name\",\"params\":{\"name\": ?}}}",
new Object[] { "1001", "new name" });

jdbc.executeUpdate(
"POST /user_info/_delete_by_query\\?refresh=true {\"query\":{\"term\":{\"uid\": ?}}}",
new Object[] { "1001" });
note

The URL's ? is not a parameter placeholder. Escape it as \\? in Java strings or \? in Mapper files. See Escaping Parameter Markers.

Document IDs and Update Conditions

When inserting through the Builder API or BaseMapper, an assigned single mapped primary key also becomes the document _id. With a direct REST command, the request path determines the document ID. See Key Generation.

When several properties have primary=true, BaseMapper uses all of them to locate records. It does not combine them into _id or create a composite unique constraint. Ensure the combination is unique; otherwise, updates or deletes may affect multiple documents.

Updating Through Different APIs

These examples reuse the entity mapping from Query Operations and change the selected record's name to new name.

LambdaTemplate lambda = new LambdaTemplate(dataSource);
int changed = lambda.update(UserInfo.class)
.eq(UserInfo::getUid, "1001")
.updateTo(UserInfo::getName, "new name")
.doUpdate();
Session session = new Configuration().newSession(dataSource);
UserInfoWriteMapper mapper = session.createMapper(UserInfoWriteMapper.class);
int changed = mapper.rename("1001", "new name");

Database Write Operations

OperationDatabase action
Update selected properties_update_by_query
Delete matching records_delete_by_query

BaseMapper updates and deletes use the same operations.

Read-After-Write

For immediate search visibility after a single-document write, use refresh=wait_for. For Builder API write-then-query calls, configure indexRefresh=true on the connection, at the cost of more frequent index refreshes.

See Query Operations for filters and result reading.

Multiple Writes and Transactions

Multiple commands do not automatically form a transaction; completed writes are not guaranteed to roll back after a failure. See Transaction Support for transaction APIs, propagation, and isolation.