我试图构建一个map / reduce来计算给定时间内的文档数量。
我尝试的代码就是这个
public class Syslog_DocumentCountByHour : AbstractMultiMapIndexCreationTask<Syslog_DocumentCountByHour.ReduceResult> {
public class ReduceResult {
public string Source { get; set; }
public DateTime Day { get; set; }
public DateTime Hour { get; set; }
public int Count { get; set; }
}
public Syslog_DocumentCountByHour() {
AddMap<SysLog>(docs => from doc in docs
select new {
Source = "CountByHour",
Day = doc.UtcDateTime.Day.ToString(),
Hour = doc.UtcDateTime.Hour.ToString(),
Count = 1
});
Reduce = results => from result in results
group result by new { result.Hour }
into g
select new {g.Key.Hour, Count = g.Sum(x => x.Count) };
}
}
但是Raven正在抛出错误
System.InvalidOperationException:无法理解查询
"$type": "System.InvalidOperationException, mscorlib", "ClassName": "System.InvalidOperationException", "Message": "Url: \"/indexes/Syslog/DocumentCountByHour\"\r\n\r\nSystem.InvalidOperationException: Could not understand query: \r\ndocs.SysLogs.Select(doc => new {\r\n Source = \"CountByHour\",\r\n Day = doc.UTC Date / Time.Day.ToString(),\r\n Hour = doc.UTC Date / Time.Hour.ToString(),\r\n Count = 1\r\n}) ---> System.InvalidOperationException: Could not understand query: \r\n[DomRegion FileName=, Begin=(3, 18), End=(-1, -1)]: Error - Unexpected symbol `Date'\r\n[DomRegion FileName=, Begin=(4, 19), End=(-1, -1)]: Error - Unexpected symbol `Date'\r\n at Raven.Database.Linq.QueryParsingUtils.GetVariableDeclarationForLinqMethods(String query, Boolean requiresSelectNewAnonymousType) in c:\\Builds\\RavenDB-Stable\\Raven.Database\\Linq\\QueryParsingUtils.cs:line 152\r\n --- End of inner exception stack trace ---\r\n at Raven.Database.Linq.QueryParsingUtils.GetVariableDeclarationForLinqMethods(String query, Boolean requiresSelectNewAnonymousType) in c:\\Builds\\RavenDB-Stable\\Raven.Database\\Linq\\QueryParsingUtils.cs:line 196\r\n at Raven.Database.Linq.DynamicViewCompiler.TransformMapDefinitionFromLinqMethodSyntax(String query, String& entityName) in c:\\Builds\\RavenDB-Stable\\Raven.Database\\Linq\\DynamicViewCompiler.cs:line 404\r\n at Raven.Database.Linq.DynamicViewCompiler.HandleMapFunction(ConstructorDeclaration ctor, String map) in c:\\Builds\\RavenDB-Stable\\Raven.Database\\Linq\\DynamicViewCompiler.cs:line 169\r\n at Raven.Database.Linq.DynamicViewCompiler.TransformQueryToClass() in c:\\Builds\\RavenDB-Stable\\Raven.Database\\Linq\\DynamicViewCompiler.cs:line 132\r\n at Raven.Database.Linq.DynamicViewCompiler.GenerateInstance() in c:\\Builds\\RavenDB-Stable\\Raven.Database\\Linq\\DynamicViewCompiler.cs:line 556\r\n at Raven.Database.Storage.IndexDefinitionStorage.AddAndCompileIndex(IndexDefinition indexDefinition) in c:\\Builds\\RavenDB-Stable\\Raven.Database\\Storage\\IndexDefinitionStorage.cs:line 153\r\n at Raven.Database.Storage.IndexDefinitionStorage.CreateAndPersistIndex(IndexDefinition indexDefinition) in c:\\Builds\\RavenDB-Stable\\Raven.Database\\Storage\\IndexDefinitionStorage.cs:line 138\r\n at Raven.Database.DocumentDatabase.PutIndex(String name, IndexDefinition definition) in c:\\Builds\\RavenDB-Stable\\Raven.Database\\DocumentDatabase.cs:line 1070\r\n at Raven.Database.Server.Responders.Index.Put(IHttpContext context, String index) in c:\\Builds\\RavenDB-Stable\\Raven.Database\\Server\\Responders\\Index.cs:line 83\r\n at Raven.Database.Server.HttpServer.DispatchRequest(IHttpContext ctx) in c:\\Builds\\RavenDB-Stable\\Raven.Database\\Server\\HttpServer.cs:line 856\r\n at Raven.Database.Server.HttpServer.HandleActualRequest(IHttpContext ctx) in c:\\Builds\\RavenDB-Stable\\Raven.Database\\Server\\HttpServer.cs:line 601\r\n",
非常感谢任何帮助。
答案 0 :(得分:0)
我可以确认此处的失败是使用PropertyName
JsonPropertyAttribute
更改
[JsonProperty(PropertyName = "UTC Date/Time")]
public DateTime UtcDateTime { get; set; }
到
public DateTime UtcDateTime { get; set; }
已停止抛出错误。显然RavenDB不喜欢空格。