字符串类型
在 Zilliz Cloud 中,VARCHAR
是用于存储字符串类型的数据类型,适用于可变长度字符串的存储。它可以存储包含单字节和多字节字符的字符串,最大长度可达 65,535 字符。在定义 VARCHAR
字段时,需要同时指定最大长度参数 max_length
。VARCHAR
字符串类型为您提供了一种高效、灵活的方式来存储和管理文本数据,适用于需要处理不同长度字符串的应用场景。
添加 VARCHAR 字段
要在 Zilliz Cloud 中使用字符串类型,需要在创建 Collection 时定义用于存储字符串的 VARCHAR
字段。这个过程包括:
-
设置
datatype
为支持的字符串数据类型,即VARCHAR
。 -
通过
max_length
参数,指定字符串类型的最大长度,单位为字节。最大不能超过 65,535 字节。
- Python
- Java
- NodeJS
- cURL
from pymilvus import MilvusClient, DataType
client = MilvusClient(uri="YOUR_CLUSTER_ENDPOINT")
# define schema
schema = client.create_schema(
auto_id=False,
enable_dynamic_fields=True,
)
schema.add_field(field_name="varchar_field1", datatype=DataType.VARCHAR, max_length=100)
schema.add_field(field_name="varchar_field2", datatype=DataType.VARCHAR, max_length=200)
schema.add_field(field_name="pk", datatype=DataType.INT64, is_primary=True)
schema.add_field(field_name="embedding", datatype=DataType.FLOAT_VECTOR, dim=3)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.common.DataType;
import io.milvus.v2.service.collection.request.AddFieldReq;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
MilvusClientV2 client = new MilvusClientV2(ConnectConfig.builder()
.uri("YOUR_CLUSTER_ENDPOINT")
.build());
CreateCollectionReq.CollectionSchema schema = client.createSchema();
schema.setEnableDynamicField(true);
schema.addField(AddFieldReq.builder()
.fieldName("varchar_field1")
.dataType(DataType.VarChar)
.maxLength(100)
.build());
schema.addField(AddFieldReq.builder()
.fieldName("varchar_field2")
.dataType(DataType.VarChar)
.maxLength(200)
.build());
schema.addField(AddFieldReq.builder()
.fieldName("pk")
.dataType(DataType.Int64)
.isPrimaryKey(true)
.build());
schema.addField(AddFieldReq.builder()
.fieldName("embedding")
.dataType(DataType.FloatVector)
.dimension(3)
.build());
import { MilvusClient, DataType } from "@zilliz/milvus2-sdk-node";
const schema = [
{
name: "metadata",
data_type: DataType.JSON,
},
{
name: "pk",
data_type: DataType.Int64,
is_primary_key: true,
},
{
name: "varchar_field2",
data_type: DataType.VarChar,
max_length: 200,
},
{
name: "varchar_field1",
data_type: DataType.VarChar,
max_length: 100,
},
];
export varcharField1='{
"fieldName": "varchar_field1",
"dataType": "VarChar",
"elementTypeParams": {
"max_length": 100
}
}'
export varcharField2='{
"fieldName": "varchar_field2",
"dataType": "VarChar",
"elementTypeParams": {
"max_length": 200
}
}'
export primaryField='{
"fieldName": "pk",
"dataType": "Int64",
"isPrimary": true
}'
export vectorField='{
"fieldName": "embedding",
"dataType": "FloatVector",
"elementTypeParams": {
"dim": 3
}
}'
export schema="{
\"autoID\": false,
\"fields\": [
$varcharField1,
$varcharField2,
$primaryField,
$vectorField
]
}"
以上示例中,我们添加了两个 VARCHAR
字段:varchar_field1
和 varchar_field2
,分别设置最大长度为 100 和 200 字节。建议您根据数据特征合理设置 max_length
,既要确保能容纳最长的数据,又不要过度分配空间。同时我们也添加了主键字段 pk
和向量字段 embedding
。
主键字段和向量字段在创建 Collection 时是必须添加的,主键字段用于唯一标识每条数据,而向量字段是进行相似性搜索的核心。有关具体信息,请参考主键与 AutoId、稀疏向量、稠密向量或 Binary 向量。
设置索引参数
为 VARCHAR 字段设置索引参数是一个可选操作,可以显著提高查询效率。
以下示例中,我们为 varchar_field1
创建了 AUTOINDEX
索引类型,表示 Zilliz Cloud 会自动根据数据类型创建合适的索引。有关更多信息,请参考 AUTOINDEX。
- Python
- Java
- NodeJS
- cURL
index_params = client.prepare_index_params()
index_params.add_index(
field_name="varchar_field1",
index_type="AUTOINDEX",
index_name="varchar_index"
)
import io.milvus.v2.common.IndexParam;
import java.util.*;
List<IndexParam> indexes = new ArrayList<>();
indexes.add(IndexParam.builder()
.fieldName("varchar_field1")
.indexName("varchar_index")
.indexType(IndexParam.IndexType.AUTOINDEX)
.build());
const indexParams = [{
index_name: 'varchar_index',
field_name: 'varchar_field1',
index_type: IndexType.AUTOINDEX,
)];
export indexParams='[
{
"fieldName": "varchar_field1",
"indexName": "varchar_index",
"indexType": "AUTOINDEX"
}
]'
在本例中,我们使用 AUTOINDEX
为 VarChar 字段创建索引。
- Python
- Java
- NodeJS
- cURL
# 添加向量索引
index_params.add_index(
field_name="embedding",
index_type="AUTOINDEX", # 使用自动索引方式,简化复杂的索引设置
metric_type="COSINE" # 指定相似度度量类型,可选择 L2、COSINE 或 IP
)
indexes.add(IndexParam.builder()
.fieldName("embedding")
.indexType(IndexParam.IndexType.AUTOINDEX)
.metricType(IndexParam.MetricType.COSINE)
.build());
indexParams.push({
index_name: 'embedding_index',
field_name: 'embedding',
metric_type: MetricType.COSINE,
index_type: IndexType.AUTOINDEX,
});
export indexParams='[
{
"fieldName": "varchar_field1",
"indexName": "varchar_index",
"indexType": "AUTOINDEX"
},
{
"fieldName": "embedding",
"metricType": "COSINE",
"indexType": "AUTOINDEX"
}
]'
创建 Collection
定义好 Schema 和索引后,我们便可以创建包含字符串字段的 Collection。
- Python
- Java
- NodeJS
- cURL
client.create_collection(
collection_name="my_varchar_collection",
schema=schema,
index_params=index_params
)
CreateCollectionReq requestCreate = CreateCollectionReq.builder()
.collectionName("my_varchar_collection")
.collectionSchema(schema)
.indexParams(indexes)
.build();
client.createCollection(requestCreate);
client.create_collection({
collection_name: "my_varchar_collection",
schema: schema,
index_params: index_params
})
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"my_varchar_collection\",
\"schema\": $schema,
\"indexParams\": $indexParams
}"
## {"code":0,"data":{}}
插入数据
Collection 创建完成后,可以插入包含字符串字段的数据。
- Python
- Java
- NodeJS
- cURL
data = [
{"varchar_field1": "Product A", "varchar_field2": "High quality product", "pk": 1, "embedding": [0.1, 0.2, 0.3]},
{"varchar_field1": "Product B", "varchar_field2": "Affordable price", "pk": 2, "embedding": [0.4, 0.5, 0.6]},
{"varchar_field1": "Product C", "varchar_field2": "Best seller", "pk": 3, "embedding": [0.7, 0.8, 0.9]},
]
client.insert(
collection_name="my_varchar_collection",
data=data
)
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.milvus.v2.service.vector.request.InsertReq;
import io.milvus.v2.service.vector.response.InsertResp;
List<JsonObject> rows = new ArrayList<>();
Gson gson = new Gson();
rows.add(gson.fromJson("{\"varchar_field1\": \"Product A\", \"varchar_field2\": \"High quality product\", \"pk\": 1, \"embedding\": [0.1, 0.2, 0.3]}", JsonObject.class));
rows.add(gson.fromJson("{\"varchar_field1\": \"Product B\", \"varchar_field2\": \"Affordable price\", \"pk\": 2, \"embedding\": [0.4, 0.5, 0.6]}", JsonObject.class));
rows.add(gson.fromJson("{\"varchar_field1\": \"Product C\", \"varchar_field2\": \"Best seller\", \"pk\": 3, \"embedding\": [0.7, 0.8, 0.9]}", JsonObject.class));
InsertResp insertR = client.insert(InsertReq.builder()
.collectionName("my_varchar_collection")
.data(rows)
.build());
const data = [
{
varchar_field1: "Product A",
varchar_field2: "High quality product",
pk: 1,
embedding: [0.1, 0.2, 0.3],
},
{
varchar_field1: "Product B",
varchar_field2: "Affordable price",
pk: 2,
embedding: [0.4, 0.5, 0.6],
},
{
varchar_field1: "Product C",
varchar_field2: "Best seller",
pk: 3,
embedding: [0.7, 0.8, 0.9],
},
];
client.insert({
collection_name: "my_sparse_collection",
data: data,
});
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/insert" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"data": [
{"varchar_field1": "Product A", "varchar_field2": "High quality product", "pk": 1, "embedding": [0.1, 0.2, 0.3]},
{"varchar_field1": "Product B", "varchar_field2": "Affordable price", "pk": 2, "embedding": [0.4, 0.5, 0.6]},
{"varchar_field1": "Product C", "varchar_field2": "Best seller", "pk": 3, "embedding": [0.7, 0.8, 0.9]}
],
"collectionName": "my_varchar_collection"
}'
## {"code":0,"cost":0,"data":{"insertCount":3,"insertIds":[1,2,3]}}
在这个例子中,我们插入了一些包含 VARCHAR 字段(varchar_field1
和 varchar_field2
)、主键(pk
)以及向量表示(embedding
)的数据。为了确保插入的数据与 Schema 中定义的字段匹配,建议提前检查数据类型,避免插入错误。
如果您在定义 Schema 时设置了 enable_dynamic_fields=True
,Zilliz Cloud 允许您插入未提前定义的字符串字段,但应注意这可能增加查询和管理的复杂性,影响性能。有关更多信息,请参考 Dynamic Field。
使用字符串字段进行查询
添加字符串字段后,您可以在搜索(Search)和查询(Query)中利用这些字段进行过滤,从而实现更精确的搜索结果。
过滤查询
添加字符串字段后,您可以在 Query 中利用这些字段进行过滤。例如,您可以查询 varchar_field1
为 "Product A" 的所有数据:
- Python
- Java
- NodeJS
- cURL
filter = 'varchar_field1 == "Product A"'
res = client.query(
collection_name="my_varchar_collection",
filter=filter,
output_fields=["varchar_field1", "varchar_field2"]
)
print(res)
# Output
# data: ["{'varchar_field1': 'Product A', 'varchar_field2': 'High quality product', 'pk': 1}"]
import io.milvus.v2.service.vector.request.QueryReq;
import io.milvus.v2.service.vector.response.QueryResp;
String filter = "varchar_field1 == \"Product A\"";
QueryResp resp = client.query(QueryReq.builder()
.collectionName("my_varchar_collection")
.filter(filter)
.outputFields(Arrays.asList("varchar_field1", "varchar_field2"))
.build());
System.out.println(resp.getQueryResults());
// Output
//
// [QueryResp.QueryResult(entity={varchar_field1=Product A, varchar_field2=High quality product, pk=1})]
client.query({
collection_name: 'my_varchar_collection',
filter: 'varchar_field1 == "Product A"',
output_fields: ['varchar_field1', 'varchar_field2']
});
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "my_varchar_collection",
"filter": "varchar_field1 == \"Product A\"",
"outputFields": ["varchar_field1", "varchar_field2"]
}'
## {"code":0,"cost":0,"data":[{"pk":1,"varchar_field1":"Product A","varchar_field2":"High quality product"}]}
此查询表达式将返回所有符合条件的数据,并输出其 varchar_field1
和 varchar_field2
字段。有关过滤查询的更多信息,请参考 Query 和 基本操作符。
向量搜索与字符串过滤结合
除了基本的标量字段过滤,您还可以将向量相似度搜索与标量字段过滤结合使用。例如,以下代码展示了如何在向量搜索中添加标量字段的过滤条件:
- Python
- Java
- NodeJS
- cURL
filter = 'varchar_field1 == "Product A"'
res = client.search(
collection_name="my_varchar_collection",
data=[[0.3, -0.6, 0.1]],
limit=5,
search_params={"params": {"nprobe": 10}},
output_fields=["varchar_field1", "varchar_field2"],
filter=filter
)
print(res)
# Output
# data: ["[{'id': 1, 'distance': -0.06000000238418579, 'entity': {'varchar_field1': 'Product A', 'varchar_field2': 'High quality product'}}]"]
import io.milvus.v2.service.vector.request.SearchReq;
import io.milvus.v2.service.vector.response.SearchResp;
String filter = "varchar_field1 == \"Product A\"";
SearchResp resp = client.search(SearchReq.builder()
.collectionName("my_varchar_collection")
.annsField("embedding")
.data(Collections.singletonList(new FloatVec(new float[]{0.3f, -0.6f, 0.1f})))
.topK(5)
.outputFields(Arrays.asList("varchar_field1", "varchar_field2"))
.filter(filter)
.build());
System.out.println(resp.getSearchResults());
// Output
//
// [[SearchResp.SearchResult(entity={varchar_field1=Product A, varchar_field2=High quality product}, score=-0.2364331, id=1)]]
client.search({
collection_name: 'my_varchar_collection',
data: [0.3, -0.6, 0.1],
limit: 5,
output_fields: ['varchar_field1', 'varchar_field2'],
filter: 'varchar_field1 == "Product A"'
params: {
nprobe:10
}
});
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/search" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "my_varchar_collection",
"data": [
[0.3, -0.6, 0.1]
],
"limit": 5,
"searchParams":{
"params":{"nprobe":10}
},
"outputFields": ["varchar_field1", "varchar_field2"],
"filter": "varchar_field1 == \"Product A\""
}'
## {"code":0,"cost":0,"data":[{"distance":-0.2364331,"id":1,"varchar_field1":"Product A","varchar_field2":"High quality product"}]}
在这个例子中,我们首先定义了查询向量,并在搜索时添加了过滤条件 varchar_field1 == "Product A"
,这样可以确保搜索结果不仅与查询向量相似,还符合指定的字符串匹配条件。有关更多信息,请参考 Filtered Search 和 基本操作符。