How to do Search inside a subFolder with Azure AI Search.

Hassan Kaleem 40 Reputation points
2024-05-01T06:29:32.8333333+00:00

In Azure AI Search how to find something inside a subdirectory? right now when I am searching something i am getting result from all of the subfolders, I have to do search within a folder for example right now I am in /Submission directory and there are subfolder in here how do I search in them? I have tried some filter options but it is not working, anybody here who can help?
[HttpGet]

[Route("azureAISearch")]

[ProducesResponseType(typeof(ResponseStatusCodes), StatusCodes.Status202Accepted)]

[ProducesResponseType(typeof(ResponseStatusCodes), StatusCodes.Status500InternalServerError)]

public async Task<IActionResult> GetAzureAiSearchResult(string? requestId = null, string? searchText = null)

{

 try

 {

     _indexClient = new SearchIndexClient(new Uri(_searchServiceUri), new AzureKeyCredential(_queryApiKey));

     _searchClient = _indexClient.GetSearchClient(_searchServiceIndex);

     //string filterExpression = $"folderName eq '{requestId}'";

     //string filterExpression = $"folderPath eq 'submission/{requestId}' and endswith(folderPath, '/{requestId}') and key eq '{searchText}'";

     //string filterExpression = $"key eq '{requestId}' and key eq '{searchText}'";

     var options = new SearchOptions()

     {

         IncludeTotalCount = true,

         //Filter = $"startswith(folderPath, 'submission/{requestId}/') and substring(folderPath, string_length(folderPath) - string_length('{requestId}') + 1, string_length('{requestId}')) eq '{requestId}'"

         //Filter = $"startswith(folderPath, 'submission/{requestId}/') and substring(folderPath, string_length(folderPath) - string_length('{requestId}') + 1) eq '{requestId}'"

         //Filter = $"startswith(folderPath, 'submission/{requestId}/') and substring(folderPath, length(folderPath) - length('{requestId}') + 1) eq '{requestId}'"

         Filter = $"startswith(folderPath, 'submission/{requestId}/') and substring(folderPath, string_length(folderPath) - string_length('{requestId}') + 1, string_length('{requestId}')) eq '{requestId}'",

     };

     // Check for a search string

     if (searchText == null)

     {

         searchText = "";

     }

     // Send the query to Search.

     var resultList = await _searchClient.SearchAsync<AzureAISearch>(searchText, options).ConfigureAwait(false);

     //return Accepted(new { QueryData = resultList});

     return Accepted(new { QueryData = resultList.Value.GetResults().Select(result => result.Document).ToList() });

 }

 catch (Exception ex)

 {

     return StatusCode(500, $"Internal server error: {ex.Message}");

 }

}

Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
740 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,227 questions
{count} votes

Accepted answer
  1. ajkuma 22,761 Reputation points Microsoft Employee
    2024-05-02T18:12:30.1733333+00:00

    To better assist you on this, what is the data source you are indexing? Are you referring to any Azure doc for the steps?

     

     Based on my understanding of your issue description, as outlined in this doc, the fields you want from the blob indexer: Azure Blob indexer - Azure AI Search | Microsoft Learn

     

     

    If you haven’t done this already, for the filter to work, the field (folderPath in this case) must be attributed as “filterable” in your index definition. Filters in text queries

     

    If you’re not storing the value in a metadata field and instead, you’re trying to extract it from a document, there could be issues if the indexer doesn’t support field mapping for your document type, such as a markdown document. In such cases, you might need to create a custom web API skill set. This skill set would need to be invoked to perform the necessary mapping before the steps you outlined. Doc section : Supported document formats

    The options listed here are supported: Limitations and considerations

    If it’s for SharePoint, to filter specific folders, introduce a custom column when creating documents in SharePoint. This could be automated using a workflow or a similar process. After the indexing process, this custom column can be used as a filter during the query for documents: apply filters

    Kindly let us know, I'll follow-up with you.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Hassan Kaleem 40 Reputation points
    2024-05-06T09:39:35.2+00:00

    Thank You for your Response, that worked here is the correct code for this.
    Note, we have to make the filed filterable before we try to apply filtering on it.
    [HttpGet]

    [Route("azureAISearch")]

    [ProducesResponseType(typeof(ResponseStatusCodes), StatusCodes.Status202Accepted)]

    [ProducesResponseType(typeof(ResponseStatusCodes), StatusCodes.Status500InternalServerError)]

    public async Task<IActionResult> GetAzureAiSearchResult(string? requestId = null, string? searchText = null)

    {

    try
    
    {
    
        _indexClient = new SearchIndexClient(new Uri(_searchServiceUri), new AzureKeyCredential(_queryApiKey));
    
        _searchClient = _indexClient.GetSearchClient(_searchServiceIndex);
    
        var options = new SearchOptions()
    
        {
    
            IncludeTotalCount = true,
    
            Filter = requestId != null ? $"metadata_storage_path eq '{requestId}'" : null
    
        };
    
        // Check for a search string
    
        searchText ??= "";
    
        // Send the query to Search.
    
        var resultList = _searchClient.SearchAsync<AzureAISearch>(searchText, options);
    
        return Accepted(new { QueryData = resultList });
    
        //return Accepted(new { QueryData = resultList.Value.GetResults().Select(result => result.Document).ToList() });
    
    }
    
    catch (Exception ex)
    
    {
    
        return StatusCode(500, $"Internal server error: {ex.Message}");
    
    }
    

    }