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

Har*_*rry 5 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;
Run Code Online (Sandbox Code Playgroud)

我正在尝试运行一个查询,该查询将返回所有小于当前时间的 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);

        });
    }
Run Code Online (Sandbox Code Playgroud)

这将引发以下异常

`{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}`
Run Code Online (Sandbox Code Playgroud)

我刚刚开始使用 azure 搜索,但找不到任何类似的问题,我试图做一些类似于 Microsoft 网站上的示例的操作:https : //docs.microsoft.com/en-us/rest/api /searchservice/odata-expression-syntax-for-azure-search

$filter=baseRate lt 200 and 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}
Run Code Online (Sandbox Code Playgroud)

看起来不像那样,关于改变什么的任何建议?

Bru*_*ton 9

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

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