Skip to main content

Array Field

An ARRAY field stores an ordered list of elements of the same data type.

Here's an example of how ARRAY fields store data:

json
{
"tags": ["pop", "rock", "classic"],
"ratings": [5, 4, 3]
}

Limits​

  • Default Values: ARRAY fields do not support default values. However, you can set the nullable attribute to True to allow null values. For details, refer to Nullable & Default.

  • Data Type: All elements in an ARRAY field must share the same data type, which is defined by the element_type parameter. When element_type is set to VARCHAR, you must also specify max_length, which defines the maximum number of bytes allowed for each array element. The element_type accepts any scalar data type supported by Zilliz Cloud, with the exception of JSON. For element_type=STRUCT, use a StructArray field and provide a struct_schema; for details, refer to StructArray. A top-level ARRAY field cannot hold vectors or nested arrays, and its elements cannot be null individually; these forms are available only for subfields inside a StructArray field.

  • Array Capacity: The number of elements in an ARRAY field must be less than or equal to the maximum capacity defined when the Array was created, as specified by max_capacity. The value must be greater than 0 and must not exceed proxy.maxArrayCapacity, which defaults to 4096.

  • Text Match: TEXT_MATCH, TEXT_MATCH_FUZZY, and PHRASE_MATCH support only VARCHAR and TEXT fields; applying them to an ARRAY field returns an error. enable_analyzer and enable_match have no effect on ARRAY fields. For details on text match, refer to Text Match.

  • String Handling: String values in Array fields are stored as-is, without semantic escaping or conversion. For example, 'a"b', "a'b", 'a\'b', and "a\"b" are stored as entered, while 'a'b' and "a"b" are considered invalid values.

Add ARRAY field​

To add an ARRAY field to the collection schema, set the following parameters:

  1. Set the field data type to ARRAY.

  2. Set element_type to the shared data type of the elements.

  3. Set max_capacity to the maximum number of elements.

Here’s how to define a collection schema that includes ARRAY fields:

📘Notes

To insert fields not defined in the schema, enable the dynamic field.

python
# Import necessary libraries
from pymilvus import MilvusClient, DataType

# Define server address
SERVER_ADDR = "YOUR_CLUSTER_ENDPOINT"

# Create a MilvusClient instance
client = MilvusClient(uri=SERVER_ADDR)

# Define the collection schema
schema = client.create_schema(
auto_id=False,
enable_dynamic_field=True,
)

# Add `tags` and `ratings` ARRAY fields with nullable=True
schema.add_field(field_name="tags", datatype=DataType.ARRAY, element_type=DataType.VARCHAR, max_capacity=10, max_length=65535, nullable=True)
schema.add_field(field_name="ratings", datatype=DataType.ARRAY, element_type=DataType.INT64, max_capacity=5, nullable=True)
schema.add_field(field_name="pk", datatype=DataType.INT64, is_primary=True)
schema.add_field(field_name="embedding", datatype=DataType.FLOAT_VECTOR, dim=3)

Set index params​

Vector fields require indexes; scalar indexes are optional. This example requests AUTOINDEX on embedding and tags. For explicit ARRAY index choices and filter support, see Array index support.

📘Notes

Scalar indexes, including AUTOINDEX, do not support recursive ARRAY fields or arrays with nullable elements. See Array index support for ordinary-array filters and Index StructArray Fields for subfield rules.

python
# Set index params
index_params = client.prepare_index_params()

# Index `tags` with AUTOINDEX
index_params.add_index(
field_name="tags",
index_type="AUTOINDEX",
index_name="tags_index"
)

# Index `embedding` with AUTOINDEX and specify similarity metric type
index_params.add_index(
field_name="embedding",
index_type="AUTOINDEX", # Use automatic indexing to simplify complex index settings
metric_type="COSINE" # Specify similarity metric type, options include L2, COSINE, or IP
)
Array index support: index types and accelerated filters

Scalar indexes on an ordinary ARRAY field index its elements. Choose from the following explicit index types:

Element typeExplicit index types
BOOL, INT8, INT16, INT32, INT64, VARCHARINVERTED, BITMAP
FLOAT, DOUBLEINVERTED

You can also request AUTOINDEX; see Scalar Index for the default ARRAY selections.

The following filters can use eligible array indexes:

FilterIndex behavior
ARRAY_CONTAINS, ARRAY_CONTAINS_ANY, ARRAY_CONTAINS_ALLINVERTED, BITMAP, or an eligible automatic selection can look up elements regardless of position.
Whole-array == or != on Boolean, integer, or VARCHAR arraysINVERTED can find candidates, then check original arrays for order and length. BITMAP does not serve this candidate path.
Whole-array comparisons on floating-point arraysFall back to scanning.
Element access, such as tags[0], or array lengthElement-membership indexing does not provide index lookup for these operations.

For expression syntax, see ARRAY Operators.

Create collection​

Once the schema and index are defined, create a collection that includes ARRAY fields.

python
client.create_collection(
collection_name="my_collection",
schema=schema,
index_params=index_params
)

# Load the collection into memory before performing searches and queries
client.load_collection(collection_name="my_collection")

Insert data​

After creating the collection, you can insert data that includes ARRAY fields.

python
# Sample data
data = [
{
"tags": ["pop", "rock", "classic"],
"ratings": [5, 4, 3],
"pk": 1,
"embedding": [0.12, 0.34, 0.56]
},
{
"tags": None, # Entire ARRAY is null
"ratings": [4, 5],
"pk": 2,
"embedding": [0.78, 0.91, 0.23]
},
{ # The tags field is completely missing
"ratings": [9, 5],
"pk": 3,
"embedding": [0.18, 0.11, 0.23]
}
]

client.insert(
collection_name="my_collection",
data=data
)
📘Notes

Beyond inserting full arrays, ARRAY fields also support the ARRAY_APPEND and ARRAY_REMOVE partial-update operators on the upsert API. These let you append elements to or remove matching elements from an existing array without first retrieving its current value, which avoids the client-side read-modify-write pattern. For details, see Upsert array fields in merge mode.

Query with filter expressions​

After inserting entities, use the query method to retrieve entities that match the specified filter expressions.

To retrieve entities where the tags is not null:

python
# Query entities where `tags` is not null
filter = 'tags IS NOT NULL'

res = client.query(
collection_name="my_collection",
filter=filter,
output_fields=["tags", "ratings", "pk"]
)

print(res)

To retrieve entities where the value of the first element of ratings is greater than 4:

python
filter = 'ratings[0] > 4'

res = client.query(
collection_name="my_collection",
filter=filter,
output_fields=["tags", "ratings", "embedding"]
)

print(res)

Vector search with filter expressions​

Use ARRAY filters in vector search to restrict the eligible entities:

python
filter = 'tags[0] == "pop"'

res = client.search(
collection_name="my_collection",
data=[[0.3, -0.6, 0.1]],
limit=5,
search_params={"params": {"nprobe": 10}},
output_fields=["tags", "ratings", "embedding"],
filter=filter
)

print(res)

For containment and length filters, see ARRAY Operators.