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" });
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.
- Builder API
- Method Annotations
- Mapper File
LambdaTemplate lambda = new LambdaTemplate(dataSource);
int changed = lambda.update(UserInfo.class)
.eq(UserInfo::getUid, "1001")
.updateTo(UserInfo::getName, "new name")
.doUpdate();
@SimpleMapper
public interface UserInfoWriteMapper {
@Update("POST /user_info/_update_by_query\\?refresh=true {\"query\":{\"term\":{\"uid\": #{uid}}},\"script\":{\"source\":\"ctx._source.name = params.name\",\"params\":{\"name\": #{name}}}}")
int rename(@Param("uid") String uid, @Param("name") String name);
}
<mapper namespace="com.example.UserInfoWriteMapper">
<update id="rename">
POST /user_info/_update_by_query\?refresh=true {"query":{"term":{"uid": #{uid}}},"script":{"source":"ctx._source.name = params.name","params":{"name": #{name}}}}
</update>
</mapper>
@RefMapper("/mapper/elastic-write.xml")
public interface UserInfoWriteMapper {
int rename(@Param("uid") String uid, @Param("name") String name);
}
Save the XML at /mapper/elastic-write.xml; the interface belongs to com.example.
Session session = new Configuration().newSession(dataSource);
UserInfoWriteMapper mapper = session.createMapper(UserInfoWriteMapper.class);
int changed = mapper.rename("1001", "new name");
Database Write Operations
| Operation | Database 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.