> ## Documentation Index
> Fetch the complete documentation index at: https://docs.topk.io/llms.txt
> Use this file to discover all available pages before exploring further.

# topk-js/schema

[topk-js](/sdk/topk-js/index) / schema

## Functions

### binaryVector()

```ts theme={null}
function binaryVector(options: VectorOptions): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `binary_vector` values.

Example:

```javascript theme={null}
import { binaryVector } from "topk-js/schema";

await client.collections().create("books", {
  title_embedding: binaryVector({ dimension: 128 })
});
```

**Parameters**

| Parameter | Type                                                           |
| --------- | -------------------------------------------------------------- |
| `options` | [`VectorOptions`](/sdk/topk-js/Namespace.schema#vectoroptions) |

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### bool()

```ts theme={null}
function bool(): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `bool` values.

Example:

```javascript theme={null}
import { bool } from "topk-js/schema";

await client.collections().create("books", {
  is_published: bool()
});
```

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### bytes()

```ts theme={null}
function bytes(): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `bytes` values.

Example:

```javascript theme={null}
import { bytes } from "topk-js/schema";

await client.collections().create("books", {
  image: bytes()
});
```

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### f16Vector()

```ts theme={null}
function f16Vector(options: VectorOptions): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `f16_vector` values.

Example:

```javascript theme={null}
import { f16Vector } from "topk-js/schema";

await client.collections().create("books", {
  title_embedding: f16Vector({ dimension: 1536 })
});
```

**Parameters**

| Parameter | Type                                                           |
| --------- | -------------------------------------------------------------- |
| `options` | [`VectorOptions`](/sdk/topk-js/Namespace.schema#vectoroptions) |

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### f32SparseVector()

```ts theme={null}
function f32SparseVector(): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `f32_sparse_vector` values.

Note: Sparse vectors use u32 dimension indices to support dictionaries of up to 2^32 - 1 terms.

Example:

```javascript theme={null}
import { f32SparseVector } from "topk-js/schema";

await client.collections().create("books", {
  sparse_field: f32SparseVector()
});
```

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### f32Vector()

```ts theme={null}
function f32Vector(options: VectorOptions): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `f32_vector` values.

Example:

```javascript theme={null}
import { f32Vector } from "topk-js/schema";

await client.collections().create("books", {
  title_embedding: f32Vector({ dimension: 1536 })
});
```

**Parameters**

| Parameter | Type                                                           |
| --------- | -------------------------------------------------------------- |
| `options` | [`VectorOptions`](/sdk/topk-js/Namespace.schema#vectoroptions) |

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### f8Vector()

```ts theme={null}
function f8Vector(options: VectorOptions): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `f8_vector` values.

Example:

```javascript theme={null}
import { f8Vector } from "topk-js/schema";

await client.collections().create("books", {
  title_embedding: f8Vector({ dimension: 1536 })
});
```

**Parameters**

| Parameter | Type                                                           |
| --------- | -------------------------------------------------------------- |
| `options` | [`VectorOptions`](/sdk/topk-js/Namespace.schema#vectoroptions) |

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### float()

```ts theme={null}
function float(): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `float` values.

Example:

```javascript theme={null}
import { float } from "topk-js/schema";

await client.collections().create("books", {
  price: float()
});
```

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### i8Vector()

```ts theme={null}
function i8Vector(options: VectorOptions): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `i8_vector` values.

Example:

```javascript theme={null}
import { i8Vector } from "topk-js/schema";

await client.collections().create("books", {
  title_embedding: i8Vector({ dimension: 1536 })
});
```

**Parameters**

| Parameter | Type                                                           |
| --------- | -------------------------------------------------------------- |
| `options` | [`VectorOptions`](/sdk/topk-js/Namespace.schema#vectoroptions) |

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### int()

```ts theme={null}
function int(): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `int` values.

Example:

```javascript theme={null}
import { int } from "topk-js/schema";

await client.collections().create("books", {
  published_year: int()
});
```

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### keywordIndex()

```ts theme={null}
function keywordIndex(): FieldIndex;
```

Creates a [FieldIndex](https://docs.topk.io/sdk/topk-js/schema#FieldIndex) type for `keyword_index` values.

Example:

```javascript theme={null}
import { text, keywordIndex } from "topk-js/schema";

await client.collections().create("books", {
  title: text().index(keywordIndex())
});
```

Adding a keyword index allows you to perform keyword search on this field.

**Returns**

[`FieldIndex`](/sdk/topk-js/Namespace.schema#fieldindex)

***

### list()

```ts theme={null}
function list(options: ListOptions): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `list` values.

Example:

```javascript theme={null}
import { list } from "topk-js/schema";

await client.collections().create("books", {
  tags: list({ valueType: "text" })
});
```

**Parameters**

| Parameter | Type                                                       |
| --------- | ---------------------------------------------------------- |
| `options` | [`ListOptions`](/sdk/topk-js/Namespace.schema#listoptions) |

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### matrix()

```ts theme={null}
function matrix(options: MatrixOptions): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `matrix` values.

Example:

```javascript theme={null}
import { matrix } from "topk-js/schema";

await client.collections().create("books", {
  token_embeddings: matrix({ dimension: 7, valueType: "f32" })
});
```

**Parameters**

| Parameter | Type                                                           |
| --------- | -------------------------------------------------------------- |
| `options` | [`MatrixOptions`](/sdk/topk-js/Namespace.schema#matrixoptions) |

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### multiVectorIndex()

```ts theme={null}
function multiVectorIndex(options: MultiVectorIndexOptions): FieldIndex;
```

Creates a [FieldIndex](https://docs.topk.io/sdk/topk-js/schema#FieldIndex) type for `multi_vector_index` values.

Example:

```javascript theme={null}
import { matrix, multiVectorIndex } from "topk-js/schema";

await client.collections().create("books", {
  token_embeddings: matrix({ dimension: 7, valueType: "f32" }).index(
    multiVectorIndex({ metric: "max_sim" })
  )
});
```

**Parameters**

| Parameter | Type                                                                               |
| --------- | ---------------------------------------------------------------------------------- |
| `options` | [`MultiVectorIndexOptions`](/sdk/topk-js/Namespace.schema#multivectorindexoptions) |

**Returns**

[`FieldIndex`](/sdk/topk-js/Namespace.schema#fieldindex)

***

### semanticIndex()

```ts theme={null}
function semanticIndex(): FieldIndex;
```

Creates a [FieldIndex](https://docs.topk.io/sdk/topk-js/schema#FieldIndex) type for `semantic_index` values.

Example:

```javascript theme={null}
import { text, semanticIndex } from "topk-js/schema";

await client.collections().create("books", {
  title: text().index(semanticIndex())
});
```

**Returns**

[`FieldIndex`](/sdk/topk-js/Namespace.schema#fieldindex)

***

### struct()

```ts theme={null}
function struct(fields: Record<string, FieldSpec>): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `struct` values.

Example:

```javascript theme={null}
import { struct, text, int } from "topk-js/schema";

await client.collections().create("books", {
  meta: struct({ author: text(), year: int() })
});
```

**Parameters**

| Parameter | Type                                                                        |
| --------- | --------------------------------------------------------------------------- |
| `fields`  | `Record`\<`string`, [`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)> |

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### text()

```ts theme={null}
function text(): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `text` values.

Example:

```javascript theme={null}
import { text } from "topk-js/schema";

await client.collections().create("books", {
  title: text()
});
```

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### u8SparseVector()

```ts theme={null}
function u8SparseVector(): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `u8_sparse_vector` values.

Note: Sparse vectors use u32 dimension indices to support dictionaries of up to 2^32 - 1 terms.

Example:

```javascript theme={null}
import { u8SparseVector } from "topk-js/schema";

await client.collections().create("books", {
  sparse_field: u8SparseVector()
});
```

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### u8Vector()

```ts theme={null}
function u8Vector(options: VectorOptions): FieldSpec;
```

Creates a [FieldSpec](https://docs.topk.io/sdk/topk-js/schema#FieldSpec) type for `u8_vector` values.

Example:

```javascript theme={null}
import { u8Vector } from "topk-js/schema";

await client.collections().create("books", {
  title_embedding: u8Vector({ dimension: 1536 })
});
```

**Parameters**

| Parameter | Type                                                           |
| --------- | -------------------------------------------------------------- |
| `options` | [`VectorOptions`](/sdk/topk-js/Namespace.schema#vectoroptions) |

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

***

### vectorIndex()

```ts theme={null}
function vectorIndex(options: VectorIndexOptions): FieldIndex;
```

Creates a [FieldIndex](https://docs.topk.io/sdk/topk-js/schema#FieldIndex) type for `vector_index` values.

Supported `metric`s:

* `euclidean` (not supported for sparse vectors)
* `cosine` (not supported for sparse vectors)
* `dot_product` (supported for dense and sparse vectors)
* `hamming` (only supported for binary\_vector type)

Example:

```javascript theme={null}
import { f32Vector, vectorIndex } from "topk-js/schema";

await client.collections().create("books", {
  title_embedding: f32Vector({ dimension: 1536 }).index(
    vectorIndex({ metric: "cosine" })
  )
});
```

**Parameters**

| Parameter | Type                                                                     |
| --------- | ------------------------------------------------------------------------ |
| `options` | [`VectorIndexOptions`](/sdk/topk-js/Namespace.schema#vectorindexoptions) |

**Returns**

[`FieldIndex`](/sdk/topk-js/Namespace.schema#fieldindex)

## Classes

### FieldIndex

**`Internal`**

***

### FieldSpec

**`Internal`**

**Methods**

##### index()

```ts theme={null}
index(index: FieldIndex): FieldSpec;
```

Creates an index on a field.

Example:

```javascript theme={null}
import { text, keywordIndex } from "topk-js/schema";

await client.collections().create("books", {
  title: text().index(keywordIndex())
});
```

**Parameters**

| Parameter | Type                                                     |
| --------- | -------------------------------------------------------- |
| `index`   | [`FieldIndex`](/sdk/topk-js/Namespace.schema#fieldindex) |

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

##### required()

```ts theme={null}
required(): FieldSpec;
```

Marks the field as required. All fields are optional by default.

Example:

```javascript theme={null}
import { text } from "topk-js/schema";

await client.collections().create("books", {
  title: text().required()
});
```

**Returns**

[`FieldSpec`](/sdk/topk-js/Namespace.schema#fieldspec)

## Interfaces

### ListOptions

Options for list field specifications.

This struct contains configuration options for list fields,
including the type of values the list can contain.

**Properties**

| Property                                  | Type                                                           | Description                             |
| ----------------------------------------- | -------------------------------------------------------------- | --------------------------------------- |
| <a id="property-valuetype" /> `valueType` | [`ListValueType`](/sdk/topk-js/Namespace.schema#listvaluetype) | The type of values the list can contain |

***

### MatrixOptions

Options for matrix field specifications.

This struct contains configuration options for matrix fields,
including the dimension and value type.

**Properties**

| Property                                    | Type                                                               | Description                                     |
| ------------------------------------------- | ------------------------------------------------------------------ | ----------------------------------------------- |
| <a id="property-dimension" /> `dimension`   | `number`                                                           | The dimension (number of columns) of the matrix |
| <a id="property-valuetype-1" /> `valueType` | [`MatrixValueType`](/sdk/topk-js/Namespace.schema#matrixvaluetype) | The value type of the matrix elements           |

***

### MultiVectorIndexOptions

Options for multi-vector index specifications.

This struct contains configuration options for multi-vector indexes,
including the distance metric to use.

**Properties**

| Property                                         | Type                                                                               | Description                                            |
| ------------------------------------------------ | ---------------------------------------------------------------------------------- | ------------------------------------------------------ |
| <a id="property-metric" /> `metric`              | `"maxsim"`                                                                         | The distance metric to use for multi-vector similarity |
| <a id="property-quantization" /> `quantization?` | [`MultiVectorQuantization`](/sdk/topk-js/Namespace.schema#multivectorquantization) | The quantization to use for multi-vector values        |
| <a id="property-topk" /> `topK?`                 | `number`                                                                           | Top-k projected values to keep                         |
| <a id="property-width" /> `width?`               | `number`                                                                           | Width of the sparse projection                         |

***

### VectorIndexOptions

Options for vector index specifications.

This struct contains configuration options for vector indexes,
including the distance metric to use.

**Properties**

| Property                              | Type                                                                         | Description                                      |
| ------------------------------------- | ---------------------------------------------------------------------------- | ------------------------------------------------ |
| <a id="property-metric-1" /> `metric` | [`VectorDistanceMetric`](/sdk/topk-js/Namespace.schema#vectordistancemetric) | The distance metric to use for vector similarity |

***

### VectorOptions

Options for vector field specifications.

This struct contains configuration options for vector fields,
including the required dimension parameter.

**Properties**

| Property                                    | Type     | Description                 |
| ------------------------------------------- | -------- | --------------------------- |
| <a id="property-dimension-1" /> `dimension` | `number` | The dimension of the vector |

## Type Aliases

### KeywordIndexType

```ts theme={null}
type KeywordIndexType = "text";
```

***

### ListValueType

```ts theme={null}
type ListValueType = "text" | "integer" | "float";
```

***

### MatrixValueType

```ts theme={null}
type MatrixValueType = "f32" | "f16" | "f8" | "u8" | "i8";
```

***

### MultiVectorDistanceMetric

```ts theme={null}
type MultiVectorDistanceMetric = "maxsim";
```

***

### MultiVectorQuantization

```ts theme={null}
type MultiVectorQuantization = "1bit" | "2bit" | "scalar";
```

***

### VectorDistanceMetric

```ts theme={null}
type VectorDistanceMetric = "cosine" | "euclidean" | "dot_product" | "hamming";
```
