Create a collection (C#)
Creates a new collection in a database.
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Creates a collection with the specified parameters.
Returns a Collection object.
You can use this object to work with documents in the collection.
By default, the Collection object is typed as Collection<Document>, where Document is Dictionary<string, object>.
You can enable stronger typing by specifying a type when you create the collection.
For more information and examples, see Custom typing for collections.
Parameters
|
You cannot edit a collection’s definition after you create the collection. |
Use the CreateCollectionAsync method, which belongs to the Database class.
You can also use CreateCollection, which is the synchronous version of the method.
Method signature
public Task<Collection<T, TId>> CreateCollectionAsync<T, TId>(
string collectionName,
CollectionDefinition definition,
CreateCollectionOptions options = null
) where T : class;
public Task<Collection<T, TId>> CreateCollectionAsync<T, TId>(
CollectionDefinition definition, CreateCollectionOptions options = null
) where T : class;
public Task<Collection<T, TId>> CreateCollectionAsync<T, TId>(
string collectionName, CreateCollectionOptions options = null
) where T : class;
public Task<Collection<T, TId>> CreateCollectionAsync<T, TId>(
CreateCollectionOptions options = null
) where T : class;
public Task<Collection<Document>> CreateCollectionAsync(
string collectionName,
CollectionDefinition definition,
CreateCollectionOptions options = null
);
public Task<Collection<T>> CreateCollectionAsync<T>(
string collectionName,
CollectionDefinition definition,
CreateCollectionOptions options = null
) where T : class;
public Task<Collection<T>> CreateCollectionAsync<T>(
CollectionDefinition definition, CreateCollectionOptions options = null
) where T : class;
public Task<Collection<T>> CreateCollectionAsync<T>(
string collectionName, CreateCollectionOptions options = null
) where T : class;
public Task<Collection<T>> CreateCollectionAsync<T>(
CreateCollectionOptions options = null
) where T : class;
public Task<Collection<Document>> CreateCollectionAsync(
string collectionName, CreateCollectionOptions options = null
);
| Name | Type | Summary |
|---|---|---|
|
|
The name of the new collection. Collection names must follow these rules:
If not specified, the client attempts to extract it from the |
|
Optional.
The full configuration for the collection. See Properties of the |
|
|
Optional.
Options for this operation.
For more information and examples for general options such as timeout and keyspace, see Customize API interaction.
For options specific to this method, see Method-specific properties of the |
| Name | Type | Summary |
|---|---|---|
|
|
Optional. The vector configuration for the collection. This includes things like the vector dimension and similarity metric. This also includes settings for server-side embedding generation if you want your collection to have vectorize enabled. Required for vector search and hybrid search. |
|
Optional. The reranker configuration for the collection. Only collections in databases in the AWS The
See Create a collection that supports hybrid search for usage. Default: A |
|
|
|
Optional.
Specifies the default ID type for documents in the collection.
This is used when you insert a document without an
See the example for usage. For more information, see Document IDs (C#). Default: Each autogenerated |
|
|
Optional. The selective indexing configuration for the collection. You must use See Create a collection and specify which fields to index and Create a collection and specify which fields shouldn’t be indexed for usage. Default: All fields of all documents. |
| Name | Type | Summary |
|---|---|---|
|
|
Optional. This only applies to collections with a vectorize embedding provider integration. Use this option to provide the embedding provider API key directly with headers instead of using an API key in the Astra DB KMS. The API key is sent to the Data API for every operation on the collection. It is useful when a vectorize integration is configured but no credentials are stored, or when you want to override the stored credentials. For more information, see Manage embedding provider integrations for vectorize. If you use an AWS embedding provider, you must use the |
|
|
If you use an AWS embedding provider, you must use the |
Examples
The following examples demonstrate how to create a collection.
Create a collection that is not vector-enabled
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.SerDes;
namespace Examples;
// Define the type for the collection
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var collection = await database.CreateCollectionAsync<User>();
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var collection = await database.CreateCollectionAsync(
"COLLECTION_NAME"
);
}
}
Create a collection that can store vector embeddings
Collections that are vector-enabled can store vector embeddings in the reserved $vector field and work with vector search.
For optimal vector search results, you should specify the dimension, metric, and source model of your vector embeddings.
All vector embeddings in a collection should be generated by the same model with the same dimensions.
The source model can be one of: ada002, bert, cohere-v3, gecko, nv-qa-4, openai-v3-large, openai-v3-small, other.
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.SerDes;
namespace Examples;
// Define the type for the collection
[CollectionName("COLLECTION_NAME")]
[CollectionVector(SimilarityMetric.Cosine, 1024, SourceModel = "nv-qa-4")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vector)]
public float[]? VectorEmbeddings { get; set; }
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var collection = await database.CreateCollectionAsync<User>();
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var definition = new CollectionDefinition()
{
Vector = new VectorOptions()
{
Dimension = 1024,
Metric = SimilarityMetric.Cosine,
SourceModel = "nv-qa-4",
},
};
var collection = await database.CreateCollectionAsync(
"COLLECTION_NAME",
definition
);
}
}
Create a collection that can automatically generate vector embeddings
If you want to automatically generate vector embeddings, create a vector-enabled collection and configure an embedding provider integration for the collection.
The configuration depends on the embedding provider.
Configure Azure OpenAI as the embedding provider
For more detailed instructions, see Integrate Azure OpenAI as an embedding provider.
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using System.Text.Json.Serialization;
using DataStax.AstraDB.DataApi.SerDes;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
// Define the type for the collection
[CollectionVectorize(
"azureOpenAI",
"MODEL_NAME",
SimilarityMetric.SIMILARITY_METRIC,
MODEL_DIMENSIONS,
new string[]
{
"providerKey",
"API_KEY_NAME",
},
new object[] {
"resourceName",
"RESOURCE_NAME",
"deploymentId",
"DEPLOYMENT_ID"
}
)]
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Create the collection
var collection = await database.CreateCollectionAsync<User>();
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Define the collection
var definition = new CollectionDefinition()
{
Vector = new VectorOptions()
{
Dimension = MODEL_DIMENSIONS,
Metric = SimilarityMetric.SIMILARITY_METRIC,
Service = new VectorServiceOptions()
{
Provider = "azureOpenAI",
ModelName = "MODEL_NAME",
Authentication = new Dictionary<string, string>()
{
{ "providerKey", "API_KEY_NAME" }
},
Parameters = new Dictionary<string, object>()
{
{ "resourceName", "RESOURCE_NAME" },
{ "deploymentId", "DEPLOYMENT_ID" }
},
}
}
};
// Create the collection
var collection = await database.CreateCollectionAsync("COLLECTION_NAME", definition);
}
}
Replace the following:
-
COLLECTION_NAME: The name for your collection. -
SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean. -
API_KEY_NAME: The name of the Azure OpenAI API key that you want to use. Must be the name of an existing Azure OpenAI API key in the Astra Portal. For more information, see Embedding provider authentication.Alternatively, you can omit this parameter and instead provide the authentication key in the
EmbeddingAPIKeyproperty ofCreateCollectionOptionsorGetCollectionOptionswhen you instantiate aCollectionobject with the commands to create a collection or get a collection. The client will send thex-embedding-api-keyheader with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides theAPI_KEY_NAMEparameter if you set both. If you use the header instead of specifying theAPI_KEY_NAMEparameter, you must include the header in every command that uses vectorize, including writes and vector search. -
MODEL_NAME: The model that you want to use to generate embeddings. The available models are:text-embedding-3-small,text-embedding-3-large,text-embedding-ada-002.For Azure OpenAI, you must select the model that matches the one deployed to your
DEPLOYMENT_IDin Azure. -
MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.
-
RESOURCE_NAME: The name of your Azure OpenAI Service resource, as defined in the resource’s Instance details. For more information, see the Azure OpenAI documentation. -
DEPLOYMENT_ID: Your Azure OpenAI resource’s Deployment name. For more information, see the Azure OpenAI documentation.
Configure Hugging Face (Dedicated) as the embedding provider
For more detailed instructions, see Integrate Hugging Face Dedicated as an embedding provider.
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using System.Text.Json.Serialization;
using DataStax.AstraDB.DataApi.SerDes;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
// Define the type for the collection
[CollectionVectorize(
"huggingfaceDedicated",
"endpoint-defined-model",
SimilarityMetric.SIMILARITY_METRIC,
MODEL_DIMENSIONS,
new string[]
{
"providerKey",
"API_KEY_NAME",
},
new object[] {
"endpointName",
"ENDPOINT_NAME",
"regionName",
"REGION_NAME",
"cloudName",
"CLOUD_NAME"
}
)]
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Create the collection
var collection = await database.CreateCollectionAsync<User>();
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Define the collection
var definition = new CollectionDefinition()
{
Vector = new VectorOptions()
{
Dimension = MODEL_DIMENSIONS,
Metric = SimilarityMetric.SIMILARITY_METRIC,
Service = new VectorServiceOptions()
{
Provider = "huggingfaceDedicated",
ModelName = "endpoint-defined-model",
Authentication = new Dictionary<string, string>()
{
{ "providerKey", "API_KEY_NAME" }
},
Parameters = new Dictionary<string, object>()
{
{ "endpointName", "ENDPOINT_NAME" },
{ "regionName", "REGION_NAME" },
{ "cloudName", "CLOUD_NAME" }
},
}
}
};
// Create the collection
var collection = await database.CreateCollectionAsync("COLLECTION_NAME", definition);
}
}
Replace the following:
-
COLLECTION_NAME: The name for your collection. -
SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean. -
API_KEY_NAME: The name of the Hugging Face Dedicated user access token that you want to use. Must be the name of an existing Hugging Face Dedicated user access token in the Astra Portal. For more information, see Embedding provider authentication.Alternatively, you can omit this parameter and instead provide the authentication key in the
EmbeddingAPIKeyproperty ofCreateCollectionOptionsorGetCollectionOptionswhen you instantiate aCollectionobject with the commands to create a collection or get a collection. The client will send thex-embedding-api-keyheader with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides theAPI_KEY_NAMEparameter if you set both. If you use the header instead of specifying theAPI_KEY_NAMEparameter, you must include the header in every command that uses vectorize, including writes and vector search. -
MODEL_NAME: The model that you want to use to generate embeddings. The available models are:endpoint-defined-model.For Hugging Face Dedicated, you must deploy the model as a text embeddings inference (TEI) container.
You must set
MODEL_NAMEtoendpoint-defined-modelbecause this integration uses the model specified in your dedicated endpoint configuration. -
MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.
-
ENDPOINT_NAME: The programmatically-generated name of your Hugging Face Dedicated endpoint. This is the first part of the endpoint URL. For example, if your endpoint URL ishttps://mtp1x7muf6qyn3yh.us-east-2.aws.endpoints.huggingface.cloud, the endpoint name ismtp1x7muf6qyn3yh. -
REGION: The cloud provider region your Hugging Face Dedicated endpoint is deployed to. For example,us-east-2. -
CLOUD_PROVIDER: The cloud provider your Hugging Face Dedicated endpoint is deployed to. For example,aws.
Configure Hugging Face (Serverless) as the embedding provider
For more detailed instructions, see Integrate Hugging Face Serverless as an embedding provider.
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using System.Text.Json.Serialization;
using DataStax.AstraDB.DataApi.SerDes;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
// Define the type for the collection
[CollectionVectorize(
"huggingface",
"MODEL_NAME",
SimilarityMetric.SIMILARITY_METRIC,
MODEL_DIMENSIONS,
new string[]
{
"providerKey",
"API_KEY_NAME",
}
)]
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Create the collection
var collection = await database.CreateCollectionAsync<User>();
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Define the collection
var definition = new CollectionDefinition()
{
Vector = new VectorOptions()
{
Dimension = MODEL_DIMENSIONS,
Metric = SimilarityMetric.SIMILARITY_METRIC,
Service = new VectorServiceOptions()
{
Provider = "huggingface",
ModelName = "MODEL_NAME",
Authentication = new Dictionary<string, string>()
{
{ "providerKey", "API_KEY_NAME" }
}
}
}
};
// Create the collection
var collection = await database.CreateCollectionAsync("COLLECTION_NAME", definition);
}
}
Replace the following:
-
COLLECTION_NAME: The name for your collection. -
SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean. -
API_KEY_NAME: The name of the Hugging Face Serverless user access token that you want to use. Must be the name of an existing Hugging Face Serverless user access token in the Astra Portal. For more information, see Embedding provider authentication.Alternatively, you can omit this parameter and instead provide the authentication key in the
EmbeddingAPIKeyproperty ofCreateCollectionOptionsorGetCollectionOptionswhen you instantiate aCollectionobject with the commands to create a collection or get a collection. The client will send thex-embedding-api-keyheader with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides theAPI_KEY_NAMEparameter if you set both. If you use the header instead of specifying theAPI_KEY_NAMEparameter, you must include the header in every command that uses vectorize, including writes and vector search. -
MODEL_NAME: The model that you want to use to generate embeddings. The available models are:sentence-transformers/all-MiniLM-L6-v2,intfloat/multilingual-e5-large,intfloat/multilingual-e5-large-instruct,BAAI/bge-small-en-v1.5,BAAI/bge-base-en-v1.5,BAAI/bge-large-en-v1.5. -
MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.
Configure Jina AI as the embedding provider
For more detailed instructions, see Integrate Jina AI as an embedding provider.
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using System.Text.Json.Serialization;
using DataStax.AstraDB.DataApi.SerDes;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
// Define the type for the collection
[CollectionVectorize(
"jinaAI",
"MODEL_NAME",
SimilarityMetric.SIMILARITY_METRIC,
MODEL_DIMENSIONS,
new string[]
{
"providerKey",
"API_KEY_NAME",
}
)]
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Create the collection
var collection = await database.CreateCollectionAsync<User>();
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Define the collection
var definition = new CollectionDefinition()
{
Vector = new VectorOptions()
{
Dimension = MODEL_DIMENSIONS,
Metric = SimilarityMetric.SIMILARITY_METRIC,
Service = new VectorServiceOptions()
{
Provider = "jinaAI",
ModelName = "MODEL_NAME",
Authentication = new Dictionary<string, string>()
{
{ "providerKey", "API_KEY_NAME" }
}
}
}
};
// Create the collection
var collection = await database.CreateCollectionAsync("COLLECTION_NAME", definition);
}
}
Replace the following:
-
COLLECTION_NAME: The name for your collection. -
SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean. -
API_KEY_NAME: The name of the Jina AI API key that you want to use. Must be the name of an existing Jina AI API key in the Astra Portal. For more information, see Embedding provider authentication.Alternatively, you can omit this parameter and instead provide the authentication key in the
EmbeddingAPIKeyproperty ofCreateCollectionOptionsorGetCollectionOptionswhen you instantiate aCollectionobject with the commands to create a collection or get a collection. The client will send thex-embedding-api-keyheader with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides theAPI_KEY_NAMEparameter if you set both. If you use the header instead of specifying theAPI_KEY_NAMEparameter, you must include the header in every command that uses vectorize, including writes and vector search. -
MODEL_NAME: The model that you want to use to generate embeddings. The available models are:jina-embeddings-v2-base-en,jina-embeddings-v2-base-de,jina-embeddings-v2-base-es,jina-embeddings-v2-base-code,jina-embeddings-v2-base-zh. -
MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.
Configure Mistral AI as the embedding provider
For more detailed instructions, see Integrate Mistral AI as an embedding provider.
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using System.Text.Json.Serialization;
using DataStax.AstraDB.DataApi.SerDes;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
// Define the type for the collection
[CollectionVectorize(
"mistral",
"MODEL_NAME",
SimilarityMetric.SIMILARITY_METRIC,
MODEL_DIMENSIONS,
new string[]
{
"providerKey",
"API_KEY_NAME",
}
)]
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Create the collection
var collection = await database.CreateCollectionAsync<User>();
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Define the collection
var definition = new CollectionDefinition()
{
Vector = new VectorOptions()
{
Dimension = MODEL_DIMENSIONS,
Metric = SimilarityMetric.SIMILARITY_METRIC,
Service = new VectorServiceOptions()
{
Provider = "mistral",
ModelName = "MODEL_NAME",
Authentication = new Dictionary<string, string>()
{
{ "providerKey", "API_KEY_NAME" }
}
}
}
};
// Create the collection
var collection = await database.CreateCollectionAsync("COLLECTION_NAME", definition);
}
}
Replace the following:
-
COLLECTION_NAME: The name for your collection. -
SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean. -
API_KEY_NAME: The name of the Mistral AI API key that you want to use. Must be the name of an existing Mistral AI API key in the Astra Portal. For more information, see Embedding provider authentication.Alternatively, you can omit this parameter and instead provide the authentication key in the
EmbeddingAPIKeyproperty ofCreateCollectionOptionsorGetCollectionOptionswhen you instantiate aCollectionobject with the commands to create a collection or get a collection. The client will send thex-embedding-api-keyheader with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides theAPI_KEY_NAMEparameter if you set both. If you use the header instead of specifying theAPI_KEY_NAMEparameter, you must include the header in every command that uses vectorize, including writes and vector search. -
MODEL_NAME: The model that you want to use to generate embeddings. The available models are:mistral-embed. -
MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.
Configure NVIDIA as the embedding provider
For more detailed instructions, see Integrate NVIDIA as an embedding provider. Your database must be in a supported region.
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using System.Text.Json.Serialization;
using DataStax.AstraDB.DataApi.SerDes;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
// Define the type for the collection
[CollectionVectorize(
"nvidia",
"nvidia/nv-embedqa-e5-v5",
SimilarityMetric.Cosine
)]
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Create the collection
var collection = await database.CreateCollectionAsync<User>();
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Define the collection
var definition = new CollectionDefinition()
{
Vector = new VectorOptions()
{
Metric = SimilarityMetric.Cosine,
Service = new VectorServiceOptions()
{
Provider = "nvidia",
ModelName = "nvidia/nv-embedqa-e5-v5",
}
}
};
// Create the collection
var collection = await database.CreateCollectionAsync("COLLECTION_NAME", definition);
}
}
Configure OpenAI as the embedding provider
For more detailed instructions, see Integrate OpenAI as an embedding provider.
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using System.Text.Json.Serialization;
using DataStax.AstraDB.DataApi.SerDes;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
// Define the type for the collection
[CollectionVectorize(
"openai",
"MODEL_NAME",
SimilarityMetric.SIMILARITY_METRIC,
MODEL_DIMENSIONS,
new string[]
{
"providerKey",
"API_KEY_NAME",
},
new object[] {
"organizationId",
"ORGANIZATION_ID",
"projectId",
"PROJECT_ID"
}
)]
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Create the collection
var collection = await database.CreateCollectionAsync<User>();
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Define the collection
var definition = new CollectionDefinition()
{
Vector = new VectorOptions()
{
Dimension = MODEL_DIMENSIONS,
Metric = SimilarityMetric.SIMILARITY_METRIC,
Service = new VectorServiceOptions()
{
Provider = "openai",
ModelName = "MODEL_NAME",
Authentication = new Dictionary<string, string>()
{
{ "providerKey", "API_KEY_NAME" }
},
Parameters = new Dictionary<string, object>()
{
{ "organizationId", "ORGANIZATION_ID" },
{ "projectId", "PROJECT_ID" }
},
}
}
};
// Create the collection
var collection = await database.CreateCollectionAsync("COLLECTION_NAME", definition);
}
}
Replace the following:
-
COLLECTION_NAME: The name for your collection. -
SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean. -
API_KEY_NAME: The name of the OpenAI API key that you want to use. Must be the name of an existing OpenAI API key in the Astra Portal. For more information, see Embedding provider authentication.Alternatively, you can omit this parameter and instead provide the authentication key in the
EmbeddingAPIKeyproperty ofCreateCollectionOptionsorGetCollectionOptionswhen you instantiate aCollectionobject with the commands to create a collection or get a collection. The client will send thex-embedding-api-keyheader with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides theAPI_KEY_NAMEparameter if you set both. If you use the header instead of specifying theAPI_KEY_NAMEparameter, you must include the header in every command that uses vectorize, including writes and vector search. -
MODEL_NAME: The model that you want to use to generate embeddings. The available models are:text-embedding-3-small,text-embedding-3-large,text-embedding-ada-002. -
MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.
-
ORGANIZATION_ID: Optional. The ID of the OpenAI organization that owns the API key. Only required if your OpenAI account belongs to multiple organizations or if you are using a legacy user API key to access projects. For more information about organization IDs, see the OpenAI API reference. -
PROJECT_ID: Optional. The ID of the OpenAI project that owns the API key. This cannot use the default project. Only required if your OpenAI account belongs to multiple organizations or if you are using a legacy user API key to access projects. For more information about project IDs, see the OpenAI API reference.
Configure Upstage as the embedding provider
For more detailed instructions, see Integrate Upstage as an embedding provider.
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using System.Text.Json.Serialization;
using DataStax.AstraDB.DataApi.SerDes;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
// Define the type for the collection
[CollectionVectorize(
"upstageAI",
"MODEL_NAME",
SimilarityMetric.SIMILARITY_METRIC,
MODEL_DIMENSIONS,
new string[]
{
"providerKey",
"API_KEY_NAME",
}
)]
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Create the collection
var collection = await database.CreateCollectionAsync<User>();
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Define the collection
var definition = new CollectionDefinition()
{
Vector = new VectorOptions()
{
Dimension = MODEL_DIMENSIONS,
Metric = SimilarityMetric.SIMILARITY_METRIC,
Service = new VectorServiceOptions()
{
Provider = "upstageAI",
ModelName = "MODEL_NAME",
Authentication = new Dictionary<string, string>()
{
{ "providerKey", "API_KEY_NAME" }
}
}
}
};
// Create the collection
var collection = await database.CreateCollectionAsync("COLLECTION_NAME", definition);
}
}
Replace the following:
-
COLLECTION_NAME: The name for your collection. -
SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean. -
API_KEY_NAME: The name of the Upstage API key that you want to use. Must be the name of an existing Upstage API key in the Astra Portal. For more information, see Embedding provider authentication.Alternatively, you can omit this parameter and instead provide the authentication key in the
EmbeddingAPIKeyproperty ofCreateCollectionOptionsorGetCollectionOptionswhen you instantiate aCollectionobject with the commands to create a collection or get a collection. The client will send thex-embedding-api-keyheader with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides theAPI_KEY_NAMEparameter if you set both. If you use the header instead of specifying theAPI_KEY_NAMEparameter, you must include the header in every command that uses vectorize, including writes and vector search. -
MODEL_NAME: The model that you want to use to generate embeddings. The available models are:solar-embedding-1-large. -
MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.
Configure Voyage AI as the embedding provider
For more detailed instructions, see Integrate Voyage AI as an embedding provider.
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using System.Text.Json.Serialization;
using DataStax.AstraDB.DataApi.SerDes;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
// Define the type for the collection
[CollectionVectorize(
"voyageAI",
"MODEL_NAME",
SimilarityMetric.SIMILARITY_METRIC,
MODEL_DIMENSIONS,
new string[]
{
"providerKey",
"API_KEY_NAME",
}
)]
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Create the collection
var collection = await database.CreateCollectionAsync<User>();
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Admin;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
new GetDatabaseOptions()
{
Token = "APPLICATION_TOKEN"
}
);
// Define the collection
var definition = new CollectionDefinition()
{
Vector = new VectorOptions()
{
Dimension = MODEL_DIMENSIONS,
Metric = SimilarityMetric.SIMILARITY_METRIC,
Service = new VectorServiceOptions()
{
Provider = "voyageAI",
ModelName = "MODEL_NAME",
Authentication = new Dictionary<string, string>()
{
{ "providerKey", "API_KEY_NAME" }
}
}
}
};
// Create the collection
var collection = await database.CreateCollectionAsync("COLLECTION_NAME", definition);
}
}
Replace the following:
-
COLLECTION_NAME: The name for your collection. -
SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean. -
API_KEY_NAME: The name of the Voyage AI API key that you want to use. Must be the name of an existing Voyage AI API key in the Astra Portal. For more information, see Embedding provider authentication.Alternatively, you can omit this parameter and instead provide the authentication key in the
EmbeddingAPIKeyproperty ofCreateCollectionOptionsorGetCollectionOptionswhen you instantiate aCollectionobject with the commands to create a collection or get a collection. The client will send thex-embedding-api-keyheader with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides theAPI_KEY_NAMEparameter if you set both. If you use the header instead of specifying theAPI_KEY_NAMEparameter, you must include the header in every command that uses vectorize, including writes and vector search. -
MODEL_NAME: The model that you want to use to generate embeddings. The available models are:voyage-2,voyage-code-2,voyage-finance-2,voyage-large-2,voyage-large-2-instruct,voyage-law-2,voyage-multilingual-2. -
MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.
Create a collection that supports hybrid search
If you want to perform hybrid search on your collection, you must create a collection that has vector, lexical, and rerank enabled.
Your collection must also be in a database in the AWS us-east-2 region.
Lexical and rerank are enabled by default when you create a collection in a database in the AWS us-east-2 region, but you can optionally configure the lexical analyzer and the reranker model.
For configuration details about the lexical analyzer, see Find data with CQL analyzers. The following example uses a configuration suitable for English text.
For configuration details about the reranker model, inspect the available reranker models. Only the NVIDIA llama-3.2-nv-rerankqa-1b-v2 reranking model reranker model is supported.
For configuration details about vector, see Create a collection that can store vector embeddings and Create a collection that can automatically generate vector embeddings.
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.SerDes;
namespace Examples;
// Define the type for the collection
[CollectionName("COLLECTION_NAME")]
[CollectionVectorize(
"nvidia",
"nvidia/nv-embedqa-e5-v5",
SimilarityMetric.Cosine
)]
[LexicalOptions(
TokenizerName = "standard",
Filters = new[] { "lowercase", "stop", "porterstem", "asciifolding" },
CharacterFilters = new string[] { }
)]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var definition = new CollectionDefinition()
{
Rerank = new RerankOptions()
{
Enabled = true,
Service = new RerankServiceOptions()
{
Provider = "nvidia",
ModelName = "nvidia/llama-3.2-nv-rerankqa-1b-v2",
},
},
};
var collection = await database.CreateCollectionAsync<User>(
definition
);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var definition = new CollectionDefinition()
{
Vector = new VectorOptions()
{
Metric = SimilarityMetric.Cosine,
Service = new VectorServiceOptions()
{
Provider = "nvidia",
ModelName = "nvidia/nv-embedqa-e5-v5",
},
},
Lexical = new LexicalOptions()
{
Analyzer = new AnalyzerOptions()
{
Tokenizer = new TokenizerOptions()
{
Name = "standard",
Arguments = new Dictionary<string, object>() { },
},
Filters = new List<string>()
{
"lowercase",
"stop",
"porterstem",
"asciifolding",
},
CharacterFilters = new List<string>() { },
},
Enabled = true,
},
Rerank = new RerankOptions()
{
Enabled = true,
Service = new RerankServiceOptions()
{
Provider = "nvidia",
ModelName = "nvidia/llama-3.2-nv-rerankqa-1b-v2",
},
},
};
var collection = await database.CreateCollectionAsync(
"COLLECTION_NAME",
definition
);
}
}
Create a collection that supports lexicographical matching
If you want to use lexicographical matching to find documents in your collection, you must create a collection that has lexical enabled.
Your collection must also be in a database in the AWS us-east-2 region.
Lexical is enabled by default when you create a collection in a database in the AWS us-east-2 region, but you can optionally configure the lexical analyzer.
For configuration details about the lexical analyzer, see Find data with CQL analyzers. The following example uses a configuration suitable for English text.
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.SerDes;
namespace Examples;
// Define the type for the collection
[CollectionName("COLLECTION_NAME")]
[LexicalOptions(
TokenizerName = "standard",
Filters = new[] { "lowercase", "stop", "porterstem", "asciifolding" },
CharacterFilters = new string[] { }
)]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var collection = await database.CreateCollectionAsync<User>();
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var definition = new CollectionDefinition()
{
Lexical = new LexicalOptions()
{
Analyzer = new AnalyzerOptions()
{
Tokenizer = new TokenizerOptions()
{
Name = "standard",
Arguments = new Dictionary<string, object>() { },
},
Filters = new List<string>()
{
"lowercase",
"stop",
"porterstem",
"asciifolding",
},
CharacterFilters = new List<string>() { },
},
Enabled = true,
},
};
var collection = await database.CreateCollectionAsync(
"COLLECTION_NAME",
definition
);
}
}
Create a collection and specify the default ID format
For more information about the default ID format, see Document IDs (C#). For allowed values, see the Parameters.
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.SerDes;
using MongoDB.Bson;
namespace Examples;
// Define the type for the collection
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId(DefaultIdType.ObjectId)]
public ObjectId? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var collection = await database.CreateCollectionAsync<User>();
}
}
If you create a custom-typed collection without providing a CollectionDefinition, then you can use one of the following attributes on your custom type to specify the default ID type instead:
-
[DocumentId(DefaultIdType.UuidV6)] -
[DocumentId(DefaultIdType.UuidV7)] -
[DocumentId(DefaultIdType.ObjectId)] -
[DocumentId](defaults to UUID v4)
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var definition = new CollectionDefinition()
{
DefaultId = new DefaultIdOptions()
{
Type = DefaultIdType.ObjectId,
},
};
var collection = await database.CreateCollectionAsync(
"COLLECTION_NAME",
definition
);
}
}
Create a collection and specify which fields to index
For more information about selective indexing, see Indexes in collections (C#).
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.SerDes;
namespace Examples;
// Define the type for the collection
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
public string? City { get; set; }
public string? Country { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var definition = new CollectionDefinition()
{
Indexing = new IndexingOptions()
{
Allow = new List<string> { "City", "Country" },
},
};
var collection = await database.CreateCollectionAsync<User>(
definition
);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var definition = new CollectionDefinition()
{
Indexing = new IndexingOptions()
{
Allow = new List<string> { "city", "country" },
},
};
var collection = await database.CreateCollectionAsync(
"COLLECTION_NAME",
definition
);
}
}
Create a collection and specify which fields shouldn’t be indexed
For more information about selective indexing, see Indexes in collections (C#).
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.SerDes;
namespace Examples;
// Define the type for the collection
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
public string? City { get; set; }
public string? Country { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var definition = new CollectionDefinition()
{
Indexing = new IndexingOptions()
{
Deny = new List<string> { "City", "Country" },
},
};
var collection = await database.CreateCollectionAsync<User>(
definition
);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var definition = new CollectionDefinition()
{
Indexing = new IndexingOptions()
{
Deny = new List<string> { "city", "country" },
},
};
var collection = await database.CreateCollectionAsync(
"COLLECTION_NAME",
definition
);
}
}
Create a collection and specify the keyspace
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Instantiate the client
var client = new DataAPIClient();
// Connect to a database
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Create a collection
var collection = await database.CreateCollectionAsync(
"COLLECTION_NAME",
new CreateCollectionOptions() { Keyspace = "KEYSPACE_NAME" }
);
}
}
Client reference
For more information, see the client reference.