In the earlier article, we saw how to create a Data Source Programmatically. Now, as a continuation, let us see how to create an Index.
The code to create an index is straight forward.
namespace CS.Help.Console
{
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Linq;
class Program
{
static void Main(string[] args)
{
CreateIndex();
}
private static void CreateIndex()
{
string searchServiceName = "Search Service Name";
string searchQueryApiKey = "API Key";
SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(searchQueryApiKey));
string indexName = "myindex";
var definition = new Index()
{
Name = indexName,
Fields = FieldBuilder.BuildForType<BlobDocuments>()
};
Index index = serviceClient.Indexes.CreateOrUpdate(definition);
}
}
}
Happy Coding,
Sathish Nadarajan.
Leave a comment