/minnesota

Sample dotnet project that uses Azure Cognitive Search to search from indexed documents

Primary LanguageC#MIT LicenseMIT

minnesota

Sample project to demonstrate how to use Azure Search Service with .NET Core.

Using wildcards in Azure Search queries is a common requirement. But it's not as straightforward as it seems.

I created this project to demonstrate a few options to use wildcards in Azure Search queries.

Spoiler alert: None of them works as expected 😢

First: Deploying Azure Search Service

After loggin in to Azure CLI, run the following command to create a new Azure Search Service.

cd src
source ../deploy_infra.sh

The deploy_infra.sh script will create a new Resource Group and an Azure Search Service instance. It will also set the SEARCH_ENDPOINT and SEARCH_ADMIN_KEY environment variables.

PROJECT_PREFIX=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 6 | head -n 1)
LOCATION="westus"

if [ -f .minnesota.json ]; then
  PROJECT_PREFIX=$(jq -r '.project_prefix' .minnesota.json)
  LOCATION=$(jq -r '.location' .minnesota.json)
else
  echo "{\"project_prefix\": \"$PROJECT_PREFIX\", \"location\": \"$LOCATION\"}" > .minnesota.json
fi

az group create --name "${PROJECT_PREFIX}-rg" --location "${LOCATION}"
az search service create --resource-group "${PROJECT_PREFIX}-rg" --name "${PROJECT_PREFIX}-search" --location "${LOCATION}" --sku "basic" --semantic-search "standard"

export SEARCH_ENDPOINT="https://${PROJECT_PREFIX}-search.search.windows.net"
export SEARCH_ADMIN_KEY=$(az search admin-key show --resource-group "${PROJECT_PREFIX}-rg" --service-name "${PROJECT_PREFIX}-search" --query "primaryKey" -o "tsv")

Second: Populating the Index

After creating the Azure Search Service, you can run the following command to populate the index with some sample data (./src/data.json).

cd src
dotnet run

The Program.cs file contains the code to create the index and populate it with some sample data (./src/data.json).

image

After Search Index is created and populated with _sample data+, Program.cs is going in a loop to ask for a search query, run the query and display the results.

According to the Azure Search Query Simple Syntax, wildcard options are available for Azure Search queries.

Program.cs#L74 adds wildcard character * to the end of the search query and runs the query.

Third: Running the Queries

First query is medical* and it returns 0 results.

image


Second query is medi* and it returns 11 results.

image


Third query is medic* and it returns 11 results.

image


Fourth query is medica* and it returns 0 results.

image


Even if the same queries run on the Azure Portal, the results are the same.


Running the medical query on the Azure Portal returns 10 results.

image


Running the medi query on the Azure Portal returns 0 results.

image


Running the medi* query on the Azure Portal returns 10 results.

image


Running the medical* query on the Azure Portal returns 0 results.

image