Hi Team,
I am using .NET 8 and ES Version="8.15.6". I an trying to use bulk api with a logic for upsert based on the document id. I tried the below index, update commands from Kibana as per documentation and they work fine
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
However when I try to do the same from my C# .NET code I am seeing a weird behavior. I see 2 docs indexed, 1 like below
{
"_index": "MyIndex",
"_id": "42r-9pIBQ0r3zUFWk2vX",
"_score": 1,
"_source": {
"update": {
"_index": "MyIndex",
"_id": "1234"
}
}
}
and other document with actual data inside the source. So if I have like 10 docs to be indexed what I see in elastic is 20 docs getting indexed
{
"_index": "MyIndex",
"_id": "5Gr-9pIBQ0r3zUFWk2vX",
"_score": 1,
"_source": {
"doc": {
"id": "1234",
//followed by rest of the fields
}}
}
Below is the code
List<object> bulkIndexOperations
// Check if the document exists in Elasticsearch
var existsResponse = await _elasticsearchClient.GetAsync<object>(elasticsearchId, idx => idx.Index(indexName));
if (existsResponse.Found)
{
// Prepare an update operation - index operation followed by source
bulkIndexOperations.Add(new { Update = new { _index = indexName, _id = documentId } });
bulkIndexOperations.Add(new { doc = document });
}
else
{
// Prepare an index operation
bulkIndexOperations.Add(new { Index = new { _index = indexName, _id = documentId } });
bulkIndexOperations.Add(document);
}
var bulkResponse = await _elasticsearchClient.BulkAsync(b => b
.Index(indexName)
.IndexMany(bulkIndexOperations));
Any idea why that weird extra document is getting indexed which has no source?
Thanks,
Moni