I am unable to create index in Azure AI search using C#

AJITH KRISHNA MANKENA 0 Reputation points
2024-12-26T16:18:20.36+00:00

I have followed below git repo to create index in Azure AI Search (Line 231 - 307) https://github.com/Azure/azure-search-vector-samples/blob/main/demo-dotnet/DotNetVectorDemo/Program.cs
I am not using SemanticSearch so removed code from Line 266 - 283

Below is the code

public static async Task Main(string[] args)

{

SetUpIndex();

}

public static async Task SetUpIndex()

{

const string vectorSearchHnswProfile = "my-vector-profile";

const string vectorSearchHnswConfig = "myHnsw";

const string vectorSearchVectorizer = "myOpenAIVectorizer";

const string semanticSearchConfig = "my-semantic-config";

const string indexName = "my-demo-index-dummyindex";

const string AzureOpenAIEmbeddingDimensions = "1024";

try

{

    Console.WriteLine("Initializing Azure Search Service");

    SearchIndexClient indexclient = new SearchIndexClient(new Uri(searchServiceEndpoint), new AzureKeyCredential(searchApiKey));

//passing searchServiceEndpoint and key in searchApiKey

    Console.WriteLine("Connected to Azure AI Search");

    SearchIndex searchIndex = new(indexName)

    {

        VectorSearch = new()

        {

            Profiles =

        {

            new VectorSearchProfile(vectorSearchHnswProfile, vectorSearchHnswConfig)

            {

                VectorizerName = vectorSearchVectorizer

            }

        },

            Algorithms =

            {

                new HnswAlgorithmConfiguration(vectorSearchHnswConfig)

            },

            Vectorizers =

            {

            new AzureOpenAIVectorizer(vectorSearchVectorizer)

            {

                Parameters = new AzureOpenAIVectorizerParameters

                {

                    ResourceUri = new Uri(openaiApiBase), // Azure Open AI endpoint

                    ApiKey = openaiApiKey, //Azure open AI Key 

                    ModelName = deploymentName, // using text-embedding-3-large model for embedding

                    DeploymentName = deploymentName

                }

            }

            },

        },

        Fields =

    {

        new SimpleField("id", SearchFieldDataType.String) { IsKey = true, IsFilterable = true, IsSortable = true, IsFacetable = true },

        new SearchableField("title") { IsFilterable = true, IsSortable = true },

        new SearchableField("content") { IsFilterable = true, IsSortable = true },

        new SearchField("contentVector", SearchFieldDataType.Collection(SearchFieldDataType.Single))

        {

            IsSearchable = true,

            VectorSearchDimensions = int.Parse(AzureOpenAIEmbeddingDimensions),

            VectorSearchProfileName = vectorSearchHnswProfile

        },

        new SearchableField("category") { IsFilterable = true, IsSortable = true, IsFacetable = true }

    }

    };

    Console.WriteLine(searchIndex.Name.ToString());

    Console.WriteLine("Creating Index");

    //await indexclient.CreateIndexAsync(searchIndex); //first tried with this, its not working

    await indexclient.CreateOrUpdateIndexAsync(searchIndex);

    Console.WriteLine($"Index created successfully {indexName}");

}

catch (RequestFailedException ex)

{

    Console.WriteLine($"Error creating index: {ex.Message}");

}

catch (Exception ex)

{

    Console.WriteLine($"Error creating index: {ex.Message}");

}

}

The code is not even throwing any errors or exceptions. It's printing Console.WriteLine("Creating Index"); on console but when entering await indexclient.CreateOrUpdateIndexAsync(searchIndex); program is simply terminating. No exception is being thrown or printed on the console. Any support on this issue is very helpful.

Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
1,119 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,154 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,002 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.