c# - 在 Azure 搜索中,如何将日期时间字段与日期时间文字进行比较?

标签 c# azure azure-cognitive-search

因此,我尝试使用 Azure 搜索对表存储进行查询,该表已建立索引。 在此处查看表的实体

   [SerializePropertyNamesAsCamelCase]
public class Asset : TableEntity
{
    public Asset(){ }

    public Asset(string name, DateTimeOffset toBePublished)
    {
        Name = name;
        ToBePublishedDate = toBePublished.ToString();
    }

    [System.ComponentModel.DataAnnotations.Key]
    public string Id{ get; set; } = DateTimeOffset.UtcNow.ToString();

    [IsFilterable, IsSortable, IsSearchable]
    public string Name { get; set; }

    [IsFilterable, IsSortable, IsSearchable]
    public string Version { get; set; }

    [IsFilterable, IsSortable, IsSearchable]
    public string ToBePublishedDate { get; set; }

    [IsFilterable, IsSortable, IsSearchable]
    public string ToBeRetiredDate { get; set; }

    [IsFilterable, IsSortable]
    public bool IsApproved { get; set; } = false;

    [IsFilterable, IsSortable]
    public bool IsDraft { get; set; } = true;

我正在尝试运行一个查询,该查询将返回所有小于当前时间的 ToBePublishedDates。到目前为止我尝试这样做的方式是这样的

 public static Task UpdateLatestAssetViewTableAsync(Asset asset, CloudTableClient client)
    {
        return Task.Run(() =>
        {
            CloudTable table = client.GetTableReference("TestClient");

            SearchParameters parameters;
            DocumentSearchResult<Asset> result;

            parameters = new SearchParameters
            {
                Filter = $"toBePublishedDate lt {DateTimeOffset.UtcNow}",
                Select = new [] {"name", "version"}
            };

            try
            {
                result = AzureSearch.CreateSearchIndexClient().Documents.Search<Asset>("*", parameters);

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            Console.Write(result);

        });
    }

这会引发以下异常

`{Microsoft.Rest.Azure.CloudException: Invalid expression: An identifier was expected at position 21.
Parameter name: $filter
   at Microsoft.Azure.Search.DocumentsOperations.<DoContinueSearchWithHttpMessagesAsync>d__21`3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Search.DocumentsOperationsExtensions.<SearchAsync>d__17`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Search.DocumentsOperationsExtensions.Search[T](IDocumentsOperations operations, String searchText, SearchParameters searchParameters, SearchRequestOptions searchRequestOptions)
   at AssetSynch.Controllers.TableStorageViewFunctions.<>c__DisplayClass0_0.<UpdateLatestAssetViewTableAsync>b__0() in C:\Users\Harry\onedrive - presentation solutions ltd\documents\visual studio 2015\Projects\AssetSynch\src\AssetSynch\Controllers\TableStorageViewFunctions.cs:line 32}`

我刚刚开始使用 azure 搜索,找不到任何类似的问题,我试图执行与 Microsoft 网站上的示例类似的操作,网址为: https://learn.microsoft.com/en-us/rest/api/searchservice/odata-expression-syntax-for-azure-search

$filter=baseRate lt 200 和lastRenovationDate ge 2012-01-01T00:00:00-08:00

但据我所知,在调试我的 C# 代码时,过滤器会变成这样

parameters = {$count=false&$filter=toBePublishedDate%20lt%2017%2F05%2F2017%2014%3A19%3A26%20%2B00%3A00&queryType=simple&searchMode=any&$select=name,version}

看起来一点也不像,有什么改变的建议吗?

最佳答案

有两个问题会阻止过滤器工作:

  1. toBePublishedDate 字段的类型不正确。 Asset 类中对应的属性是 string 类型,但它需要是 DateTimeOffset。否则,您无法使用 lt 运算符对其进行比较。
  2. 过滤器中日期时间文字的格式需要遵循正确的格式。这应该有效:Filter = $"toBePublishedDate lt {DateTimeOffset.UtcNow.ToString("O")}"。它使用 ToString 和往返格式说明符“O”。请参阅MSDN有关各种 DateTime 格式说明符的文档。

关于c# - 在 Azure 搜索中,如何将日期时间字段与日期时间文字进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44027727/

相关文章:

c# - 如何在 Windows Phone 8.1 中缓存页面

sql - LINQ to SQL 输出与预期不同,如何解决?

azure - 从 Azure 认知搜索索引中删除文档

azure - 估计 Azure 搜索成本

c# - 与多个事件发布者和多线程关联的单个 .NET 事件订阅者

c# - 当返回类型为 IHttpActionResult 时,Web API 2 返回不带引号的简单字符串

c# - 将 IAsyncResult 模式转换为任务

azure - 无法使用 SPARK 在 Synapse Lake 数据库中创建架构

sql - 如何将数据从生产迁移到开发但又打乱敏感数据?

Azure 搜索索引 - 管理数据库每 24 小时删除/替换时的索引器?