MCPcopy Create free account
hub / github.com/alibaba/zvec / create_simple_test_collection

Function create_simple_test_collection

examples/c/basic_example.c:38–124  ·  view source on GitHub ↗

* @brief Create a simple test collection using CollectionSchema */

Source from the content-addressed store, hash-verified

36 * @brief Create a simple test collection using CollectionSchema
37 */
38static zvec_error_code_t create_simple_test_collection(
39 zvec_collection_t **collection) {
40 // Create collection schema using C API
41 zvec_collection_schema_t *schema =
42 zvec_collection_schema_create("test_collection");
43 if (!schema) {
44 return ZVEC_ERROR_INTERNAL_ERROR;
45 }
46
47 zvec_error_code_t error = ZVEC_OK;
48
49 // Create index parameters using new API
50 zvec_index_params_t *invert_params =
51 zvec_index_params_create(ZVEC_INDEX_TYPE_INVERT);
52 if (!invert_params) {
53 zvec_collection_schema_destroy(schema);
54 return ZVEC_ERROR_RESOURCE_EXHAUSTED;
55 }
56 zvec_index_params_set_invert_params(invert_params, true, false);
57
58 zvec_index_params_t *hnsw_params =
59 zvec_index_params_create(ZVEC_INDEX_TYPE_HNSW);
60 if (!hnsw_params) {
61 zvec_index_params_destroy(invert_params);
62 zvec_collection_schema_destroy(schema);
63 return ZVEC_ERROR_RESOURCE_EXHAUSTED;
64 }
65 zvec_index_params_set_metric_type(hnsw_params, ZVEC_METRIC_TYPE_COSINE);
66 zvec_index_params_set_hnsw_params(hnsw_params, 16, 200);
67
68 // Create and add ID field (primary key)
69 zvec_field_schema_t *id_field =
70 zvec_field_schema_create("id", ZVEC_DATA_TYPE_STRING, false, 0);
71 zvec_field_schema_set_index_params(id_field, invert_params);
72 error = zvec_collection_schema_add_field(schema, id_field);
73 if (error != ZVEC_OK) {
74 zvec_index_params_destroy(invert_params);
75 zvec_index_params_destroy(hnsw_params);
76 zvec_collection_schema_destroy(schema);
77 return error;
78 }
79
80 // Create text field (inverted index)
81 zvec_field_schema_t *text_field =
82 zvec_field_schema_create("text", ZVEC_DATA_TYPE_STRING, true, 0);
83 zvec_field_schema_set_index_params(text_field, invert_params);
84 error = zvec_collection_schema_add_field(schema, text_field);
85 if (error != ZVEC_OK) {
86 zvec_index_params_destroy(invert_params);
87 zvec_index_params_destroy(hnsw_params);
88 zvec_collection_schema_destroy(schema);
89 return error;
90 }
91
92 // Create embedding field (HNSW index)
93 zvec_field_schema_t *embedding_field = zvec_field_schema_create(
94 "embedding", ZVEC_DATA_TYPE_VECTOR_FP32, false, 3);
95 zvec_field_schema_set_index_params(embedding_field, hnsw_params);

Callers 1

mainFunction · 0.85

Tested by

no test coverage detected