- [Instructor] So back to Visual Studio. Just taking a look at what we just did. First we queried the service client and then line number 27, that happens to be a typo. You might want to make sure that this is Indexes. And then we created that index and it's that index that exists. We'll delete it for the next call. For now, we can comment this code.
Then we have the index definition and now we created this particular index and we imported all those documents. We won't really need any of this code. All this code can be commented or even introduced into a different method that could be called whenever needed, but this is mostly set up code. Next what we need to do is query this index that we just created. For that to work, we're going to say search indexclient and I'm going to call it queryClient equals new SearchIndexClient and then say searchServiceName and then give it the name of the index.
Most of the time, it's going to come from app settings. For now, we're going to hardcode this and then say new search credentials and our credentials will have an API key. In this case, we're going to use the query key. You can always use the admin keys for queries too, however, it's a better practice to just use the query key because it's read only. Once we've done the queries, we're going to get the document search result and it's going to be of type account.
We're going to call it results. Next, we're going to supply some parameters to our search. And the simplest parameter through supply is select, which means that all I want to do is make sure that I don't get back the entire object and I only get the field that I'm looking for.
So I can say I want the first name, I want the last name. Once I have the parameters, I'm going to execute the search. Results equals queryClient.Documents.Search of type accounts and then we can search anything.
If you go to accounts.json, let's look at what we want to search. We see a first names Hughes. I'm going to Ctrl + C, copy this and say Ctrl + F and look for this in the document. So we have an email which is Hughes, but it wouldn't work because it's not an entire word. But we have one more value and in this case, it's a first name. So we should get two results back.
And now I'm going to introduce parameters in the function and then write the results. For now, we'll just introduce a breakpoint here and see what kind of results we get. Now we're going to press F5. As you can see, we've got count two, which is exactly what we were expecting. In this case, we have first name, which is Hughes, and then number two, we have the last name, which is Hughes, and the first name is Vin.
Let's use a slightly more complicated query with a filter. In this case, we're going to have new search parameters and the first one is going to be a filter. And I want to say give me age as long as it's less than 45 since age is filterable, I should get my results. And then I also want to see the records except I want to see the first property as age and then I want to get the first name...
And the last name rather than getting an entire object. I'm going to say results equals query client.Documents.Search Account and in this case I could say star. Go and search everything so I can get the most amount of results back. I'm doing this to get more data. Last time we just got two documents. Star means everything, but we do have a filter here.
So let's do F5. We have our results. We can see we have count 50 and let's randomly select anything. As you can see, the age is 31, which is less than 45 and we get back the first name and the last name only. We can see the age is 34, which is, again, less than 45. We can pick one more record randomly. Age is 23 and we have the last name as Diaz.
Let me show you another query where we can use a different operator. We can do an orderby and say OrderBy new array and I'm going to use state in a descending order and then I can select certain fields. I'm going to start with the state because we are ordering by the state and then we can show the last name and maybe before that, we also want to see the city.
We can also say we want only the top 10 records. Now if we have less than 10, we'll get less than 10, but otherwise, we'll only get the top 10 records. And we can say results equal query client.documents.search. Account and in this case, we can just say star again. Now one thing to keep in mind is whatever we place in here is going to search everything which is searchable.
So Hughes is not just searching the first name and the last name. It's searching every field over there that's supposed to be searchable and the only fields that will be shown are the ones that are retrievable and also part of the select operator. Just to get more records back, I'm going to do a star query instead of doing a point query and now we can say F5. So we can see the results and when you have 10 records, which makes sense since we did a star query so we should have more than 10 records, but since we said top equals through 10, we'll get 10 records.
View the document. You can see state starts with WY and then go a little bit further. We still have the state Wyoming and that's again the same state. So it is ordering it by the state. We can kill this and remove this particular constraint and now do F5. And now the results are 50. Despite default, we get 50 records and you can see it's still the same state.
So we have a lot of records from the same state for some reason. Now we're going to make a simple change and say city in a descending order or we can just remove the descending order and make it an ascending order and press F5. Look at the results. We have count 50 and we're starting with state and now we have city as Ola.
We go back up there. The state is Wyoming but the city starts with a B. Do you see, this is in ascending order so the city starts with a B and we have Ola, and if we go a little bit further down the chain, we see a different state this time, which is great. And then the city starts with C in this case. Let me go a little bit further, so it's working. So we're getting the city back in a descending order and we're getting the state in a descending order.
One of the things to keep in mind is when we were creating the index, that we had a different definition altogether. We could have also done create or update and it also comes with an Async version so either way, both of them would have worked.
- Querying and indexing
- Creating a search service
- Using APIs during searching
- Importing JSON data
- Handling synonyms
- Working with suggestors and facets