The code is as follows:
/// <summary>
/// Create an index
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected async void Button1_Click(object sender, EventArgs e)
{
// Connection configuration
var settings = new ElasticsearchClientSettings(new Uri("http://localhost:9200"))
.Authentication(new BasicAuthentication("elastic", "Trq8**********+gX"))
.DefaultIndex("vector_demo");
var client = new ElasticsearchClient(settings);
// 1. Create an index (with a 384-dimensional vector field)
var createIndexResponse = await client.Indices.CreateAsync("vector_demo", c => c
.Mappings(m => m
.Properties(p => p
.Keyword("id")
.Text("title")
.DenseVector("embedding", v => v
.Dims(384)
.Index(true)
.Similarity(DenseVectorSimilarity.Cosine)
.IndexOptions(o => o
.Type(DenseVectorIndexOptionsType.Hnsw)
.M(16)
.EfConstruction(100)
)
)
)
);
if (!createIndexResponse.IsValidResponse)
{
Response.Write($"Failed to create index: {createIndexResponse.DebugInformation}");
}
}
/// <summary>
/// Add data
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected async void Button2_Click(object sender, EventArgs e)
{
// Connection configuration
var settings = new ElasticsearchClientSettings(new Uri("http://localhost:9200"))
.Authentication(new BasicAuthentication("elastic", "Trq8w***********+gX"))
.DefaultIndex("vector_demo");
var client = new ElasticsearchClient(settings);
// 2. Add sample data
var documents = new List<object>
{
new {
id = "doc1",
title = "Introduction to Artificial Intelligence",
embedding = GenerateRandomVector(384)
},
new {
id = "doc2",
title = "Machine Learning Algorithms",
embedding = GenerateRandomVector(384)
},
new {
id = "doc3",
title = "Applications of Natural Language Processing",
embedding = GenerateRandomVector(384)
}
};
// 3. Bulk index documents (after correction, no need to wrap with BulkOperation anymore)
var bulkOperations = new List<IBulkOperation>();
foreach (var doc in documents)
{
var indexOperation = new BulkIndexOperation<object>(doc)
{
Index = "vector_demo"
};
bulkOperations.Add(indexOperation);
}
var bulkRequest = new BulkRequest
{
Operations = bulkOperations
};
var bulkResponse = await client.BulkAsync(bulkRequest);
// When executing the above line, an exception will be thrown: Elastic.Transport.UnexpectedTransportException: “The 'G' format combined with a precision is not supported. The unsupported member type is located on type 'System.Single'. Path: $.embedding.”
NotSupportedException: The 'G' format combined with a precision is not supported.
}
/// <summary>
/// Generate a random vector (for example only)
/// </summary>
/// <param name="dimensions"></param>
/// <returns></returns>
float[] GenerateRandomVector(int dimensions)
{
var random = new Random();
var vector = new float[dimensions];
for (int i = 0; i < dimensions; i++)
{
vector[i] = (float)random.NextDouble();
}
// Normalization (optional, depending on your vector generation method)
var magnitude = Math.Sqrt(vector.Sum(v => v * v));
for (int i = 0; i < dimensions; i++)
{
vector[i] /= (float)magnitude;
}
return vector;
}