I am adding documents to Azure Search Index VIA .NET Core console app. When I am adding new documents it over-writes the old document and finally index has only one document.
I am using IndexDocumentsBatch method to upload list of documents in to the index. When I upload 5 documents the index has only last document from the list of 5 documents.
var inputDocuments = DemoObjects();
var sampleDocuments = await GetSampleDocumentsAsync(openAIClient, inputDocuments);
await searchClient.IndexDocumentsAsync(IndexDocumentsBatch.Upload(sampleDocuments));
In Azure Cognitive Search, each document needs a unique key that identifies it in the index. If multiple documents have the same key, each subsequent document will overwrite the previous one. Chech this doc for reference
You are adding documents to the Azure Search index is overwriting each other because they share the same key value.
Install Azure.Search.Documents
nuget package.
Check below code to prevent overwriting.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Azure;
using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
namespace ConsoleApp6
{
internal class Program
{
private static readonly string serviceName = "testserviceb01";
private static readonly string apiKey = "API-Key";
private static readonly Uri endpoint = new Uri($"https://{serviceName}.search.windows.net");
private static readonly string indexName = "sample-index";
static async Task Main(string[] args)
{
var searchIndexClient = new SearchIndexClient(endpoint, new AzureKeyCredential(apiKey));
var searchClient = new SearchClient(endpoint, indexName, new AzureKeyCredential(apiKey));
await CreateIndexIfNotExistsAsync(searchIndexClient, indexName);
await UploadDocumentsAsync(searchClient);
}
public static async Task CreateIndexIfNotExistsAsync(SearchIndexClient indexClient, string indexName)
{
try
{
var existingIndex = await indexClient.GetIndexAsync(indexName);
Console.WriteLine($"Index '{indexName}' already exists.");
}
catch (RequestFailedException ex) when (ex.Status == 404)
{
var fields = new FieldBuilder().Build(typeof(SampleDocument));
var index = new SearchIndex(indexName, fields);
await indexClient.CreateOrUpdateIndexAsync(index);
Console.WriteLine($"Index '{indexName}' created.");
}
}
public static List<SampleDocument> DemoObjects()
{
return new List<SampleDocument>
{
new SampleDocument { Id = Guid.NewGuid().ToString(), Name = "Document 1" },
new SampleDocument { Id = Guid.NewGuid().ToString(), Name = "Document 2" },
new SampleDocument { Id = Guid.NewGuid().ToString(), Name = "Document 3" },
new SampleDocument { Id = Guid.NewGuid().ToString(), Name = "Document 4" },
new SampleDocument { Id = Guid.NewGuid().ToString(), Name = "Document 5" }
};
}
public static async Task UploadDocumentsAsync(SearchClient searchClient)
{
var inputDocuments = DemoObjects();
var batch = IndexDocumentsBatch.Upload(inputDocuments);
try
{
IndexDocumentsResult result = await searchClient.IndexDocumentsAsync(batch);
Console.WriteLine("Documents indexed successfully.");
}
catch (RequestFailedException ex)
{
Console.WriteLine($"Indexing failed: {ex.Message}");
}
}
}
public class SampleDocument
{
[SimpleField(IsKey = true, IsFilterable = true)]
public string Id { get; set; }
public string Name { get; set; }
}
}
Indexed successfully: