Join Chander Dhall for an in-depth discussion in this video Synonyms, part of Azure Search for Developers.
- [Instructor] Now that we've seen how we can do create operations on an index let's go ahead and add some synonyms to Azure search and see how it handles it. Before we proceed, I'd like to get rid of some of this code. First we'd like to add a synonym map, so we could say is accountsSynonyms where accounts happens to be the index and then synonyms will be added using the map.
Now one thing you'll notice is that it wouldn't really work and you wouldn't get any instances for it and the reason is simple. It's actually part of preview which is of the next version. It doesn't exist in the current stable version, so what you'll have to do is go to view, package manager console, and say install package microsoft.azure.search and specify the version and it's 4.0.0 dash preview.
So once you have that you'll see that this automatically resolves itself, so we have a new synonym map. We can give it any name we'd like and I'm just going to give it a verb-less name to describe what it does, so it's a city state synonym map and then it's looking for a format and we're just using the solr format and then we can add synonyms. Now there are a couple of ways to do it.
So you could do something like Cal, California, and then CA and what that means is any time it looks for CA and there's a value called California, it will automatically replace that for it and it also means that if it's looking for CAL and the value happens to be CA it will still bring it back and then you can add a lot of other ones. For example, you could do slash N and you can use a completely different way to do this exact same thing.
You could have done Texas and say Texas resolves to TX. Now they both will work, except this one is more like a synonym which works either left to right or right to left, but this one only works one way. So that means if you have Texas it will look for TX in the field, but not the other way around. So if you have TX it wouldn't look for Texas. So that's just one of the things to keep in mind.
You can use any approach you'd like. So once we have the accounts synonyms, the next thing we'd like to do is have the serviceClient create or update the synonym map which in this case happens to be accountsSynonyms. Well next we want to make sure that our index is updated and we're going to get that instance from serviceClient.Indexes.Get and say accounts which is the name of the index and next would be to go to Fields.First and then we can choose the name and the name will be state, we do have a property called state, it has to match something that exists in your class and then we can give it the name here.
So the name here would be city-state-synonym-map and then we can add this also to city if we'd like. So now that we've done this, we would like to update the index.
Next we can do a query and if we do a query now we should be able to get the value back except this time we're going to query something with a synonym. So we're going to have a search parameter. We can say our new search fields we're going to start with one, we can say state is a new search field and we're only going to search state in this case and then since we're searching state, it's probably a good idea to get the state back, so we can verify results and then we can say we're looking for CAL, not California, or CA because that's already part of the object and we can check our data for now and say control F and look for CA and you can see state CA exists, but if I were to look for CAL we will not get that as part of the state.
A better way to search this would be copying the entire object and say CA and you can see that we get a lot of documents back with CA, but if I do CAL, I get nothing back. So regular text search fails however since we programmatically used a synonym, we should still get a result back. I'm going to do F5. We can see we got 17 results back, that means we had 17 instances of California.
You can see the state coming back is CA, but what we're looking for is just CAL. Same here, same there, so that worked.
- Querying and indexing
- Creating a search service
- Using APIs during searching
- Importing JSON data
- Handling synonyms
- Working with suggestors and facets