Query Operations
Query Milvus through the Builder API or Mappers, or write SQL supported by JDBC-Milvus. See JDBC Milvus for connection setup.
Before Querying
The examples use the existing, loaded book_vectors collection; see Preparing the Collection for its definition. Load the collection before scalar or vector queries. See Pagination for paged reads.
Entity Mapping
FLOAT_VECTOR fields accept List<Float> directly; the list length must match the field dimension. See Type Support for other vector types.
@Table("book_vectors")
public class BookVector {
@Column(value = "book_id", primary = true)
private Long bookId;
private String title;
@Column("word_count")
private Integer wordCount;
@Column("book_intro")
private List<Float> bookIntro;
// Getters and setters omitted
}
Keys are assigned by the application or generated by collection AutoID; see Key Generation.
Executing Query Commands
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
List<BookVector> rows = jdbc.queryForList(
"SELECT * FROM book_vectors WHERE book_id = ?",
new Object[] { 1001L }, BookVector.class);
Querying Through Different APIs
- Builder API
- Method Annotations
- Mapper File
LambdaTemplate lambda = new LambdaTemplate(dataSource);
BookVector item = lambda.query(BookVector.class)
.eq(BookVector::getBookId, 1001L)
.queryForObject();
Scalar queries use native Query; vector queries use Search.
@SimpleMapper
public interface BookVectorMapper {
@Query("SELECT * FROM book_vectors WHERE book_id = #{id}")
BookVector findById(@Param("id") Long id);
}
Session session = new Configuration().newSession(dataSource);
BookVectorMapper mapper = session.createMapper(BookVectorMapper.class);
BookVector item = mapper.findById(1001L);
<mapper namespace="com.example.BookVectorMapper">
<select id="findById" resultType="com.example.BookVector">
SELECT * FROM book_vectors WHERE book_id = #{id}
</select>
</mapper>
@RefMapper("/mapper/milvus-user.xml")
public interface BookVectorMapper {
BookVector findById(@Param("id") Long id);
}
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.
Vector field mapping, search metrics, and scalar filters are described in Vector Operations.
Query
Field selection, entity and Map results, scalar results, and COUNT(*) are supported. Computed projections, DISTINCT, and general SQL aggregates are not supported. For predicate syntax and optional conditions, see Builder API.
Map Queries
| Entry point | Map key names |
|---|---|
| Entity-based asMap() | Java property names |
| queryFreedom | Milvus field names |
| BaseMapper Map query methods | Registered entity property names |
See Map Writes for writes and NULL properties.
Vector Queries
See Vector Operations for vector mapping, search metrics, and scalar filters.
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 Reading Results for result columns.