The Azure Cognitive Search is the search engine in Microsoft Azure. You can search using a simple queries (default) which is good at doing full text searches, or you can use full syntax which is a Lucene query syntax. The full syntax is good at searching for specific values in specific fields.
GOOD TO KNOW: SEARCHABLE VS FILTERABLE
Not all fields can be searched, and not all fields are searched the same way. Your field needs to be “facetable” (great for GUID’s and other ID’s that you do exact search on) or “searchable” (great for text) in order for the field to be searched. If your field is “filterable” (great for booleans and other exact values), you need to specify the search differently for this field.
In my examples, I will search using the “AllCategoryIDs” and “CustomerName” fields, and filter using the “IsFeed” field.
THE SEARCH SYNTAX:
You can test out your searches using the POST endpoint in Azure Search.
Do a POST to:
https://[yourazure]/indexes/[yourindex]/docs/search?api-version=2020-06-30 Headers: api-key: The API key of the index Content-Type: application/json
Content:
{ "search": "field:value", "filter": "field eq true", "queryType": "full", "searchMode": "all" }
Replace “field” with the name of the field, and “value” with the name of the value.
Notice how the syntax is different for search and filter? The “search” field is the actual Lucene Query Syntax, while the “filter” is the simple syntax. Why is it so? I don’t know.
SEARCH EXAMPLE: GIVE ME ALL CUSTOMERS WITH NAME “MICROSOFT” OR “APPLE”:
{ "search": "CustomerName:Microsoft or CustomerName:Apple", "queryType": "full", "searchMode": "all" }
SEARCH EXAMPLE: ALWAYS RETURN “MICROSOFT” AND ALL CUSTOMERS WITH A CERTAIN CATEGORY GUID:
{ "search": "AllCategoryIds:0baa80ca-a16e-4823-823e-06a11ddd2310 OR CustomerName:Microsoft", "queryType": "full", "searchMode": "all" }
SEARCH EXAMPLE: GIVE ME ALL CUSTOMERS WITH NAME “MICROSOFT” WHERE ISFEED IS FALSE:
{ "search": "CustomerName:Microsoft", "filter": "IsFeed eq false", "queryType": "full", "searchMode": "all" }
HOW DO TO SEARCH FROM C#
This is a small example on how to use Microsoft Azure Search NuGet Package to do a full lucene query search:
using Microsoft.Azure.Search; using Microsoft.Azure.Search.Models; void Search() { // Get a search client SearchServiceClient searchServiceClient = new SearchServiceClient("accountname", new SearchCredentials("apikey")); // Get an index from the search client ISearchIndexClient indexClient = searchServiceClient.Indexes.GetClient("indexname"); // Create the search parameters SearchParameters searchParameters = new SearchParameters(); searchParameters.QueryType = QueryType.Full; searchParameters.SearchMode = SearchMode.All; searchParameters.IncludeTotalResultCount = true; // Optional filter searchParameters.Filter = "IsFeed eq false"; // The actual query string queryText = "CustomerName:Microsoft"; // Do the search DocumentSearchResult<Document> documentSearchResult = indexClient.Documents.Search(queryText, searchParameters); foreach (SearchResult<Document> searchResult in documentSearchResult.Results) { // Do stuff with the search result } }
MORE TO READ:
- Azure Cognitive Search from Microsoft
- Microsoft Azure Search NuGet Package
- Lucene query syntax in Azure Cognitive Search from Microsoft
- Query types and composition in Azure Cognitive Search from Microsoft