Query Operations
dbVisitor treats HTTP method + path + JSON as an executable statement. @Query accepts REST commands, not just SELECT. These examples use Elasticsearch 7 typeless requests. Connection setup: JDBC Elasticsearch.
Entity Mapping
@Table("user_info")
public class UserInfo {
@Column(primary = true)
private String uid;
private String name;
// Getters and setters omitted
}
Index and field name rules are covered under Name Sensitivity.
Exact Matching
Map uid and name as keyword in the example index:
PUT /user_info {"mappings":{"properties":{"uid":{"type":"keyword"},"name":{"type":"keyword"}}}}
Fluent eq generates a match query. On text fields it may match analyzed terms; use keyword fields and the term request below for exact matching.
Executing Query Commands
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
List<UserInfo> rows = jdbc.queryForList(
"POST /user_info/_search {\"query\":{\"term\":{\"uid\": ?}}}",
new Object[] { "1001" }, UserInfo.class);
Querying Through Different APIs
- Builder API
- Method Annotations
- Mapper File
LambdaTemplate lambda = new LambdaTemplate(dataSource);
UserInfo item = lambda.query(UserInfo.class)
.eq(UserInfo::getUid, "1001")
.queryForObject();
Builder queries generate _search requests.
@SimpleMapper
public interface UserInfoMapper {
@Query("POST /user_info/_search {\"query\":{\"term\":{\"uid\": #{uid}}}}")
UserInfo findById(@Param("uid") String uid);
}
Session session = new Configuration().newSession(dataSource);
UserInfoMapper mapper = session.createMapper(UserInfoMapper.class);
UserInfo item = mapper.findById("1001");
<mapper namespace="com.example.UserInfoMapper">
<select id="findById" resultType="com.example.UserInfo">
POST /user_info/_search {"query":{"term":{"uid": #{uid}}}}
</select>
</mapper>
@RefMapper("/mapper/elastic-user.xml")
public interface UserInfoMapper {
UserInfo findById(@Param("uid") String uid);
}
Save the XML as the resource named in @RefMapper; the example classes use package com.example. Create the Mapper through Session as in the annotation example.
Reading a script calculation
In ES 6/7, use Painless script_fields in a search request, not a stored-function call. With at least one document in calculation:
String command = """
POST /calculation/_search
{"size": 1, "_source": false, "query": {"match_all": {}},
"script_fields": {"value": {"script": {
"lang": "painless", "source": "params.x + params.y",
"params": {"x": ?, "y": ?}
}}}}
""";
Integer sum = jdbc.queryForObject(command, new Object[] { 10, 5 }, Integer.class);
The calculation runs per search hit. No hit means no calculation row. Positional and named arguments and scalar conversion are supported; JDBC CallableStatement callbacks, OUT-parameter records, and SQL table-function syntax are not. Keep the script fixed and bind values through params.
See Where Builder for collection predicates and explicit null filtering.
Query Execution Options
Mapper files accept statementType, timeout, and fetchSize; their effect follows the driver settings. For resultSetType, keep DEFAULT or use FORWARD_ONLY. Scrollable result sets (SCROLL_INSENSITIVE and SCROLL_SENSITIVE) are not supported. See Execution Options for XML configuration.
See Result Reading for result columns.