Skip to main content

Create a collection

Weaviate stores data in "collections". A collection is a set of objects that share the same data structure. In our movie database, we might have a collection of movies, a collection of actors, and a collection of reviews.

Here we will create a collection of movies.

Code

This example creates a collection for the movie data:

import weaviate from "weaviate-client";
import { WeaviateClient, configure, vectorizer } from "weaviate-client";

// Instantiate your client (not shown). e.g.:
// const requestHeaders = {'X-OpenAI-Api-Key': process.env.OPENAI_APIKEY as string,}
// client = weaviate.connectToWeaviateCloud(..., headers: requestHeaders) or
// client = weaviate.connectToLocal(..., headers: requestHeaders)

await client.collections.create({
name: "Movie",
properties: [
{ name: "title", dataType: configure.dataType.TEXT},
{ name: "overview", dataType: configure.dataType.TEXT},
{ name: "vote_average", dataType: configure.dataType.NUMBER},
{ name: "genre_ids", dataType: configure.dataType.INT_ARRAY},
{ name: "release_date", dataType: configure.dataType.DATE},
{ name: "tmdb_id", dataType: configure.dataType.INT},
],
// Define the vectorizer module
vectorizers: vectorizer.text2VecOpenAI(),
// Define the generative module
generative: configure.generative.openAI(),
})

client.close()

Each collection definition must have a name. Then, you can define additional parameters like we've done in this example.

Explain the code

Properties

Properties are the object attributes that you want to store in the collection. Each property has a name and a data type.

In our movie database, we have properties like title, release_date and genre_ids, with data types like TEXT (string), DATE (date), or INT (integer). It's also possible to have arrays of integers, like we have with genre_ids.

Auto-schema

Weaviate can automatically infer the schema from the data. However, it's a good practice to define the properties explicitly, for better control and to avoid surprises.

Vectorizer configuration

If you do not specify the vector yourself, Weaviate will use a specified vectorizer to generate vector embeddings from your data.

In this code example, we specify the text2vec-openai module with default options.

    vectorizers: vectorizer.text2VecOpenAI(),

Generative configuration

If you wish to use your collection with a generative model (e.g. a large language model), you must specify the generative module.

In this code example, we specify the openai module (generative-openai is the full name) with default options.

    generative: configure.generative.openAI(),

Javascript classes

The code example makes use of methods such as property, dataType and configure. They are defined in the weaviate.configure object and are used to define the collection.

import weaviate from "weaviate-client";
import { WeaviateClient, configure, vectorizer } from "weaviate-client";

Questions and feedback

If you have any questions or feedback, let us know in the user forum.