- [Instructor] Before we move on to showing an example that does boosting or scoring, I'd like to remove some of this code. Every item returned in search result happens to have a score associated with it. Scoring refers to the competition of a search score for every item. Scoring algorithm is internal to search engines and is called the ranking algorithm. At the end of the day, score of an item simply indicates it's relevance.
The higher the score, the higher the relevancy of that item in that particular search. However, there are times because of the needs of the business we might have to customize the search results. In order to do that, we need to create a scoring profile. As we saw earlier, a scoring profile is part of the index definition and it needs the index definition to be updated if the index has already been created. A scoring profile is composed of weighted fields, functions, and parameters.
In order to use the profile, we will need to work with textWeights and I'm going to say equals new TextWeights and now we'll assign weights to the fields that need to be scored higher and ranked higher than other fields. It's a very simple dictionary that takes the name of the field and then has a double, which is the score. The score could be anything, it could be 1.2, 3.5, really doesn't matter.
The higher the score, the better it is for rating or scoring that particular field higher than others. We can use firstName and say we're going to assign this a score of 2 and the reason we're trying to do this is if you go back to accounts.json and we look at the same thing we searched earlier, hugh's as you can see, we have a last name Hughes and then we also have a first name that's Hughes.
What we'd like to do is return the same query that gives us two records back, but we're going to make sure that this time we're going to say the weight of firstName is two and the weight of lastName is five. So that means when I get the result, the last name Hughes will be higher than the first name Hughes. So a very simple case just to demonstrate scoring, or boosting, in Azure search.
Now I'd like to create the definition. I'm going to call it accountIndexDefinition and, of course, the name happens to be accounts and fields are FieldBuilder.BuildForType<Account> and then I'd like to assign the weights we just created.
So we have accountIndexDefinition.ScoringProfiles, we'll create a new list of scoring profiles and then initialize this with a scoring profile, again the name of the scoring profile doesn't matter, but has to be the same name we're going to end up using later. I'm going to call it boostLastName over the first name and then say TextWeights equals textWeights and then use a function aggregation.
In this case, I'm just going to use FirstMatching. The next thing would be to update the current index. I'm going to say, Indexes.createOrUpdate and pass in the definition we just created and next we'll run the query, we're going to say DocumentSearchResult of type account call it results and we can say try catch and if there's an exception, for now we'll just throw it and then we're going to have search parameters equals new search parameters and we're really looking at getting the return back, in this case, we definitely want the first name and the last name so we can compare our results and then we'll make a call to Azure search.
Before we make that call, we need the client for querying. We'll use SearchIndexClient queryClient equals new SearchIndexClient and then pass in the search service name accounts which is the name of index and then new SearchCredentials, I'm going to pass in the query key.
We're going to use the queryClient to search index account and we're going to search for Hughes and pass in the parameters. Place a break point at line number 64 and say F5.
So just so you know that we have created a scoring profile, but we haven't really used it here. So these results could either be in order or completely out of order, we don't know that yet, but this is just the base result without the application of that scoring profile. So let's go to results and check this out. As you can see, we have two results here and the first one happens to have a score of 1.97, which is definitely the higher one out of the two and you can see the first name happens to be Hughes.
Now if you remember our scoring profile, it actually boosts the last name higher than anything else. If I were to go back and show you. We added the textWeights right here and inside the textWeights we did add weight for first name, but we added a higher weight for last name. Now what does this mean? If there is city, or let's say age, or anything else that happens to have the same exact value, in that case first name and last name will actually rank higher than even city, so the number five is a pretty high number, that means no matter what happens last name will be ranked higher than anything else out there.
So let's go back and if we look at the document which has last name currently is only 1.84, so how do we apply this? It's very simple. We can go back to our search parameters and say parameters.ScoringProfile and grab the same exact profile that we just used which is boostLastName. So once we apply this, all we need to do is press F5, we can look at the results, we still have two results, and this time we have a score of 3.9, now this is a big jump from 1.9 to 3.9 and let's see what the document is.
So as you can see the document now has last name Hughes which is exactly what we are expecting after the application of that profile. Just by adding that profile we have been able to boost last name high up and then next is number two and as you can see the score's actually lower than last time, it's 1.5, so comparatively if you go by the scores we gave which is two and five, it makes sense. You can see the last name happens to be Owens and first name is Hughes.
So you saw that very easily what we could do is use a scoring profile to boost the last name, in this case, or any other property for that matter.
- Querying and indexing
- Creating a search service
- Using APIs during searching
- Importing JSON data
- Handling synonyms
- Working with suggestors and facets