0

I'm trying to build a map/reduce that counts the number of documents in a given hour.

The code I'm trying is this

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) };
    }
}

But Raven is throwing an error

System.InvalidOperationException: Could not understand query

"$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",

Any help would be greatly appreciated.

4

1 に答える 1

0

I can confirm that the failure here was the PropertyName set using the JsonPropertyAttribute

Changing

    [JsonProperty(PropertyName = "UTC Date/Time")]
    public DateTime UtcDateTime { get; set; }

to

    public DateTime UtcDateTime { get; set; }

Has stopped the error from being thrown. Evidently RavenDB doesn't like spaces.

于 2013-03-05T01:16:30.507 に答える