-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathIndexQueryCommand.cs
More file actions
54 lines (48 loc) · 1.82 KB
/
Copy pathIndexQueryCommand.cs
File metadata and controls
54 lines (48 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Text.Json;
using Azure.Mcp.Tools.Search.Options.Index;
using Azure.Mcp.Tools.Search.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Mcp.Core.Commands;
using Microsoft.Mcp.Core.Models.Command;
namespace Azure.Mcp.Tools.Search.Commands.Index;
[CommandMetadata(
Id = "f1938a77-8d6c-49c7-b592-71b4f26508e7",
Name = "query",
Title = "Query an Azure AI Search (formerly known as \"Azure Cognitive Search\") Index",
Description = """
Queries/searches documents in an Azure AI Search index with a given query, returning the results of the
query/search.
""",
Destructive = false,
Idempotent = true,
OpenWorld = false,
ReadOnly = true,
Secret = false,
LocalRequired = false)]
public sealed class IndexQueryCommand(ILogger<IndexQueryCommand> logger, ISearchService searchService)
: AuthenticatedCommand<IndexQueryOptions, List<JsonElement>>
{
private readonly ILogger<IndexQueryCommand> _logger = logger;
private readonly ISearchService _searchService = searchService;
public override async Task<CommandResponse> ExecuteAsync(CommandContext context, IndexQueryOptions options, CancellationToken cancellationToken)
{
try
{
var results = await _searchService.QueryIndex(
options.Service,
options.Index,
options.Query,
options.RetryPolicy,
cancellationToken);
context.Response.Results = ResponseResult.Create(results, SearchJsonContext.Default.ListJsonElement);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error executing search query");
HandleException(context, ex);
}
return context.Response;
}
}