Join Chander Dhall for an in-depth discussion in this video Suggestors, part of Azure Search for Developers.
- [Instructor] Now I'd like to show you suggesters, which are also known as autocomplete or type aheads. Before we get there we're going to have to get rid of some of this code. One thing to keep in mind is that in order to make suggesters work, I will have to alter the definition. However, with the current version of Azure Search, there's a limitation, and it would not allow me to add that particular definition for the existing fields. It only allows me to do that for additional fields that I want to add.
So for now just to keep things simple, what we're going to do is we're going to delete our existing index, and then import the documents as we did previously. In order to do that, you can simply copy and paste from the file I've provided. And bring them at the right here.
We don't need the weights anymore, so I'm going to get rid of this code. We have the account definitions. Before we add the account definition, I want to make sure that we have serviceClient.Indexes.Exists. And if the index exists, we want to make sure that we delete it. It's a good practice to check for exceptions while this is happening. But for demo purposes, we won't do that.
Now that we have the account and definition, we don't even need this coding profile, so we can get rid of this code. And add the suggester code we need. So we have our suggester = new suggester. And then give it any name we like. We can call it autoComplete. And now we want to add certain fields to it.
We could do SourceFields.Add. This could be firstName. And we can add another field. This could be lastName. Then we need to add that gesture inside our definition.
And then add the suggester. Now that's all we need for our index to have this information. Now our index has a suggester called autoComplete, which has source fields firstName and lastName. Now we can use the same exact logic to be able to query this from our query client. We can get rid of all this code. And get SuggestParameters. Keep in mind these are not search parameters, they are SuggestParameters.
Initialize them. We can say UseFuzzyMatching = true. And then say Top equals let's say 20 records. And want to make sure we have the query client, or in this case the SearchIndexClient. I'm going to call this the queryClient = new SearchIndexClient. And then I have the searchServiceName, and we have accounts, and then SearchCredentials, with a queryKey.
Next I like to call suggesterResults, or the autoComplete results, and this will be equal to queryClient.Documents.Suggest. And we can parse in anything right now, and if we were to take one of our previously existing queries, we had something which was Hughes. And let's just make it Hug. And then parse in the name of the suggester, and the suggestParameters.
And we want to put a break point right here, because it might take some time for the documents to be indexed. But we still haven't called the function, which is ImportDocuments. So what we want to do is after we create or update that particular index. And then we need to parse in an index client here. We can create that real quick. ISearchIndexClient indexClient = serviceClient.Indexes.GetClient, and call that accounts, 'cause that's the name of the index.
Now that we have that, all we need to do is parse that in here, and then wait for this. Now ideally it might take couple of seconds for this much data to get there. So what we'll do is we'll press F5 and then wait here at line number 62. 'Cause we want the indexing to happen in Azure before we make that query. Now this point of time, we've got every single thing. We've got the query, we have the suggester added into the index, and we waited couple of seconds there.
What we're going to do is say F10. As you can see, we got 20 records back. If you look at the first one, the text is Hughes, which is very good. And then the document has a lot of values, but the one that we care about is Hughes, which is part of the firstName. Keep in mind it does not really have any scoring at all done right now. And we can go a little farther, and you'll start seeing the fuzzy logic kick in. As you can see, what we have is text Huff.
And this is not starting with Hug, but since we have fuzzy logic on, as you can see the ninth record happens to be a little off, and this is off by just one letter, and that's Hug and Huf. So it's pretty close. Let's go back a little bit higher, and you can see there's Huffman. And number two was still Hughes, but again, there are only two values in the entire database, especially when it comes to firstName and lastName for Hughes. So we can only get two exact matches here.
We can see number three Huffman also comes from the firstName. We can go number 10. Huff comes from the lastName. And go back all the way to this particular record, and we can see Hur is what we get now, it's coming from the lastName. That's because we have added the source fields, and they could only be firstName and lastName in this case. So this is a very easy way to create an autocomplete without having to do much work.
All we had to do was create a suggester, add it as part of the index, and then while retrieving, we used the same name, which was autoComplete.
- Querying and indexing
- Creating a search service
- Using APIs during searching
- Importing JSON data
- Handling synonyms
- Working with suggestors and facets