在 Azure 搜索中,如何将日期时间字段与日期时间文字进行比较?
In Azure Search, how do I compare a datetime field to a datetime literal?
所以我正在尝试使用 Azure 搜索对我的 Table 存储进行查询,table 已编入索引。
请在此处查看 table 的实体
[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;
我正在尝试 运行 一个查询,该查询将 return 所有小于当前时间的 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 search 并找不到任何类似的问题,我正在尝试做类似于 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}
看起来一点也不像,有什么建议可以更改吗?
有两个问题阻止过滤器工作:
toBePublishedDate
字段的类型不正确。 Asset
class中对应的属性是string
类型,但需要是DateTimeOffset
。否则您不能使用 lt
运算符对其进行比较。
- 过滤器中日期时间文字的格式需要遵循正确的格式。这应该有效:
Filter = $"toBePublishedDate lt {DateTimeOffset.UtcNow.ToString("O")}"
。它使用 ToString
和往返格式说明符 "O"。有关各种 DateTime
格式说明符的文档,请参阅 MSDN。
所以我正在尝试使用 Azure 搜索对我的 Table 存储进行查询,table 已编入索引。 请在此处查看 table 的实体
[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;
我正在尝试 运行 一个查询,该查询将 return 所有小于当前时间的 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 search 并找不到任何类似的问题,我正在尝试做类似于 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}
看起来一点也不像,有什么建议可以更改吗?
有两个问题阻止过滤器工作:
toBePublishedDate
字段的类型不正确。Asset
class中对应的属性是string
类型,但需要是DateTimeOffset
。否则您不能使用lt
运算符对其进行比较。- 过滤器中日期时间文字的格式需要遵循正确的格式。这应该有效:
Filter = $"toBePublishedDate lt {DateTimeOffset.UtcNow.ToString("O")}"
。它使用ToString
和往返格式说明符 "O"。有关各种DateTime
格式说明符的文档,请参阅 MSDN。