Using full Lucene Query Syntax in Azure Search

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.

Search Index Example

Notice how the fields have different search properties

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:

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .NET Core, Microsoft Azure and tagged , , , , . Bookmark the permalink.

1 Response to Using full Lucene Query Syntax in Azure Search

  1. Pingback: Programmatically create and delete Azure Cognitive Search Indexes from C# code | Brian Pedersen's Sitecore and .NET Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.