Error when adding vector data with Elastic.Clients.Elasticsearch version 9.0.0

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;
}

Welcome!

What is the error?

Hi @ppworks

from your code snippet, it seems like the exception is as follows:

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.
}

I just tested the exact same code and for me the test data is correctly indexed.

What .NET SDK are you using and what is the TFM for your application?

Maybe you missed a word Florian. What is the ???? for his/her application!?

Thanks! I edited my post :slightly_smiling_face:

The System.Memory shim (even the latest version version 4.6.3) for .NET Framework, does not support specifying custom precision for serialized data.

Given that ES driver tries to pass there a G17 format string, it crashes. This leads to the inability of driver to serialize floating-point numbers, when driver is running under .NET Framework.

Reported as Broken floating-point source serialization for .NET Framework · Issue #8696 · elastic/elasticsearch-net · GitHub