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

时间:2017-05-17 14:29:34

标签: c# azure azure-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搜索并且找不到任何类似的问题,我试图做类似于微软网站上的例子: 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}

看起来什么都不是,有什么改变的建议?

1 个答案:

答案 0 :(得分:4)

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

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