Search code examples
azureluceneazure-cognitive-search

How to search with whitespace with Azure AI Search filter with Lucene Syntax


I am using Azure Search and azure.search.documents.SearchOptions

I have this which works

SearchOptions searchOptions = new SearchOptions()
    .setFilter("search.ismatch('.*golden.*', 'description', 'full', 'all')");

Above gives me the results for if the description contains the word "golden" anywhere.

But if I try to have it search for multiple words, then it does not work. I'll use the word "golden rod" as an example, where I want to find any description that has the string "golden rod" anywhere.

What I've tried:

  • search.ismatch('.*golden rod.*', 'description', 'full', 'all')
  • search.ismatch('.*golden?rod.*', 'description', 'full', 'all')
  • search.ismatch('.*golden.?rod.*', 'description', 'full', 'all')
  • search.ismatch('.*golden\\ rod.*', 'description', 'full', 'all')

Solution

  • This the sample index i created

    
    {
      "@odata.context": "https://searchservicename.search.windows.net/indexes('index')/$metadata#docs(*)",
      "value": [
        {
          "@search.score": 1,
          "HotelId": "3",
          "Description": "Motel 6 Golden"
        },
        {
          "@search.score": 1,
          "HotelId": "2",
          "Description": "Rodeway Inn"
        },
        {
          "@search.score": 1,
          "HotelId": "1",
          "Description": "Golden Rod Motel"
        }
      ]
    }
    
    

    enter image description here

    // Define sample documents
            var documents = new List<SearchDocument>
            {
                new SearchDocument
                {
                    { "HotelId", "1" },
                    { "Description", "Golden Rod Motel" }
                },
                new SearchDocument
                {
                    { "HotelId", "2" },
                    { "Description", "Rodeway Inn" }
                },
                new SearchDocument
                {
                    { "HotelId", "3" },
                    { "Description", "Motel 6 Golden" }
                },
                // Add more sample documents as needed
            };
    
    

    To search for multiple words with whitespace using Azure Cognitive Search filter with Lucene syntax, you can use the following approach: '\"golden rod\"', 'Description', 'full', 'all'

    enter image description here