gpt4 book ai didi

c# - RavenDB 中的 Map/Reduce 抛出 InvalidOperationException

转载 作者:行者123 更新时间:2023-11-30 17:14:21 24 4
gpt4 key购买 nike

我在使用 map/reduce 操作时遇到了困难。我是 C# 的爱好者,所以可能部分是由于我缺乏 lambda 和 linq 的经验来彻底分析和理解导致错误的原因(什么是对象创建表达式)。

代码编译完美,但执行时抛出异常,我不明白哪个部分不正确。

当我禁用 reduce 操作时,错误仍然存​​在,所以我认为它与
map 部分。

RavenDB 版本为:RavenDB-Build-573

这是异常: System.InvalidOperationException: Variable
初始值设定项选择必须有一个带有对象的 lambda 表达式 create
表达

我为 RavenDB 提供了这样的索引:

IndexCreation.CreateIndexes(typeof(FullTree).Assembly, store);

Entity 对象是一个简单的类,具有一个 int Id 和一个 int ParentId。

这是 map /减少部分:

public class FullTreeObject
{
public int Id { get; set; }
public int Count { get; set; }
public int ParentId { get; set; }
}

public class FullTree : AbstractIndexCreationTask<Entity,FullTreeObject>
{
public FullTree()
{

Map = entities => from entity in entities
let ids = new object[]
{
new { entity.Id, Count = 0,
entity.ParentId },
new { Id = entity.ParentId, Count
= 1, ParentId = (string)null}
}
from id in ids
select id;

Reduce = results => from result in results
group result by result.Id into g
let parent = g.FirstOrDefault(x =>
x.ParentId != null)
select new
{
Id = g.Key,
Count = g.Sum(x => x.Count),
ParentId = parent ==
(object)null ? (object)null : parent.ParentId
};

}
}

希望有人能给我提供一些线索。提前致谢。

——

作为对输入的 react ,我修改了 map/reduce 实现。 RavenDB 需要 map 和 reduce 输出相同类型,但老实说,这不可能是正确的,因为我输出的是实体对象的值,而不是数组中获取的值。但我无法通过智能感知达到这些值(value)。

该索引是在 RavenDB 工作室中创建的,但没有返回任何结果,因此我可以假设我的实现有问题。
public class FullTree : AbstractIndexCreationTask<Entity,FullTreeObject>
{
public FullTree()
{
Map = entities => from entity in entities
let ids = new object[]
{
new { entity.Id, Count = 0, entity.ParentId },
new { Id = entity.ParentId, Count = 1, ParentId = (string)null}
}
from id in ids
select new {
entity.Id,
Count = 1,
entity.ParentId
};


Reduce = results => from result in results
group result by result.Id into g
let parent = g.FirstOrDefault(x => x.ParentId != null)
select new
{
Id = g.Key,
Count = g.Sum(x => x.Count),
ParentId = parent == (object)null ? (object)null : parent.ParentId
};
}
}

Build 601 更新:
public class FullTree : AbstractIndexCreationTask<Entity, FullTree.ReduceResult>
{

public class ReduceResult
{
public int Id { get; set; }
public int Count { get; set; }
public int ParentId { get; set; }
}
public FullTree()
{
Map = entities => from entity in entities
let items = new[]
{
new { Id = (int) entity.Id, Count = (int) 0, ParentId = (int) entity.ParentId },
new { Id = (int) entity.ParentId, Count = (int) 1, ParentId = (int) 0}
}
from item in items
select new {
Id = item.Id,
Count = item.Count,
ParentId = item.ParentId
};


Reduce = results => from result in results
group result by result.Id into g
let itemWithParent = g.FirstOrDefault(x => x.ParentId != 0)
select new
{
Id = g.Key,
Count = g.Sum(x => x.Count),
ParentId = (itemWithParent == null) ? (int)0 : itemWithParent.ParentId
};
}
}

错误返回:
[WebException:远程服务器返回错误:(500) 内部服务器错误。]
System.Net.HttpWebRequest.GetResponse() +6111651
c:\Builds\raven\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:231 中的 Raven.Client.Connection.HttpJsonRequest.ReadStringInternal(Func`1 getResponse)

[无效操作异常:{
"Url": "/indexes/FullTree",
"错误": "System.InvalidOperationException: 无法理解查询:\r\n-- 第 2 行第 55 行:无效的 NewExpression\r\n-- 第 2 行第 91 行:无法解析 double .0.0\r\n- - 第 2 行第 183 行:无法解析 double .0.0\r\n--第 2 行第 275 行:无法在 Raven.Database.Linq.QueryParsingUtils.GetVariableDeclarationForLinqMethods(字符串查询, bool 值 requiresSelectNewAnonymousType) in c:\Builds\raven-unstable\Raven.Database\Linq\QueryParsingUtils.cs:line 122\r\n at Raven.Database.Linq.DynamicViewCompiler.TransformMapDefinitionFromLinqMethodSyntax(String query, String& entityName) c:\Builds\raven-unstable\Raven.Database\Linq\DynamicViewCompiler.cs:line 355\r\n at Raven.Database.Linq.DynamicViewCompiler.HandleMapFunction(ConstructorDeclaration ctor, String map) in c:\Builds\raven-不稳定\Raven.Database\Linq\DynamicViewCompiler.cs:line 132\r\n at Raven.Database.Linq.DynamicViewCompiler.TransformQueryToClass() in c:\Builds\raven-unstable\Raven.Database\Linq\DynamicViewC ompiler.cs:line 97\r\n at Raven.Database.Linq.DynamicViewCompiler.GenerateInstance() in c:\Builds\raven-unstable\Raven.Database\Linq\DynamicViewCompiler.cs:line 489\r\n at Raven .Database.DocumentDatabase.PutIndex(String name, IndexDefinition definition) in c:\Builds\raven-unstable\Raven.Database\DocumentDatabase.cs:line 717\r\n at Raven.Database.Server.Responders.Index.Put( IHttpContext 上下文,字符串索引)在 c:\Builds\raven-unstable\Raven.Database\Server\Responders\Index.cs:line 72\r\n at Raven.Database.Server.Responders.Index.Respond(IHttpContext 上下文)在 c:\Builds\raven-unstable\Raven.Database\Server\Responders\Index.cs:line 49\r\n at Raven.Database.Server.HttpServer.DispatchRequest(IHttpContext ctx) in c:\Builds\raven-不稳定\Raven.Database\Server\HttpServer.cs:line 527\r\n 在 c:\Builds\raven-unstable\Raven.Database\Server\HttpServer 中的 Raven.Database.Server.HttpServer.HandleActualRequest(IHttpContext ctx)。 cs:line 303"
}]
Raven.Client.Connection.HttpJsonRequest.ReadStringInternal(Func`1 getResponse) in c:\Builds\raven\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:295

最佳答案

Micha,错误现已修复。它在构建 601 内。
要了解如何实际实现您所要求的索引,请查看此测试:https://github.com/ayende/ravendb/blob/master/Raven.Tests/Bugs/MapRedue/TreeWithChildrenCount.cs

关于c# - RavenDB 中的 Map/Reduce 抛出 InvalidOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8832221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com