メインコンテンツまでスキップ

Array フィールド

ARRAY フィールドは、同じデータ型の要素の順序付きリストを格納します。

以下は、ARRAY フィールドがデータを格納する方法の例です。

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

Limits

  • デフォルト値: ARRAY フィールドはデフォルト値をサポートしません。ただし、nullable 属性を True に設定することで null 値を許可できます。詳細は、Nullable & Default を参照してください。

  • データ型: ARRAY フィールド内のすべての要素は、element_type パラメータで定義される同じデータ型である必要があります。element_typeVARCHAR に設定されている場合は、配列要素の max_length も指定する必要があります。element_type は任意の scalar データ型、JSON、および STRUCT を受け入れます。

  • 配列容量: ARRAY フィールド内の要素数は、Array の作成時に max_capacity で指定した最大容量以下である必要があります。この値は 1 から 4096 の範囲の整数である必要があります。

  • 文字列の扱い: Array フィールド内の文字列値は、意味的なエスケープや変換を行わず、そのまま格納されます。たとえば、'a"b'"a'b"'a\'b'"a\"b" は入力どおりに格納されます。一方、'a'b'"a"b" は無効な値と見なされます。

Add ARRAY field

Zilliz Cloud cluster で ARRAY フィールドを使用するには、collection schema の作成時に関連するフィールド型を定義します。このプロセスには以下が含まれます。

  1. datatype をサポートされている Array データ型 ARRAY に設定します。

  2. element_type パラメータを使用して、配列内の要素のデータ型を指定します。同じ配列内のすべての要素は同じデータ型である必要があります。

  3. max_capacity パラメータを使用して、配列の最大容量、つまり含めることができる要素数の上限を定義します。

以下は、ARRAY フィールドを含む collection schema を定義する方法です。

📘Notes

schema の定義時に enable_dynamic_fields=True を設定すると、Zilliz Cloud では事前に定義されていない scalar フィールドを挿入できます。ただし、これによりクエリや管理の複雑さが増し、パフォーマンスに影響する可能性があります。詳細は、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_fields=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

index は、search と query のパフォーマンス向上に役立ちます。Zilliz Cloud cluster では、vector フィールドに対する index 作成は必須ですが、scalar フィールドに対しては任意です。

以下の例では、vector フィールド embeddingARRAY フィールド tags の両方に、AUTOINDEX index type を使用して index を作成します。この type では、Milvus がデータ型に基づいて最適な index を自動的に選択します。

python
# Set index params

index_params = client.prepare_index_params()

# Index `age` 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
)

Create collection

schema と index を定義したら、ARRAY フィールドを含む collection を作成します。

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

Insert data

collection を作成した後、ARRAY フィールドを含むデータを挿入できます。

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

完全な配列を挿入するだけでなく、ARRAY フィールドは upsert API において ARRAY_APPEND および ARRAY_REMOVE の部分更新演算子もサポートしています。これにより、現在の値を最初に取得することなく、既存の配列に要素を追加したり、一致する要素を削除したりできます。そのため、クライアント側の read-modify-write パターンを回避できます。詳細は、Upsert array fields in merge mode を参照してください。

Query with filter expressions

entity を挿入した後、query メソッドを使用して、指定した filter expression に一致する entity を取得します。

tags が null ではない entity を取得するには、以下を使用します。

python
# Query to exclude 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)

# Example output:
# data: [
# "{'tags': ['pop', 'rock', 'classic'], 'ratings': [5, 4, 3], 'pk': 1}"
# ]

ratings の最初の要素の値が 4 より大きい entity を取得するには、以下を使用します。

python
filter = 'ratings[0] > 4'

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

print(res)

# Example output:
# data: [
# "{'tags': ['pop', 'rock', 'classic'], 'ratings': [5, 4, 3], 'embedding': [0.12, 0.34, 0.56], 'pk': 1}",
# "{'tags': None, 'ratings': [9, 5], 'embedding': [0.18, 0.11, 0.23], 'pk': 3}"
# ]

Vector search with filter expressions

基本的な scalar フィールドのフィルタリングに加えて、vector 類似度 search を scalar フィールドフィルタと組み合わせることができます。たとえば、以下のコードは vector search に scalar フィールドフィルタを追加する方法を示しています。

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)

# Example output:
# data: [
# "[{'id': 1, 'distance': -0.2479381263256073, 'entity': {'tags': ['pop', 'rock', 'classic'], 'ratings': [5, 4, 3], 'embedding': [0.11999999731779099, 0.3400000035762787, 0.5600000023841858]}}]"
# ]

さらに、Zilliz Cloud は ARRAY_CONTAINSARRAY_CONTAINS_ALLARRAY_CONTAINS_ANYARRAY_LENGTH のような高度な Array フィルタリング演算子をサポートしており、query 機能をさらに強化できます。詳細は、ARRAY Operators を参照してください。

Ctrl I