Exposed 1.3.0 Help

Vector

Exposed provides support for vector data types, allowing you to store and query high-dimensional embeddings directly within your database, as well as to run AI-powered similarity searches. Oracle, SQL Server, MySQL, and MariaDB all support explicit vector types, with PostgreSQL providing the type through the open-source extension pgvector.

Supported types

The exposed-core module supports three main ways to define vector columns using the .vector() method:

  • The most common vector definition that accepts FloatArray values and allows you to define dimensions:

    val embedding = vector("embedding", dimensions = 3)

    This will set the dimensions format as using 32-bit floating-point numbers, if your database supports explicit formats.

  • Vector definition with type inference that sets the underlying format. Currently, the only options available are FloatArray and IntArray:

    val embedding = vector<IntArray>("embedding", 99)

    By default, FloatArray will set a 32-bit floating-point number format, while IntArray will set an 8-bit integer format.

  • Full vector definition with control over the accepted input type, the dimensions, and the underlying format:

    val embedding = vector<FloatArray>("embedding", 2049, VectorFormat.FLOAT64)

    This is useful, for example, when an alternative floating-point format is required, like for 64-bit numbers.

Basic usage

Vector columns store and retrieve data as Kotlin primitive arrays, either FloatArray or IntArray, providing a natural way to work with vector data in your code.

You can define vector columns within a table definition as follows:

object MultipleVectors : Table("multiple_vectors") { val ve1 = vector("ve1", dimensions = 3) val ve2 = vector<IntArray>("ve2", 3) val ve3 = vector<FloatArray>("ve3", 3, format = VectorFormat.FLOAT64) }

Here's an example of inserting data into vector columns:

MultipleVectors.insert { it[ve1] = floatArrayOf(1f, 0f, 0f) it[ve2] = intArrayOf(10, 11, 12) it[ve3] = floatArrayOf(0.234f, 0.345f, 0.567f) }

PostgreSQL usage

PostgreSQL provides support for the vector data type through the installation of the pgvector extension. Exposed assumes that this extension has already been enabled at some point prior to table creation or use of a vector column:

CREATE EXTENSION IF NOT EXISTS vector;

If this is not the case, usage of the column will result in a database exception.

Vector functions

Exposed provides vector distance functions with results being calculated based on the following distance similarity options:

  • Cosine

  • Euclidean

  • Dot product

Here's an example of querying vector data using a distance function:

val distance = MultipleVectors.ve1.cosineDistance(floatArrayOf(0f, 1f, 0f)).alias("cd") MultipleVectors .select(distance) .where { MultipleVectors.ve2.euclideanDistance(intArrayOf(1, 1, 1)) less 5 } .orderBy(distance to SortOrder.ASC) .toList()
Last modified: 14 May 2026