メインコンテンツまでスキップ
バージョン: User Guides (BYOC)

Boolean & Number

ブールフィールドまたは数値フィールドは、ブール値または数値を格納するスカラーフィールドです。これらの値は、2つの可能な値のいずれか、または整数(integers)および小数(浮動小数点数)のいずれかになります。通常、数量や測定値、または論理的・数学的に処理する必要のあるデータを表現するために使用されます。

以下の表は、Zilliz Cloud クラスターで利用可能な数値フィールドのデータ型を示しています。

Field Type

Description

BOOL

Boolean type for storing true or false, suitable for describing binary states.

INT8

8-bit integer, suitable for storing small-range integer data.

INT16

16-bit integer, for medium-range integer data.

INT32

32-bit integer, ideal for general integer data storage like product quantities or user IDs.

INT64

64-bit integer, suitable for storing large-range data like timestamps or identifiers.

FLOAT

32-bit floating-point number, for data requiring general precision, such as ratings or temperature.

DOUBLE

64-bit double-precision floating-point number, for high-precision data like financial information or scientific calculations.

ブールフィールドを宣言するには、datatypeBOOL に設定します。数値フィールドを宣言するには、利用可能な数値データ型のいずれかに設定します。たとえば、整数フィールドの場合は データType.INT64、浮動小数点数フィールドの場合は データType.FLOAT を使用します。

📘Notes

Zilliz Cloud supports null values and default values for boolean and number fields. To enable these features, set nullable to True and default_value to a numeric value. For details, refer to NULL許容 & Default.

Add boolean and number fields

ブール値または数値データを格納するには、コレクションスキーマ内で対応するタイプのフィールドを定義します。以下は、2つの数値フィールドを持つコレクションスキーマの例です:

  • age: 整数データを格納し、NULL許容で、デフォルト値は 18 です。

  • broken: ブールデータを格納し、NULL許容ですが、デフォルト値はありません。

  • price: 浮動小数点数データを格納し、NULL許容ですが、デフォルト値はありません。

📘Notes

If you set enable_dynamic_fields=True when defining the schema, Zilliz Cloud allows you to insert スカラーフィールド that were not defined in advance. However, this may increase the complexity of queries and management, potentially impacting performance. For more information, refer to Dynamic Field.

# 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 an INT64 field `age` that supports null values with default value 18
schema.add_field(field_name="age", datatype=DataType.INT64, nullable=True, default_value=18)
schema.add_field(field_name="broken", datatype=DataType.BOOL, nullable=True)
# Add a FLOAT field `price` that supports null values without default value
schema.add_field(field_name="price", datatype=DataType.FLOAT, 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)

インデックスパラメータの設定

インデックス作成は、検索およびクエリのパフォーマンスを向上させます。Zilliz Cloudクラスターでは、ベクトルフィールドに対してはインデックス作成が必須ですが、スカラーフィールドに対しては任意です。

次の例では、ベクトルフィールド embedding とスカラーフィールド age の両方に AUTOINDEX インデックスタイプを使用してインデックスを作成しています。このタイプでは、Milvusがデータ型に基づいて最も適したインデックスを自動的に選択します。詳細については、AUTOINDEX Explained を参照してください。

# Set index params

index_params = client.prepare_index_params()

# Index `age` with AUTOINDEX
index_params.add_index(
field_name="age",
index_type="AUTOINDEX",
index_name="age_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
client.create_collection(
collection_name="my_collection",
schema=schema,
index_params=index_params
)

データの挿入

コレクションを作成した後、スキーマに一致するエンティティを挿入します。

# Sample data
data = [
{"age": 25, "price": 99.99, "pk": 1, "embedding": [0.1, 0.2, 0.3]},
{"age": 30, "pk": 2, "embedding": [0.4, 0.5, 0.6]}, # `price` field is missing, which should be null
{"age": None, "price": None, "pk": 3, "embedding": [0.2, 0.3, 0.1]}, # `age` should default to 18, `price` is null
{"age": 45, "price": None, "pk": 4, "embedding": [0.9, 0.1, 0.4]}, # `price` is null
{"age": None, "price": 59.99, "pk": 5, "embedding": [0.8, 0.5, 0.3]}, # `age` should default to 18
{"age": 60, "price": None, "pk": 6, "embedding": [0.1, 0.6, 0.9]} # `price` is null
]

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

フィルター式を使用したクエリ

エンティティを挿入した後、指定されたフィルター式に一致するエンティティを取得するには query メソッドを使用します。

age が 30 より大きいエンティティを取得するには:

filter = 'age > 30'

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

print(res)

# Example output:
# data: [
# "{'age': 45, 'price': None, 'pk': 4}",
# "{'age': 60, 'price': None, 'pk': 6}"
# ]

price が null であるエンティティを取得するには:

filter = 'price is null'

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

print(res)

# Example output:
# data: [
# "{'age': 30, 'price': None, 'pk': 2}",
# "{'age': 18, 'price': None, 'pk': 3}",
# "{'age': 45, 'price': None, 'pk': 4}",
# "{'age': 60, 'price': None, 'pk': 6}"
# ]

age が値 18 を持つエンティティを取得するには、以下の式を使用します。age のデフォルト値は 18 であるため、期待される結果には、age が明示的に 18 に設定されているエンティティ、または age が null に設定されているエンティティが含まれます。

filter = 'age == 18'

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

print(res)

# Example output:
# data: [
# "{'age': 18, 'price': None, 'pk': 3}",
# "{'age': 18, 'price': 59.99, 'pk': 5}"
# ]

フィルター式を用いたベクトル検索

数値フィールドによる基本的なフィルタリングに加えて、ベクトル類似性検索と数値フィールドフィルターを組み合わせることもできます。たとえば、以下のコードはベクトル検索に数値フィールドフィルターを追加する方法を示しています。

filter = "25 <= age <= 35"

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

print(res)

# Example output:
# data: [
# "[{'id': 2, 'distance': -0.2016308456659317, 'entity': {'age': 30, 'price': None}}, {'id': 1, 'distance': -0.23643313348293304, 'entity': {'age': 25, 'price': 99.98999786376953}}]"
# ]

この例では、まずクエリベクトルを定義し、検索時にフィルター条件 25 <= age <= 35 を追加します。これにより、検索結果がクエリベクトルに類似しているだけでなく、指定された年齢範囲にも合致することが保証されます。詳細については、フィルタリング を参照してください。