VarChar Field
Zilliz Cloud supports textual scalar data with VARCHAR and TEXT fields. This page describes VARCHAR, which is designed for short, bounded string metadata such as names, tags, categories, and external IDs.
For longer source text, document passages, article bodies, tickets, or logs that should be stored and returned with entities, use a TEXT field instead. Use TEXT when a value may exceed 65,535 bytes or when you do not want to set a fixed max_length in the collection schema. For details, refer to Text Field.
When you define a VARCHAR field, two parameters are mandatory:
-
Set the
datatypetoDataType.VARCHAR. -
Specify the
max_length, which defines the maximum number of UTF-8 bytes allowed for each field value. The valid range formax_lengthis from 1 to 65,535.
The number of bytes can differ from the number of characters. For example, with max_length=10, "abcdefghij" fits within the limit because it uses 10 UTF-8 bytes. The string "一二三四五六七八九十" also has 10 characters, but uses 30 UTF-8 bytes and exceeds the limit.
Zilliz Cloud supports NULL values and default values for VARCHAR fields. To enable these features, enable nullable and set default_value to a string value. For details, refer to Nullable & Default.
Add VARCHAR field
To store short, bounded string metadata in Zilliz Cloud, define a VARCHAR field in your collection schema. Below is an example of defining a collection schema with two VARCHAR fields:
-
varchar_field1: stores up to 100 bytes, allows NULL values, and has a default value of"Unknown". -
varchar_field2: stores up to 200 bytes, allows NULL values, but does not have a default value.
If you enable enable_dynamic_field when defining the schema, Zilliz Cloud allows you to insert scalar fields 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.
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
# 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, token="YOUR_CLUSTER_TOKEN")
# Define the collection schema
schema = client.create_schema(
auto_id=False,
enable_dynamic_field=True,
)
# Add `varchar_field1` that supports NULL values with default value "Unknown"
schema.add_field(field_name="varchar_field1", datatype=DataType.VARCHAR, max_length=100, nullable=True, default_value="Unknown")
# Add `varchar_field2` that supports NULL values without default value
schema.add_field(field_name="varchar_field2", datatype=DataType.VARCHAR, max_length=200, 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)
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")
.token("YOUR_CLUSTER_TOKEN")
.build());
CreateCollectionReq.CollectionSchema schema = client.createSchema();
schema.setEnableDynamicField(true);
schema.addField(AddFieldReq.builder()
.fieldName("varchar_field1")
.dataType(DataType.VarChar)
.maxLength(100)
.isNullable(true)
.defaultValue("Unknown")
.build());
schema.addField(AddFieldReq.builder()
.fieldName("varchar_field2")
.dataType(DataType.VarChar)
.maxLength(200)
.isNullable(true)
.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 (
"context"
"fmt"
"github.com/milvus-io/milvus/client/v3/column"
"github.com/milvus-io/milvus/client/v3/entity"
"github.com/milvus-io/milvus/client/v3/index"
"github.com/milvus-io/milvus/client/v3/milvusclient"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "YOUR_CLUSTER_ENDPOINT"
client, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
APIKey: "YOUR_CLUSTER_TOKEN",
})
if err != nil {
fmt.Println(err.Error())
// handle error
}
defer client.Close(ctx)
schema := entity.NewSchema()
schema.WithField(entity.NewField().
WithName("pk").
WithDataType(entity.FieldTypeInt64).
WithIsPrimaryKey(true),
).WithField(entity.NewField().
WithName("embedding").
WithDataType(entity.FieldTypeFloatVector).
WithDim(3),
).WithField(entity.NewField().
WithName("varchar_field1").
WithDataType(entity.FieldTypeVarChar).
WithMaxLength(100).
WithNullable(true).
WithDefaultValueString("Unknown"),
).WithField(entity.NewField().
WithName("varchar_field2").
WithDataType(entity.FieldTypeVarChar).
WithMaxLength(200).
WithNullable(true),
)
use milvus::v2::prelude::*;
let client = ClientV2::new(
&ConnectConfig::new()
.uri("YOUR_CLUSTER_ENDPOINT")
.token("YOUR_CLUSTER_TOKEN"),
)
.await?;
let schema = CollectionSchema::new()
.add_field(
FieldSchema::new()
.name("varchar_field1")
.data_type(DataType::VarChar)
.max_length(100)
.nullable(true)
.default_value(DefaultValue::String("Unknown".to_string())),
)
.add_field(
FieldSchema::new()
.name("varchar_field2")
.data_type(DataType::VarChar)
.max_length(200)
.nullable(true),
)
.add_field(
FieldSchema::new()
.name("pk")
.data_type(DataType::Int64)
.primary_key(true),
)
.add_field(
FieldSchema::new()
.name("embedding")
.data_type(DataType::FloatVector)
.dimension(3),
);
#include <iostream>
#include <memory>
#include "milvus/MilvusClientV2.h"
auto client = milvus::MilvusClientV2::Create();
milvus::ConnectParam connect_param{"YOUR_CLUSTER_ENDPOINT", "YOUR_CLUSTER_TOKEN"};
auto status = client->Connect(connect_param);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
milvus::CollectionSchemaPtr schema = std::make_shared<milvus::CollectionSchema>();
schema->AddField({"pk", milvus::DataType::INT64, "", true, false});
schema->AddField(milvus::FieldSchema("embedding", milvus::DataType::FLOAT_VECTOR).WithDimension(3));
schema->AddField(milvus::FieldSchema("varchar_field1", milvus::DataType::VARCHAR).WithMaxLength(100).WithNullable(true).WithDefaultValue("Unknown"));
schema->AddField(milvus::FieldSchema("varchar_field2", milvus::DataType::VARCHAR).WithMaxLength(200).WithNullable(true));
import { MilvusClient, DataType } from "@zilliz/milvus2-sdk-node";
const client = new MilvusClient({
address: "YOUR_CLUSTER_ENDPOINT",
token: "YOUR_CLUSTER_TOKEN"
});
const schema = [
{
name: "varchar_field1",
data_type: DataType.VarChar,
max_length: 100,
nullable: true,
default_value: "Unknown",
},
{
name: "varchar_field2",
data_type: DataType.VarChar,
max_length: 200,
nullable: true,
},
{
name: "pk",
data_type: DataType.Int64,
is_primary_key: true,
},
{
name: "embedding",
data_type: DataType.FloatVector,
dim: 3,
},
];
export varcharField1='{
"fieldName": "varchar_field1",
"dataType": "VarChar",
"elementTypeParams": {
"max_length": 100
},
"nullable": true,
"defaultValue": "Unknown"
}'
export varcharField2='{
"fieldName": "varchar_field2",
"dataType": "VarChar",
"elementTypeParams": {
"max_length": 200
},
"nullable": true
}'
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
]
}"
Set index params
Indexing helps improve search and query performance. In Zilliz Cloud, indexing is mandatory for vector fields but optional for scalar fields.
The following example creates indexes on the vector field embedding and the scalar field varchar_field1, both using the AUTOINDEX index type. With this type, Zilliz Cloud automatically selects the most suitable index based on the data type. You can also customize the index type and params for each field. For details, refer to Index Explained.
You can also build an NGRAM index to accelerate LIKE filtering on VARCHAR fields. For details, refer to NGRAM.
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
# Set index params
index_params = client.prepare_index_params()
# Index `varchar_field1` with AUTOINDEX
index_params.add_index(
field_name="varchar_field1",
index_type="AUTOINDEX",
index_name="varchar_index"
)
# Index `embedding` with AUTOINDEX and specify 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
)
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());
indexes.add(IndexParam.builder()
.fieldName("embedding")
.indexType(IndexParam.IndexType.AUTOINDEX)
.metricType(IndexParam.MetricType.COSINE)
.build());
indexOption1 := milvusclient.NewCreateIndexOption("my_collection", "embedding",
index.NewAutoIndex(index.MetricType(entity.COSINE)))
indexOption2 := milvusclient.NewCreateIndexOption("my_collection", "varchar_field1",
index.NewInvertedIndex())
use milvus::v2::prelude::*;
let indexes = vec![
IndexParam::new()
.field_name("varchar_field1")
.index_name("varchar_index")
.index_type(IndexType::AutoIndex),
IndexParam::new()
.field_name("embedding")
.index_type(IndexType::AutoIndex)
.metric_type(MetricType::Cosine),
];
std::vector<milvus::IndexDesc> indexes = {
milvus::IndexDesc("varchar_field1", "varchar_index", milvus::IndexType::AUTOINDEX),
milvus::IndexDesc("embedding", "", milvus::IndexType::AUTOINDEX, milvus::MetricType::COSINE)
};
import { IndexType, MetricType } from "@zilliz/milvus2-sdk-node";
const indexParams = [{
index_name: 'varchar_index',
field_name: 'varchar_field1',
index_type: IndexType.AUTOINDEX,
}];
indexParams.push({
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"
}
]'
Create collection
Once the schema and index are defined, create a collection that includes string fields.
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
# Create Collection
client.create_collection(
collection_name="my_collection",
schema=schema,
index_params=index_params
)
CreateCollectionReq requestCreate = CreateCollectionReq.builder()
.collectionName("my_collection")
.collectionSchema(schema)
.indexParams(indexes)
.build();
client.createCollection(requestCreate);
err = client.CreateCollection(ctx,
milvusclient.NewCreateCollectionOption("my_collection", schema).
WithIndexOptions(indexOption1, indexOption2))
if err != nil {
fmt.Println(err.Error())
// handle error
}
_, err = client.LoadCollection(ctx, milvusclient.NewLoadCollectionOption("my_collection"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
use milvus::v2::prelude::*;
client
.create_collection(
CreateCollectionRequest::builder()
.collection_name("my_collection")
.schema(schema)
.index_params(indexes)
.build()?,
)
.await?;
auto status = client->CreateCollection(milvus::CreateCollectionRequest()
.WithCollectionName("my_collection")
.WithIndexes(std::move(indexes))
.WithCollectionSchema(schema));
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
await client.createCollection({
collection_name: "my_collection",
schema: schema,
index_params: indexParams
});
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d "{
\"collectionName\": \"my_collection\",
\"schema\": $schema,
\"indexParams\": $indexParams
}"
## {"code":0,"data":{}}
Insert data
After creating the collection, insert entities that match the schema.
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
# Sample data
data = [
{"varchar_field1": "Product A", "varchar_field2": "High quality product", "pk": 1, "embedding": [0.1, 0.2, 0.3]},
{"varchar_field1": "Product B", "pk": 2, "embedding": [0.4, 0.5, 0.6]}, # varchar_field2 field is missing, which should be NULL
{"varchar_field1": None, "varchar_field2": None, "pk": 3, "embedding": [0.2, 0.3, 0.1]}, # `varchar_field1` should default to `Unknown`, `varchar_field2` is NULL
{"varchar_field1": "Product C", "varchar_field2": None, "pk": 4, "embedding": [0.5, 0.7, 0.2]}, # `varchar_field2` is NULL
{"varchar_field1": None, "varchar_field2": "Exclusive deal", "pk": 5, "embedding": [0.6, 0.4, 0.8]}, # `varchar_field1` should default to `Unknown`
{"varchar_field1": "Unknown", "varchar_field2": None, "pk": 6, "embedding": [0.8, 0.5, 0.3]}, # `varchar_field2` is NULL
{"varchar_field1": "", "varchar_field2": "Best seller", "pk": 7, "embedding": [0.8, 0.5, 0.3]}, # Empty string is not treated as NULL
]
# Insert data
client.insert(
collection_name="my_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\", \"pk\": 2, \"embedding\": [0.4, 0.5, 0.6]}", JsonObject.class));
rows.add(gson.fromJson("{\"varchar_field1\": null, \"varchar_field2\": null, \"pk\": 3, \"embedding\": [0.2, 0.3, 0.1]}", JsonObject.class));
rows.add(gson.fromJson("{\"varchar_field1\": \"Product C\", \"varchar_field2\": null, \"pk\": 4, \"embedding\": [0.5, 0.7, 0.2]}", JsonObject.class));
rows.add(gson.fromJson("{\"varchar_field1\": null, \"varchar_field2\": \"Exclusive deal\", \"pk\": 5, \"embedding\": [0.6, 0.4, 0.8]}", JsonObject.class));
rows.add(gson.fromJson("{\"varchar_field1\": \"Unknown\", \"varchar_field2\": null, \"pk\": 6, \"embedding\": [0.8, 0.5, 0.3]}", JsonObject.class));
rows.add(gson.fromJson("{\"varchar_field1\": \"\", \"varchar_field2\": \"Best seller\", \"pk\": 7, \"embedding\": [0.8, 0.5, 0.3]}", JsonObject.class));
InsertResp insertR = client.insert(InsertReq.builder()
.collectionName("my_collection")
.data(rows)
.build());
column1, _ := column.NewNullableColumnVarChar("varchar_field1",
[]string{"Product A", "Product B", "Product C", "Unknown", ""},
[]bool{true, true, false, true, false, true, true})
column2, _ := column.NewNullableColumnVarChar("varchar_field2",
[]string{"High quality product", "Exclusive deal", "Best seller"},
[]bool{true, false, false, false, true, false, true})
_, err = client.Insert(ctx, milvusclient.NewColumnBasedInsertOption("my_collection").
WithInt64Column("pk", []int64{1, 2, 3, 4, 5, 6, 7}).
WithFloatVectorColumn("embedding", 3, [][]float32{
{0.1, 0.2, 0.3},
{0.4, 0.5, 0.6},
{0.2, 0.3, 0.1},
{0.5, 0.7, 0.2},
{0.6, 0.4, 0.8},
{0.8, 0.5, 0.3},
{0.8, 0.5, 0.3},
}).
WithColumns(column1, column2),
)
if err != nil {
fmt.Println(err.Error())
// handle err
}
use milvus::v2::prelude::*;
client
.insert(
InsertRequest::builder()
.collection_name("my_collection")
.columns(vec![
FieldData::nullable(
FieldData::varchar(
"varchar_field1",
vec![
"Product A".to_string(),
"Product B".to_string(),
"Product C".to_string(),
"Unknown".to_string(),
"".to_string(),
],
),
vec![true, true, false, true, false, true, true],
)?,
FieldData::nullable(
FieldData::varchar(
"varchar_field2",
vec![
"High quality product".to_string(),
"Exclusive deal".to_string(),
"Best seller".to_string(),
],
),
vec![true, false, false, false, true, false, true],
)?,
FieldData::int64("pk", vec![1, 2, 3, 4, 5, 6, 7]),
FieldData::float_vector(
"embedding",
vec![
vec![0.1, 0.2, 0.3],
vec![0.4, 0.5, 0.6],
vec![0.2, 0.3, 0.1],
vec![0.5, 0.7, 0.2],
vec![0.6, 0.4, 0.8],
vec![0.8, 0.5, 0.3],
vec![0.8, 0.5, 0.3],
],
),
])
.build()?,
)
.await?;
milvus::EntityRows data = {{{"varchar_field1", "Product A"}, {"varchar_field2", "High quality product"}, {"pk", 1}, {"embedding", std::vector<float>{0.1, 0.2, 0.3}}},
{{"varchar_field1", "Product B"}, {"pk", 2}, {"embedding", std::vector<float>{0.4, 0.5, 0.6}}},
{{"varchar_field1", nullptr}, {"varchar_field2", nullptr}, {"pk", 3}, {"embedding", std::vector<float>{0.2, 0.3, 0.1}}},
{{"varchar_field1", "Product C"}, {"varchar_field2", nullptr}, {"pk", 4}, {"embedding", std::vector<float>{0.5, 0.7, 0.2}}},
{{"varchar_field1", nullptr}, {"varchar_field2", "Exclusive deal"}, {"pk", 5}, {"embedding", std::vector<float>{0.6, 0.4, 0.8}}},
{{"varchar_field1", "Unknown"}, {"varchar_field2", nullptr}, {"pk", 6}, {"embedding", std::vector<float>{0.8, 0.5, 0.3}}},
{{"varchar_field1", ""}, {"varchar_field2", "Best seller"}, {"pk", 7}, {"embedding", std::vector<float>{0.8, 0.5, 0.3}}}};
milvus::InsertResponse response;
auto status = client->Insert(milvus::InsertRequest()
.WithCollectionName("my_collection")
.WithRowsData(std::move(data)),
response);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
const data = [
{
varchar_field1: "Product A",
varchar_field2: "High quality product",
pk: 1,
embedding: [0.1, 0.2, 0.3],
},
{
varchar_field1: "Product B",
pk: 2,
embedding: [0.4, 0.5, 0.6],
},
{
varchar_field1: null,
varchar_field2: null,
pk: 3,
embedding: [0.2, 0.3, 0.1],
},
{
varchar_field1: "Product C",
varchar_field2: null,
pk: 4,
embedding: [0.5, 0.7, 0.2],
},
{
varchar_field1: null,
varchar_field2: "Exclusive deal",
pk: 5,
embedding: [0.6, 0.4, 0.8],
},
{
varchar_field1: "Unknown",
varchar_field2: null,
pk: 6,
embedding: [0.8, 0.5, 0.3],
},
{
varchar_field1: "",
varchar_field2: "Best seller",
pk: 7,
embedding: [0.8, 0.5, 0.3],
},
];
await client.insert({
collection_name: "my_collection",
data: data,
});
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/insert" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
--data '{
"data": [
{"varchar_field1": "Product A", "varchar_field2": "High quality product", "pk": 1, "embedding": [0.1, 0.2, 0.3]},
{"varchar_field1": "Product B", "pk": 2, "embedding": [0.4, 0.5, 0.6]},
{"varchar_field1": null, "varchar_field2": null, "pk": 3, "embedding": [0.2, 0.3, 0.1]},
{"varchar_field1": "Product C", "varchar_field2": null, "pk": 4, "embedding": [0.5, 0.7, 0.2]},
{"varchar_field1": null, "varchar_field2": "Exclusive deal", "pk": 5, "embedding": [0.6, 0.4, 0.8]},
{"varchar_field1": "Unknown", "varchar_field2": null, "pk": 6, "embedding": [0.8, 0.5, 0.3]},
{"varchar_field1": "", "varchar_field2": "Best seller", "pk": 7, "embedding": [0.8, 0.5, 0.3]}
],
"collectionName": "my_collection"
}'
## {"code":0,"cost":0,"data":{"insertCount":7,"insertIds":[1,2,3,4,5,6,7]}}
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 varchar_field1 matches the string "Product A":
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
# Filter `varchar_field1` with value "Product A"
filter = 'varchar_field1 == "Product A"'
res = client.query(
collection_name="my_collection",
filter=filter,
output_fields=["varchar_field1", "varchar_field2"]
)
print(res)
# Example 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_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})]
filter := "varchar_field1 == \"Product A\""
queryResult, err := client.Query(ctx, milvusclient.NewQueryOption("my_collection").
WithFilter(filter).
WithOutputFields("varchar_field1", "varchar_field2"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println("varchar_field1", queryResult.GetColumn("varchar_field1").FieldData().GetScalars())
fmt.Println("varchar_field2", queryResult.GetColumn("varchar_field2").FieldData().GetScalars())
// Output
//
// varchar_field1 string_data:{data:"Product A"}
// varchar_field2 string_data:{data:"High quality product"}
use milvus::v2::prelude::*;
let res = client
.query(
QueryRequest::builder()
.collection_name("my_collection")
.filter(r#"varchar_field1 == "Product A""#)
.output_fields(["varchar_field1", "varchar_field2"])
.build()?,
)
.await?;
println!("{:?}", res.results());
auto request = milvus::QueryRequest()
.WithCollectionName("my_collection")
.WithFilter(R"(varchar_field1 == "Product A")")
.AddOutputField("varchar_field1")
.AddOutputField("varchar_field2");
milvus::QueryResponse response;
auto status = client->Query(request, response);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
milvus::EntityRows output_rows;
status = response.Results().OutputRows(output_rows);
for (const auto& row : output_rows) {
std::cout << "\t" << row << std::endl;
}
await client.query({
collection_name: 'my_collection',
filter: 'varchar_field1 == "Product A"',
output_fields: ['varchar_field1', 'varchar_field2']
});
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_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"}]}
To retrieve entities where the varchar_field2 is NULL:
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
# Filter entities where `varchar_field2` is null
filter = 'varchar_field2 is null'
res = client.query(
collection_name="my_collection",
filter=filter,
output_fields=["varchar_field1", "varchar_field2"]
)
print(res)
# Example output:
# data: [
# "{'varchar_field1': 'Product B', 'varchar_field2': None, 'pk': 2}",
# "{'varchar_field1': 'Unknown', 'varchar_field2': None, 'pk': 3}",
# "{'varchar_field1': 'Product C', 'varchar_field2': None, 'pk': 4}",
# "{'varchar_field1': 'Unknown', 'varchar_field2': None, 'pk': 6}"
# ]
String filter = "varchar_field2 is null";
QueryResp resp = client.query(QueryReq.builder()
.collectionName("my_collection")
.filter(filter)
.outputFields(Arrays.asList("varchar_field1", "varchar_field2"))
.build());
System.out.println(resp.getQueryResults());
// Output
//
// [
// QueryResp.QueryResult(entity={varchar_field1=Product B, varchar_field2=null, pk=2}),
// QueryResp.QueryResult(entity={varchar_field1=Unknown, varchar_field2=null, pk=3}),
// QueryResp.QueryResult(entity={varchar_field1=Product C, varchar_field2=null, pk=4}),
// QueryResp.QueryResult(entity={varchar_field1=Unknown, varchar_field2=null, pk=6})
// ]
filter = "varchar_field2 is null"
queryResult, err = client.Query(ctx, milvusclient.NewQueryOption("my_collection").
WithFilter(filter).
WithOutputFields("varchar_field1", "varchar_field2"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println("varchar_field1", queryResult.GetColumn("varchar_field1"))
fmt.Println("varchar_field2", queryResult.GetColumn("varchar_field2"))
use milvus::v2::prelude::*;
let res = client
.query(
QueryRequest::builder()
.collection_name("my_collection")
.filter("varchar_field2 is null")
.output_fields(["varchar_field1", "varchar_field2"])
.build()?,
)
.await?;
println!("{:?}", res.results());
auto request = milvus::QueryRequest()
.WithCollectionName("my_collection")
.WithFilter("varchar_field2 IS NULL")
.AddOutputField("varchar_field1")
.AddOutputField("varchar_field2");
milvus::QueryResponse response;
auto status = client->Query(request, response);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
milvus::EntityRows output_rows;
status = response.Results().OutputRows(output_rows);
for (const auto& row : output_rows) {
std::cout << "\t" << row << std::endl;
}
await client.query({
collection_name: 'my_collection',
filter: 'varchar_field2 is null',
output_fields: ['varchar_field1', 'varchar_field2']
});
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
# restful
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"filter": "varchar_field2 is null",
"outputFields": ["varchar_field1", "varchar_field2"]
}'
To retrieve entities where varchar_field1 has the value "Unknown", use the following expression below. As the default value of varchar_field1 is "Unknown", the expected result should include entities with varchar_field1 explicitly set to "Unknown" or with varchar_field1 set to NULL.
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
# Filter entities with `varchar_field1` with value `Unknown`
filter = 'varchar_field1 == "Unknown"'
res = client.query(
collection_name="my_collection",
filter=filter,
output_fields=["varchar_field1", "varchar_field2"]
)
print(res)
# Example output:
# data: [
# "{'varchar_field1': 'Unknown', 'varchar_field2': None, 'pk': 3}",
# "{'varchar_field1': 'Unknown', 'varchar_field2': 'Exclusive deal', 'pk': 5}",
# "{'varchar_field1': 'Unknown', 'varchar_field2': None, 'pk': 6}"
# ]
String filter = "varchar_field1 == \"Unknown\"";
QueryResp resp = client.query(QueryReq.builder()
.collectionName("my_collection")
.filter(filter)
.outputFields(Arrays.asList("varchar_field1", "varchar_field2"))
.build());
System.out.println(resp.getQueryResults());
// Output
//
// [
// QueryResp.QueryResult(entity={varchar_field1=Unknown, varchar_field2=null, pk=3}),
// QueryResp.QueryResult(entity={varchar_field1=Unknown, varchar_field2=Exclusive deal, pk=5}),
// QueryResp.QueryResult(entity={varchar_field1=Unknown, varchar_field2=null, pk=6})
// ]
filter = "varchar_field1 == \"Unknown\""
queryResult, err = client.Query(ctx, milvusclient.NewQueryOption("my_collection").
WithFilter(filter).
WithOutputFields("varchar_field1", "varchar_field2"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println("varchar_field1", queryResult.GetColumn("varchar_field1"))
fmt.Println("varchar_field2", queryResult.GetColumn("varchar_field2"))
use milvus::v2::prelude::*;
let res = client
.query(
QueryRequest::builder()
.collection_name("my_collection")
.filter(r#"varchar_field1 == "Unknown""#)
.output_fields(["varchar_field1", "varchar_field2"])
.build()?,
)
.await?;
println!("{:?}", res.results());
auto request = milvus::QueryRequest()
.WithCollectionName("my_collection")
.WithFilter(R"(varchar_field1 == "Unknown")")
.AddOutputField("varchar_field1")
.AddOutputField("varchar_field2");
milvus::QueryResponse response;
auto status = client->Query(request, response);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
milvus::EntityRows output_rows;
status = response.Results().OutputRows(output_rows);
for (const auto& row : output_rows) {
std::cout << "\t" << row << std::endl;
}
// node
await client.query({
collection_name: 'my_collection',
filter: 'varchar_field1 == "Unknown"',
output_fields: ['varchar_field1', 'varchar_field2']
});
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
# restful
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"filter": "varchar_field1 == \"Unknown\"",
"outputFields": ["varchar_field1", "varchar_field2"]
}'
Vector search with filter expressions
In addition to basic scalar field filtering, you can combine vector similarity searches with scalar field filters. For example, the following code shows how to add a scalar field filter to a vector search:
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
# Search with string filtering
# Filter `varchar_field2` with value "Best seller"
filter = 'varchar_field2 == "Best seller"'
res = client.search(
collection_name="my_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)
# Example output:
# data: [
# "[{'id': 7, 'distance': -0.04468163847923279, 'entity': {'varchar_field1': '', 'varchar_field2': 'Best seller'}}]"
# ]
import io.milvus.v2.service.vector.request.data.FloatVec;
import io.milvus.v2.service.vector.request.SearchReq;
import io.milvus.v2.service.vector.response.SearchResp;
String filter = "varchar_field2 == \"Best seller\"";
SearchResp resp = client.search(SearchReq.builder()
.collectionName("my_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=, varchar_field2=Best seller}, score=-0.04468164, id=7)]]
queryVector := []float32{0.3, -0.6, 0.1}
filter = "varchar_field2 == \"Best seller\""
annParam := index.NewCustomAnnParam()
annParam.WithExtraParam("nprobe", 10)
resultSets, err := client.Search(ctx, milvusclient.NewSearchOption(
"my_collection", // collectionName
5, // limit
[]entity.Vector{entity.FloatVector(queryVector)},
).WithANNSField("embedding").
WithFilter(filter).
WithAnnParam(annParam).
WithOutputFields("varchar_field1", "varchar_field2"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
for _, resultSet := range resultSets {
fmt.Println("IDs: ", resultSet.IDs.FieldData().GetScalars())
fmt.Println("Scores: ", resultSet.Scores)
fmt.Println("varchar_field1: ", resultSet.GetColumn("varchar_field1"))
fmt.Println("varchar_field2: ", resultSet.GetColumn("varchar_field2"))
}
use milvus::v2::prelude::*;
let res = client
.search(
SearchRequest::builder()
.collection_name("my_collection")
.vector_field("embedding")
.vectors(SearchVectors::Float(vec![vec![0.3, -0.6, 0.1]]))
.filter(r#"varchar_field2 == "Best seller""#)
.output_fields(["varchar_field1", "varchar_field2"])
.limit(5)
.build()?,
)
.await?;
println!("{:?}", res.results());
std::vector<float> query_vector = {0.3, -0.6, 0.1};
auto request = milvus::SearchRequest()
.WithCollectionName("my_collection")
.WithAnnsField("embedding")
.WithFilter(R"(varchar_field2 == "Best seller")")
.WithLimit(5)
.AddExtraParam("nprobe", "10")
.AddOutputField("varchar_field1")
.AddOutputField("varchar_field2")
.AddFloatVector(query_vector);
milvus::SearchResponse response;
auto status = client->Search(request, response);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
auto search_results = response.Results();
for (auto& result : search_results.Results()) {
milvus::EntityRows output_rows;
status = result.OutputRows(output_rows);
for (const auto& row : output_rows) {
std::cout << "\t" << row << std::endl;
}
}
await client.search({
collection_name: 'my_collection',
data: [0.3, -0.6, 0.1],
limit: 5,
output_fields: ['varchar_field1', 'varchar_field2'],
filter: 'varchar_field2 == "Best seller"',
params: {
nprobe: 10
}
});
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/search" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"data": [
[0.3, -0.6, 0.1]
],
"limit": 5,
"searchParams":{
"params":{"nprobe":10}
},
"outputFields": ["varchar_field1", "varchar_field2"],
"filter": "varchar_field2 == \"Best seller\""
}'
## {"code":0,"cost":0,"data":[{"distance":-0.044681635,"pk":7,"varchar_field1":"","varchar_field2":"Best seller"}]}