Join Chander Dhall for an in-depth discussion in this video Add index programmatically, part of Azure Search for Developers.
- [Instructor] Now, we go back to Program.cs and then create our index. First thing we'll do is we'll un-comment this field. This particular utility, which is BuildForType, will take the type, which in this case is type Account, which is a class in C#, and then create the fields we need for the index definition for index accounts, which is our index in cazton Azure Search instance. Next, what we need to do is create an instance of ISearchIndexClient, and we're going to call this indexClient.
We can have two kinds of clients. One is an indexClient, the other one is a queryClient. I like to keep a differentiation. And, then I could use a serviceClient and say, .Indexes.GetClient, and inside GetClient, all I need is the name of index, which happens to be account. Once we have that, we'll write a utility to import all these documents that we have in accounts.json file, and import them to our Azure instance of cazton, which is our Azure Search instance.
As you can see, we have data which is here, and if we were to read this data, you can see that we have line number one. If I go and press End, I could see there's no comma at the end. If I go and say, Ctrl+End, I can go all the way down to line number 1,000, and then press End again, and you can see there's no square brackets here. That means this data isn't in an array, which is comma separated with all these values, so we'll have to do is use a StreamReader to read the entire file line by line, and every time we get the first line, we'll can work that over to an actual object.
So, we'll take the JSON and then convert it into an actual object. Once we do that successfully for one, we're going to do that in a loop for all of them. Then, we will push the data over to Azure. The first thing to do would be to get a list of actions, which is part of Azure Search. I'm going to say, new List of IndexAction, and IndexAction of type Account. And I'll say, string line.
Well, now we're going to start the reader inside System.io.StreamReader. I'm going to call it file equals new StreamReader, accounts.json. And, we can also print the line to see what we're getting. Well, we can also use a debugger to see what we're getting.
We're going to resolve JObject, which is part of Newtonsoft.Json NuGet package, and say, json equals JObject.Parse, and we're going to parse the entire line. That line is an account. Now, first thing to do is get that line. You know how to do that. We can use a while loop and say, while line equals file.ReadLine not equals null, 'cause as long as there is a line, we need the while loop to continue.
We need to make sure that we have the parentheses in the right order. And then we have Account account equals json.ToObject. And then say, actions.Add IndexAction.Upload. Now, you can have different kinds of actions, so you can have Upload, Merge, or even MergeOrUpload, and even Delete is an action.
And, if you remember, we can have a bulk action that can have about 1,000 actions as part of it, and not more than 1,000. So, we can say IndexAction.Upload and say, account. Once this is done, we want to make sure that we close the file. Now, if you are doing this at home, it's a very good idea to put a breakpoint right here and see what kind of object we're getting back, because sometimes you might have a typo with anything in JSON, and you may not get what you expect.
So you can press F5, or hit Start, and you can hover over account, and you can see that we're getting Account_Number. We're getting every single value we wanted, and you can always go back and check this to make sure that it's exactly the values that you were looking for. So far, so good. We're going to stop debugging. Next, you want to make sure we clear a new batch of actions.
And then, we need to update the Azure Search instance with this new batch. Now, all this batch is doing is adding 1,000 new records, which are part of accounts.json. Well, in case you have an exception, give it the exact type, which is IndexBatchException.
In this case, we're just going to throw that exception, however, in a production environment, or in a web worker situation, what you're better off doing is trying to retry to do the same exact batching operation. Now, all this will work, but we haven't created the index yet, and that's on purpose. We want to make sure that we have every single definition before we even start the index. So, after we define the definitions, what we're going to do is come in here and say serviceClient.Indexes.Create, and now we're going to add the accountIndexDefinition.
This should create the index for us. Just a bit of a typo here. And that should work. You can also introduce a breakpoint right here and see what's going on. Press F5. As you can see, we have a batch of 1,000 records, and if you look at one of these batch, it says, ActionType is Upload, which will be consistently the same ActionType. We can always have a batch with multiple ActionTypes.
One record will always have one ActionType, but we can always have multiple ActionTypes in the entire batch. And, the document happens to be one of those account numbers. One thing to keep in mind is that we want to make sure that account id, if you're importing documents, Account_Number in this case, happens to be unique. So, now we can do F10, or F5, and this should create our index in Azure Search instance.
So, now you want to go to cazton, which is your search service, and then you can see we have accounts with 1,000 documents, and the storage size is pretty minimal, it's just 408 kilobytes.
- Querying and indexing
- Creating a search service
- Using APIs during searching
- Importing JSON data
- Handling synonyms
- Working with suggestors and facets