Skip to main content

Execute Commands

Hint

This article is generated by AI translation.

Use JdbcTemplate to directly execute ElasticSearch commands for reading and writing data. Before this, please ensure that the ElasticSearch data source is correctly configured. For details, please refer to ElasticSearch Driver Guide.

Note

See JdbcTemplate for more patterns. The driver does not support:

  • Batching
  • Stored procedures

JdbcTemplate Interface

Define JdbcTemplate, passing the created DataSource or Connection as a parameter.

Create JdbcTemplate
// 2. Create Session
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
// or
JdbcTemplate jdbc = new JdbcTemplate(connection);

Insert Data

Insert Data
// Direct command style
jdbc.execute("POST /my_index/_doc/1 { \"name\": \"mali\", \"age\": 26 }");
// Parameterized command style
jdbc.execute("POST /my_index/_doc/1 { \"name\": ?, \"age\": ? }", new Object[] { "mali", 26 });

Query Data

Query List
// Query all
List<Map<String, Object>> list = jdbc.queryForList("POST /my_index/_search { \"query\": { \"match_all\": {} } }");
Conditional Query
// Query specific condition
Map<String, Object> mali = jdbc.queryForMap("POST /my_index/_search { \"query\": { \"term\": { \"name\": \"mali\" } } }");
String json = (String) mali.get("_DOC");

Update Data

Update Data
// Update age
jdbc.execute("test.user_info.update({name: 'mali'}, {$set: {age: 27}})");

Delete Data

Delete Data
// Delete specific record
jdbc.execute("test.user_info.remove({name: 'mali'})");