Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmake/MilvusProtoGen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

include_guard(GLOBAL)

set(PROTO_COMMIT c39cddab3fac51c1a1228136ce5e95b131bd2ce6)
set(PROTO_COMMIT ae7fea6ab2f4e958f2feef0f0edb9a0d23fa7e0c)

include(FetchContent)

Expand Down
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Legacy examples for MilvusClient (maintenance mode):
- `./cmake_build/examples/v1/sdk_vector_sparse_v1`: example to show the usage of SparseVector field.

Recommended examples for MilvusClientV2:
- `./cmake_build/examples/v2/sdk_alias_v2`: example to show the usage of aliases, including createAlias(), listAliases(), describeAlias(), alterAlias() and dropAlias().
- `./cmake_build/examples/v2/sdk_array_v2`: example to show the usage of Array field.
- `./cmake_build/examples/v2/sdk_cdc_v2`: example to show the usage of CDC interfaces including DumpMessages().
- `./cmake_build/examples/v2/sdk_db_v2`: example to show the usage of databases.
Expand All @@ -54,6 +55,7 @@ Recommended examples for MilvusClientV2:
- `./cmake_build/examples/v2/sdk_geometry_field_v2`: a general example to show the usage of Geometry field.
- `./cmake_build/examples/v2/sdk_group_by_v2`: a general example to show the usage of grouping search.
- `./cmake_build/examples/v2/sdk_hybrid_search_v2`: example to show the usage of hybrid search interface.
- `./cmake_build/examples/v2/sdk_index_v2`: example to show the usage of index interfaces, including createIndex(), listIndexes(), describeIndex(), alterIndexProperties(), dropIndexProperties() and dropIndex().
- `./cmake_build/examples/v2/sdk_iterator_query_v2`: example to show the usage of query iterator.
- `./cmake_build/examples/v2/sdk_iterator_search_v2`: example to show the usage of search iterator.
- `./cmake_build/examples/v2/sdk_json_v2`: example to show the usage of JSON field.
Expand All @@ -64,6 +66,7 @@ Recommended examples for MilvusClientV2:
- `./cmake_build/examples/v2/sdk_optimize_v2`: example to show the usage of Optimize().
- `./cmake_build/examples/v2/sdk_order_by_v2`: example to show the usage of ORDER BY for search and query.
- `./cmake_build/examples/v2/sdk_partition_key_v2`: example to show the usage of partition key.
- `./cmake_build/examples/v2/sdk_partition_v2`: example to show the usage of partitions, including createPartition(), hasPartition(), loadPartitions(), GetPartitionStatistics(), releasePartitions() and dropPartition().
- `./cmake_build/examples/v2/sdk_rbac_v2`: example to show the usage of RBAC.
- `./cmake_build/examples/v2/sdk_rerank_function_v2`: example to show the usage of rerank function.
- `./cmake_build/examples/v2/sdk_run_analyzer_v2`: example to show the usage of run_analyzer().
Expand Down
11 changes: 11 additions & 0 deletions examples/src/v2/add_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ main(int argc, char* argv[]) {
QueryById(client, 100);
}

{
// alter the max length of the added field
status = client->AlterCollectionFieldProperties(milvus::AlterCollectionFieldPropertiesRequest()
.WithCollectionName(collection_name)
.WithFieldName(field_text)
.AddProperty("max_length", "200"));
util::CheckStatus("alter field 'text' max_length", status);
std::cout << "Field 'text' max_length altered to 200" << std::endl;
DescribeCollection(client);
}

{
const std::string text_value = "this is a new row";
InsertRow(client, 500, &text_value);
Expand Down
120 changes: 120 additions & 0 deletions examples/src/v2/alias.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Licensed to the LF AI & Data foundation under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <iostream>
#include <memory>
#include <string>
#include <vector>

#include "ExampleUtils.h"
#include "milvus/MilvusClientV2.h"

namespace {
const std::string collection_name = "CPP_V2_ALIAS";
const std::string second_collection_name = collection_name + "_2";
const std::string alias_name = "cpp_v2_alias";
const std::string field_id = "id";
const std::string field_vector = "vector";
const uint32_t dimension = 4;

void
CreateCollection(milvus::MilvusClientV2Ptr& client, const std::string& name) {
milvus::CollectionSchemaPtr collection_schema = std::make_shared<milvus::CollectionSchema>();
collection_schema->AddField({field_id, milvus::DataType::INT64, "", true, true});
collection_schema->AddField(
milvus::FieldSchema(field_vector, milvus::DataType::FLOAT_VECTOR).WithDimension(dimension));

// drop a leftover collection from a previous run
client->DropCollection(milvus::DropCollectionRequest().WithCollectionName(name));

auto status = client->CreateCollection(
milvus::CreateCollectionRequest().WithCollectionName(name).WithCollectionSchema(collection_schema));
util::CheckStatus("create collection: " + name, status);
}

void
CreateAlias(milvus::MilvusClientV2Ptr& client) {
auto status =
client->CreateAlias(milvus::CreateAliasRequest().WithCollectionName(collection_name).WithAlias(alias_name));
util::CheckStatus("create alias: " + alias_name, status);
}

void
ListAliases(milvus::MilvusClientV2Ptr& client) {
milvus::ListAliasesResponse response;
auto status = client->ListAliases(milvus::ListAliasesRequest().WithCollectionName(collection_name), response);
util::CheckStatus("list aliases of: " + collection_name, status);
std::cout << "Aliases of " << collection_name << ": ";
util::PrintList(response.Aliases());
}

void
DescribeAlias(milvus::MilvusClientV2Ptr& client) {
milvus::DescribeAliasResponse response;
auto status = client->DescribeAlias(milvus::DescribeAliasRequest().WithAlias(alias_name), response);
util::CheckStatus("describe alias: " + alias_name, status);
std::cout << "Alias '" << response.Desc().Name() << "' -> collection '" << response.Desc().CollectionName()
<< "' in database '" << response.Desc().DatabaseName() << "'" << std::endl;
}

void
AlterAlias(milvus::MilvusClientV2Ptr& client) {
// create a second collection and repoint the alias to it
CreateCollection(client, second_collection_name);
auto status = client->AlterAlias(
milvus::AlterAliasRequest().WithCollectionName(second_collection_name).WithAlias(alias_name));
util::CheckStatus("alter alias: " + alias_name, status);
}

void
DropAlias(milvus::MilvusClientV2Ptr& client) {
auto status = client->DropAlias(milvus::DropAliasRequest().WithAlias(alias_name));
util::CheckStatus("drop alias: " + alias_name, status);
}

} // namespace

int
main(int argc, char* argv[]) {
printf("Example start...\n");

auto client = milvus::MilvusClientV2::Create();

milvus::ConnectParam connect_param{"http://localhost:19530", "root:Milvus"};
auto status = client->Connect(connect_param);
util::CheckStatus("connect milvus server", status);

// drop leftovers from a previous run; an alias blocks dropping its target collection,
// so the alias (if any) must be dropped before either collection.
client->DropAlias(milvus::DropAliasRequest().WithAlias(alias_name));
client->DropCollection(milvus::DropCollectionRequest().WithCollectionName(second_collection_name));
client->DropCollection(milvus::DropCollectionRequest().WithCollectionName(collection_name));

CreateCollection(client, collection_name);
CreateAlias(client);
ListAliases(client);
DescribeAlias(client);
AlterAlias(client);
DescribeAlias(client);
DropAlias(client);

// cleanup
client->DropCollection(milvus::DropCollectionRequest().WithCollectionName(second_collection_name));
client->DropCollection(milvus::DropCollectionRequest().WithCollectionName(collection_name));

client->Disconnect();
return 0;
}
10 changes: 10 additions & 0 deletions examples/src/v2/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ main(int argc, char* argv[]) {
std::cout << "database.replica.number = " << resp_desc_db.Desc().Properties().at("database.replica.number")
<< std::endl;

status = client->AlterDatabaseProperties(milvus::AlterDatabasePropertiesRequest()
.WithDatabaseName(my_db_name)
.AddProperty("database.max.collections", "20"));
util::CheckStatus("alter database properties: " + my_db_name, status);

status = client->DropDatabaseProperties(milvus::DropDatabasePropertiesRequest()
.WithDatabaseName(my_db_name)
.AddPropertyKey("database.max.collections"));
util::CheckStatus("drop database properties: " + my_db_name, status);

status = client->UseDatabase(my_db_name);
util::CheckStatus("switch database:" + my_db_name, status);
std::string current_db_name;
Expand Down
46 changes: 45 additions & 1 deletion examples/src/v2/general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ main(int argc, char* argv[]) {
// for this collection and load the collection
status = client->DropCollection(
milvus::DropCollectionRequest().WithCollectionName(collection_name).WithDatabaseName(db_name));
// a leftover from an interrupted rename demo must also be dropped or the rename fails on rerun
client->DropCollection(
milvus::DropCollectionRequest().WithCollectionName(collection_name + "_renamed").WithDatabaseName(db_name));
status = client->CreateCollection(
milvus::CreateCollectionRequest()
.WithDatabaseName(db_name)
Expand Down Expand Up @@ -387,6 +390,47 @@ main(int argc, char* argv[]) {
}
}

{
// delete a batch of rows by their primary keys
std::vector<int64_t> ids_to_delete(insert_ids.begin(), insert_ids.begin() + 100);
milvus::DeleteResponse resp_delete;
status = client->Delete(
milvus::DeleteRequest().WithCollectionName(collection_name).WithIDs(std::move(ids_to_delete)), resp_delete);
util::CheckStatus("delete rows by primary keys", status);
}

{
// delete rows by a filter expression
milvus::DeleteResponse resp_delete;
status = client->Delete(
milvus::DeleteRequest().WithCollectionName(collection_name).WithFilter(field_age + " > 90"), resp_delete);
util::CheckStatus("delete rows by filter: " + field_age + " > 90", status);
}

{
// truncate the collection, removing all rows but keeping the schema and partitions
status = client->TruncateCollection(milvus::TruncateCollectionRequest().WithCollectionName(collection_name));
util::CheckStatus("truncate collection: " + collection_name, status);
}

{
// rename the collection, then rename it back so the rest of the example keeps using the original name
const std::string new_name = collection_name + "_renamed";
Comment thread
yhmo marked this conversation as resolved.
status = client->RenameCollection(
milvus::RenameCollectionRequest().WithCollectionName(collection_name).WithNewCollectionName(new_name));
util::CheckStatus("rename collection: " + collection_name + " -> " + new_name, status);

milvus::DescribeCollectionResponse desc_response;
status =
client->DescribeCollection(milvus::DescribeCollectionRequest().WithCollectionName(new_name), desc_response);
util::CheckStatus("describe renamed collection: " + new_name, status);
std::cout << "Describe renamed collection: " << desc_response.Desc().CollectionName() << std::endl;

status = client->RenameCollection(
milvus::RenameCollectionRequest().WithCollectionName(new_name).WithNewCollectionName(collection_name));
util::CheckStatus("rename collection: " + new_name + " -> " + collection_name, status);
}

// describe the collection, load state is LOADED
DescribeCollection(client);

Expand All @@ -405,7 +449,7 @@ main(int argc, char* argv[]) {
// create index again
{
milvus::IndexParam index_vector(field_face, "vector_index_name", milvus::IndexType::HNSW,
milvus::MetricType::L2);
milvus::MetricType::L2);
index_vector.AddExtraParam("M", "32");
index_vector.AddExtraParam("efConstruction", "100");

Expand Down
Loading
Loading