Skip to main content

Insert Data into StructArray Fields

Public Preview

Insert data into a StructArray field when each entity contains an ordered list of structured elements. In the insert payload, a StructArray field is represented as an array of objects. Each object represents one Struct element and uses the Struct subfield names defined in the collection schema.

This page uses the tech_articles collection from Create a StructArray Field. Each entity is a technical article, and the chunks field stores article chunks as Struct elements.

Before you begin​

Make sure the collection schema already contains the chunks StructArray field.

FieldTypeInsert value
doc_idINT64Article ID.
titleVARCHARArticle title.
categoryVARCHARArticle category.
title_vectorFLOAT_VECTORArticle-level embedding.
chunksARRAYA list of chunk objects.

Each object in chunks must follow the Struct schema.

SubfieldTypeInsert value
textVARCHARChunk text.
sectionVARCHARSection name, such as index, search, or filter.
pageINT64Page number or logical position.
quality_scoreFLOATChunk-level score.
has_codeBOOLWhether the chunk contains code.
emb_list_vectorFLOAT_VECTORVector written for EmbeddingList search.
embFLOAT_VECTORVector written for element-level search.
📘Notes

In an insert payload, chunks is a regular field whose value is an array of Struct objects. Inside each object, use subfield names such as text and emb. Use path syntax, such as chunks[text] or chunks[emb], only after insertion when you create indexes, run searches, build filters, or specify output fields.

Understand the insert payload shape​

The chunks value is an array of Struct elements. Each element is an object whose keys are subfield names.

json
{
"doc_id": 1,
"title": "StructArray indexing patterns",
"category": "index",
"title_vector": [0.12, 0.08, 0.32, 0.48],
"chunks": [
{
"text": "Create one index for each vector subfield.",
"section": "index",
"page": 1,
"quality_score": 0.96,
"has_code": false,
"emb_list_vector": [0.10, 0.20, 0.30, 0.40],
"emb": [0.10, 0.20, 0.30, 0.40]
},
{
"text": "Use MAX_SIM metrics for EmbeddingList search.",
"section": "index",
"page": 2,
"quality_score": 0.91,
"has_code": true,
"emb_list_vector": [0.16, 0.24, 0.35, 0.45],
"emb": [0.16, 0.24, 0.35, 0.45]
}
]
}

emb_list_vector and emb are separate vector subfields because they support different search modes. EmbeddingList search treats all vectors in a StructArray field as one embedding list and returns entity-level results with MAX_SIM* metrics. Element-level search searches each Struct element independently and can return the matched element offset. This example stores the same vector values in both fields for simplicity. In a production application, you can store the same embeddings in both subfields when both search modes use the same chunk embedding, or store different embeddings when the two search modes use different representations.

Insert rows​

Use the insert operation to write the following three entities. Each entity includes all required Struct subfields. The Go example uses column-based input: each map groups the subfield arrays for one entity.

python
from pymilvus import MilvusClient

client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN",
)

data = [
{
"doc_id": 1,
"title": "StructArray indexing patterns",
"category": "index",
"title_vector": [0.12, 0.08, 0.32, 0.48],
"chunks": [
{
"text": "Create one index for each vector subfield.",
"section": "index",
"page": 1,
"quality_score": 0.96,
"has_code": False,
"emb_list_vector": [0.10, 0.20, 0.30, 0.40],
"emb": [0.10, 0.20, 0.30, 0.40],
},
{
"text": "Use MAX_SIM metrics for EmbeddingList search.",
"section": "index",
"page": 2,
"quality_score": 0.91,
"has_code": True,
"emb_list_vector": [0.16, 0.24, 0.35, 0.45],
"emb": [0.16, 0.24, 0.35, 0.45],
},
],
},
{
"doc_id": 2,
"title": "Filtered StructArray search",
"category": "filter",
"title_vector": [0.20, 0.18, 0.22, 0.40],
"chunks": [
{
"text": "Use element_filter to match scalar conditions within the same Struct element.",
"section": "filter",
"page": 1,
"quality_score": 0.93,
"has_code": True,
"emb_list_vector": [0.21, 0.18, 0.33, 0.44],
"emb": [0.21, 0.18, 0.33, 0.44],
},
{
"text": "MATCH_LEAST checks how many elements satisfy a predicate.",
"section": "filter",
"page": 2,
"quality_score": 0.88,
"has_code": False,
"emb_list_vector": [0.24, 0.22, 0.31, 0.39],
"emb": [0.24, 0.22, 0.31, 0.39],
},
],
},
{
"doc_id": 3,
"title": "Element-level search with offsets",
"category": "search",
"title_vector": [0.33, 0.11, 0.29, 0.37],
"chunks": [
{
"text": "Element-level search can return the offset of the matched Struct element.",
"section": "search",
"page": 1,
"quality_score": 0.95,
"has_code": False,
"emb_list_vector": [0.32, 0.14, 0.28, 0.41],
"emb": [0.32, 0.14, 0.28, 0.41],
}
],
},
]

result = client.insert(
collection_name="tech_articles",
data=data,
)

print(result)

Insert into nullable StructArray fields​

Run this optional example only if the parent chunks field was created with nullable enabled. NULL applies to the entire field, not an individual Struct element or subfield. An empty array is a present value and is not NULL.

python
client.insert(
collection_name="tech_articles",
data=[
{
"doc_id": 10,
"title": "Article without chunks yet",
"category": "draft",
"title_vector": [0.05, 0.10, 0.15, 0.20],
"chunks": None,
}
],
)

For a non-NULL StructArray value, provide valid values for every required subfield. Do not mix NULL subfield arrays with present subfield arrays in the same entity.

A Struct element itself cannot be null. Element-nullable subfields can hold null values in individual Struct elements, as described in the next section.

📘Notes

For version and deployment availability, see StructArray Limits. A StructArray field added to an existing collection must be nullable; existing entities have NULL for the whole added field.

Insert null values for individual Struct elements​

An element-nullable subfield can hold a null value in some Struct elements and a valid value in others. The following example assumes that the chunks Struct schema defines a lang VARCHAR subfield with nullable=True and a tags array subfield with nullable=True and element_nullable=True. For the complete schema definition, see Allow null values in Struct elements.

In a Struct element, set an element-nullable subfield to None or omit it. Both store a null value.

python
client.insert(
collection_name="tech_articles",
data=[
{
"doc_id": 11,
"title": "Chunks with missing attributes",
"category": "search",
"title_vector": [0.07, 0.11, 0.17, 0.21],
"chunks": [
{
"text": "Detected language and tags are present.",
"section": "search",
"page": 1,
"quality_score": 0.90,
"has_code": False,
"lang": "en",
"tags": ["search", "tuning"],
"emb_list_vector": [0.12, 0.22, 0.32, 0.42],
"emb": [0.12, 0.22, 0.32, 0.42],
},
{
"text": "Language is unknown and tags are omitted.",
"section": "search",
"page": 2,
"quality_score": 0.84,
"has_code": False,
"lang": None,
"emb_list_vector": [0.14, 0.24, 0.34, 0.44],
"emb": [0.14, 0.24, 0.34, 0.44],
},
],
}
],
)

In the second Struct element, lang is explicitly null and tags is omitted, so Zilliz Cloud stores null for both in that element. The other subfields still require a value in every Struct element, and a Struct element itself cannot be null. On an array subfield, None stores a null inner array, while [] stores an empty inner array.

Null subfield values behave as follows in later operations:

  • Query results return null for the subfield value in that element.

  • In filters, a null value makes the element predicate unknown rather than false. Use $[lang] is null or $[lang] is not null inside element_filter or MATCH_* to test it. For details, see StructArray Operators.

  • Null vectors in an element-nullable vector subfield do not participate in search.

Validate inserted data​

Before loading the collection, create the required vector indexes as described in Index StructArray Fields. The following examples wait for loading to complete, then query selected subfields. For cURL, install jq to check the load-state response. With bounded-staleness consistency, a query immediately after insertion may not include the new rows; repeat it after the writes become visible, or configure strong consistency when creating the example collection.

python
client.load_collection("tech_articles")

rows = client.query(
collection_name="tech_articles",
filter="doc_id in [1, 2, 3]",
output_fields=[
"doc_id",
"title",
"chunks[text]",
"chunks[section]",
"chunks[quality_score]",
],
)

for row in rows:
print(row)

Use StructArray field paths, such as chunks[text], only when you query, search, filter, or create indexes. Insert payloads should still use nested objects under chunks.

Insert rules​

RuleExplanation
Use an array of objects for a StructArray field.The value of chunks is a list, and each item in the list is a Struct element.
Use subfield names inside each Struct element.Insert {"text": "...", "emb": [...]} inside chunks, not {"chunks[text]": "..."}.
Match the Struct schema.Each Struct element must use the subfields defined in the Struct schema. An element-nullable subfield can be omitted or set to None in a Struct element.
Match vector dimensions.Vector values must match the dim configured for their vector subfields.
Respect max_capacity.The number of Struct elements in one entity must not exceed the max_capacity of the StructArray field.
Use separate vector subfields for separate search modes.If both EmbeddingList search and element-level search are required, write vector values to both vector subfields.
Use null only when the field is nullable.Non-nullable StructArray fields require valid StructArray values.

Common mistakes​

  • Using field paths such as chunks[text] in insert payloads.

  • Omitting a subfield that is not element-nullable from a Struct element.

  • Inserting vectors with the wrong dimension.

  • Inserting more Struct elements than max_capacity allows.

  • Setting a subfield to None in a Struct element when the subfield is not element-nullable, or inserting a null Struct element such as [None, {...}].

  • Writing vectors only to emb_list_vector and then trying to run element-level search on chunks[emb].

  • Writing vectors only to emb and then trying to run EmbeddingList search on chunks[emb_list_vector].

Next steps​

  1. To create indexes for chunks[emb_list_vector], chunks[emb], and scalar subfields, read Index StructArray Fields.

  2. To search StructArray vector subfields, read Basic Vector Search with StructArray.

  3. To review nullable behavior and version-specific limitations, read StructArray Limits.