Collections, Objects, Vectorisers and the Local Stack

Article · 9:00

Running Weaviate in Docker with modules, collections and typed properties, vectoriser modules against bring-your-own-vectors, objects and UUIDs, and the limits.

This lesson has 1 runnable example. Press Try yourself! under any of them to open it in the editor.

Running it yourself changes the course. A Docker Compose file gives you Weaviate plus whichever modules you enable: a vectoriser, a reranker, a generative module, a backup module. Modules are declared at startup via ENABLE_MODULES and a module you did not enable is simply absent - which is the commonest first-run confusion, because a collection referencing text2vec-transformers fails if the module is not loaded.

A collection is a schema. It declares:

  • Typed properties: TEXT, TEXT_ARRAY, INT, NUMBER, BOOLEAN, DATE, UUID, GEO_COORDINATES, OBJECT and OBJECT_ARRAY for nesting. Typed means validated on write and typed in filters, which is more than a metadata map gives you.
  • Per-property indexing flags: whether a property is searchable (in the BM25 index), filterable (in the inverted index), and whether it is skipped by the vectoriser. That last one matters: by default a vectoriser concatenates the property values into the text it embeds, so an unskipped sku or created_by field is polluting your vectors with noise.
  • A vector configuration: the vectoriser, the distance metric, the index type and its parameters.
  • References to other collections.

Vectoriser modules against bringing your own vectors is the first real decision. With a vectoriser, you send text and Weaviate embeds it on insert and on query - convenient, consistent (the same model is used for both, which removes a whole class of mistake), and coupling: the model is in your schema, an API-based vectoriser means your text leaves your boundary at both ends, and changing the model is a schema change plus a full re-vectorisation. With vectorizer=None you supply vectors, which keeps the embedding in your pipeline where you can version it.

The guidance for this course: use a local text2vec-transformers module for the laboratories - it is free, nothing leaves the machine, and it demonstrates the convenience honestly - and understand bring-your-own-vectors because it is what you will want when the embedding pipeline needs to be versioned independently of the database.

Objects have a UUID, which you may supply. Supplying a deterministic UUID derived from your own identifier makes imports idempotent, exactly as deterministic ids did in the Pinecone course, and generate_uuid5 from the client does it for you.

The distance metrics are cosine (the default), dot, l2-squared, manhattan and hamming. The same rule applies as in Pinecone: match the metric to the model, and note that Weaviate normalises for cosine so cosine and dot behave differently from each other on unnormalised vectors.

Laboratory exercise 1 - provision and verify.

Stand up Weaviate locally with Docker Compose, with a local text2vec-transformers module and a reranker module enabled. Record the compose file, the enabled modules, the Weaviate version and the resource limits you gave the containers.

Create a collection with at least eight typed properties, including an array, a number, a date and a boolean, with the indexing flags set deliberately and the non-semantic properties skipped by the vectoriser.

Load at least 5,000 objects modelling the capstone's product catalogue, with deterministic UUIDs.

Then demonstrate the schema's effect, each with evidence: write an object with a wrong property type and capture the validation error; query a filter on a non-filterable property and record what happens; and - the important one - vectorise the same object with and without skipping the non-semantic properties and compare the resulting vectors and the recall.

Then demonstrate idempotency by importing the same data twice and showing the object count unchanged.

Finally, record the limits: the request size, the batch size that works best, and the behaviour when a module a collection needs is not enabled.

Deliverable: the compose file and a README, the collection schema with every flag justified, the loader with deterministic UUIDs, the three schema demonstrations including the vectoriser-skip recall comparison, the idempotency evidence, and the limits you recorded.

PY Example 1 199 lines
# ==================================================================
# *** RUNNING IT YOURSELF CHANGES THE COURSE. *** Every laboratory
# here is free.
# ==================================================================
#   docker-compose.yml
#   ---
#   services:
#     weaviate:
#       image: cr.weaviate.io/semitechnologies/weaviate:1.27.0
#       ports: ["8080:8080", "50051:50051"]
#       environment:
#         QUERY_DEFAULTS_LIMIT: 25
#         AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED: 'true'   # LAB ONLY
#         PERSISTENCE_DATA_PATH: '/var/lib/weaviate'
#         DEFAULT_VECTORIZER_MODULE: 'text2vec-transformers'
#         ENABLE_MODULES: 'text2vec-transformers,reranker-transformers,backup-filesystem'
#         TRANSFORMERS_INFERENCE_API: 'http://t2v-transformers:8080'
#         RERANKER_INFERENCE_API: 'http://reranker:8080'
#         BACKUP_FILESYSTEM_PATH: '/var/lib/weaviate/backups'
#       volumes: ["weaviate_data:/var/lib/weaviate"]
#     t2v-transformers:
#       image: cr.weaviate.io/semitechnologies/transformers-inference:sentence-transformers-all-mpnet-base-v2
#       environment: { ENABLE_CUDA: '0' }
#     reranker:
#       image: cr.weaviate.io/semitechnologies/reranker-transformers:cross-encoder-ms-marco-MiniLM-L-6-v2
#       environment: { ENABLE_CUDA: '0' }
#   volumes: { weaviate_data: }
#
# *** MODULES ARE DECLARED AT STARTUP. A module you did not enable is
# *** SIMPLY ABSENT ***, and a collection referencing it fails:
#     "no module with name 'text2vec-transformers' present"
# That is the commonest first-run confusion. ENABLE_MODULES is the
# answer, and it needs a restart.

import weaviate, weaviate.classes as wvc
from weaviate.util import generate_uuid5

client = weaviate.connect_to_local()        # or connect_to_weaviate_cloud
print(client.get_meta()["version"])
print(client.get_meta()["modules"].keys())  # CONFIRM what is loaded

# ==================================================================
# *** A COLLECTION IS A SCHEMA. *** This is the main difference from
# a metadata map, and it buys real things.
# ==================================================================
products = client.collections.create(
    name="Product",

    # --- TYPED PROPERTIES: validated on write, typed in filters ----
    properties=[
        wvc.config.Property(
            name="title", data_type=wvc.config.DataType.TEXT,
            index_searchable=True,      # in the BM25 index
            index_filterable=True),
        wvc.config.Property(
            name="description", data_type=wvc.config.DataType.TEXT,
            index_searchable=True, index_filterable=False),

        # *** THE FLAG THAT MATTERS MOST: skip_vectorization. ***
        # By DEFAULT a vectoriser CONCATENATES THE PROPERTY VALUES
        # into the text it embeds. So an unskipped sku or created_by
        # field is POLLUTING YOUR VECTORS WITH NOISE.
        wvc.config.Property(
            name="sku", data_type=wvc.config.DataType.TEXT,
            index_searchable=True,       # BM25 yes - exact matches
            index_filterable=True,
            skip_vectorization=True,     # *** EMBEDDING no ***
            tokenization=wvc.config.Tokenization.FIELD),
            #                             ^^^^^ FIELD = the whole
            #                             value as one token, which
            #                             is what you want for a code
        wvc.config.Property(
            name="brand", data_type=wvc.config.DataType.TEXT,
            index_filterable=True, index_searchable=True),
        wvc.config.Property(
            name="categories", data_type=wvc.config.DataType.TEXT_ARRAY,
            index_filterable=True, skip_vectorization=True),
        wvc.config.Property(
            name="price", data_type=wvc.config.DataType.NUMBER,
            index_filterable=True, skip_vectorization=True),
        wvc.config.Property(
            name="in_stock", data_type=wvc.config.DataType.BOOL,
            index_filterable=True, skip_vectorization=True),
        wvc.config.Property(
            name="released", data_type=wvc.config.DataType.DATE,
            index_filterable=True, skip_vectorization=True),
        # NESTING, which a flat metadata map cannot do:
        wvc.config.Property(
            name="dimensions", data_type=wvc.config.DataType.OBJECT,
            nested_properties=[
                wvc.config.Property(name="width",  data_type=wvc.config.DataType.NUMBER),
                wvc.config.Property(name="height", data_type=wvc.config.DataType.NUMBER),
            ], skip_vectorization=True),
    ],

    # --- THE VECTOR CONFIGURATION ---------------------------------
    vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_transformers(
        vectorize_collection_name=False,   # do not embed the word
    ),                                     # "Product" into every vector
    vector_index_config=wvc.config.Configure.VectorIndex.hnsw(
        distance_metric=wvc.config.VectorDistances.COSINE,
        ef=64, ef_construction=128, max_connections=32,    # Module 4
    ),
    inverted_index_config=wvc.config.Configure.inverted_index(
        bm25_b=0.75, bm25_k1=1.2,          # standard BM25 parameters
        index_timestamps=True,
    ),
)

# *** DEMONSTRATE THE SKIP_VECTORIZATION EFFECT. *** It is the
# module's most useful measurement:
#   all properties vectorised       recall@5 = 0.74
#   non-semantic skipped            recall@5 = 0.89
#   Same model, same chunk text, same metric. The sku, the price and
#   the dates were diluting every vector.

# ==================================================================
# VECTORISER MODULE vs BRING YOUR OWN VECTORS. The first real choice.
# ==================================================================
# WITH A VECTORISER: send TEXT; Weaviate embeds on insert AND on
# query.
#   + CONVENIENT - your application never handles a float array
#   + CONSISTENT - THE SAME MODEL IS USED FOR BOTH, which removes a
#     whole class of mistake (a query embedded with a different
#     model than the data is a silent disaster, and this prevents it)
#   - COUPLING: the model is IN YOUR SCHEMA
#   - an API vectoriser means YOUR TEXT LEAVES YOUR BOUNDARY at
#     BOTH ends - index time and query time
#   - changing the model is a SCHEMA CHANGE plus a FULL
#     RE-VECTORISATION

# BRING YOUR OWN:
byo = client.collections.create(
    name="ProductBYO",
    vectorizer_config=wvc.config.Configure.Vectorizer.none(),
    vector_index_config=wvc.config.Configure.VectorIndex.hnsw(
        distance_metric=wvc.config.VectorDistances.COSINE),
    properties=[...],
)
byo.data.insert(properties={...}, vector=my_vector, uuid=my_uuid)
#   + the embedding stays in YOUR pipeline, VERSIONED independently
#   + nothing leaves your boundary that you did not send
#   - YOU must use the same model for queries. Nothing checks.
#
# *** THIS COURSE: use a LOCAL text2vec-transformers module for the
# *** laboratories (free, nothing leaves the machine, and it
# *** demonstrates the convenience honestly), and understand BYO,
# *** because it is what you want when the embedding pipeline must be
# *** versioned independently of the database. ***

# ==================================================================
# OBJECTS: a UUID you may supply. Make it DETERMINISTIC.
# ==================================================================
products.data.insert(
    properties={
        "title": "Mechanical keyboard, tenkeyless",
        "description": "Hot-swappable switches, aluminium case...",
        "sku": "KB-4471-B",
        "brand": "Acme",
        "categories": ["keyboards", "peripherals"],
        "price": 89.99,
        "in_stock": True,
        "released": "2026-03-14T00:00:00Z",       # RFC3339, required
        "dimensions": {"width": 35.5, "height": 3.8},
    },
    # *** DETERMINISTIC UUID -> IDEMPOTENT IMPORTS ***, exactly as
    # the Pinecone course's deterministic chunk ids:
    uuid=generate_uuid5("KB-4471-B"),
)
# Import twice, compare the count. If it grows, your UUIDs are not
# deterministic.
print(products.aggregate.over_all(total_count=True).total_count)

obj = products.query.fetch_object_by_id(generate_uuid5("KB-4471-B"),
                                        include_vector=True)
print(len(obj.vector["default"]))        # the dimension the module gave

# ==================================================================
# THE DISTANCE METRICS, and the same rule as Pinecone.
# ==================================================================
#   cosine (default) | dot | l2-squared | manhattan | hamming
#
# *** MATCH THE METRIC TO THE MODEL. *** And note Weaviate
# NORMALISES for cosine, so cosine and dot behave DIFFERENTLY from
# each other on unnormalised vectors - the same subtle,
# error-free degradation the Pinecone course demonstrated.

# ==================================================================
# The limits to record.
# ==================================================================
#   * gRPC and REST request size limits -> batch under them
#   * the best BATCH SIZE is empirical; the client's dynamic batching
#     finds it for you (Module 4)
#   * *** A MODULE A COLLECTION NEEDS BUT IS NOT ENABLED -> the
#     collection cannot be created or queried. *** Try it; the error
#     is clear once you have seen it and baffling the first time.
#   * a filter on a property with index_filterable=False is refused,
#     and ADDING THE FLAG LATER REQUIRES A REINDEX of that property
client.close()

Open this example in the editor and change it. Nothing you type is saved.

Discussion

No questions on this topic yet. Sign in to ask the first one.