gpt4 book ai didi

RavenDB ID 在代码创建的索引中变得困惑

转载 作者:行者123 更新时间:2023-12-01 06:41:48 24 4
gpt4 key购买 nike

首先,我刚刚开始使用 RavenDB,所以在我解释问题时请耐心等待。我正在尝试创建我的第一个 map 、 map 、缩小、转换索引。是的,我知道我正在尝试做很多事情,但我已经完成了大部分工作。

因此,首先我在 global.asax 中执行此操作以确保所有“ID”属性都用作文档的标识符。

_documentStore.Conventions.FindIdentityProperty = p => p.Name == "ID";

好的,现在让我们看一下索引。
public class ProblemListViewIndex : AbstractMultiMapIndexCreationTask<ProblemListView>
{
public ProblemListViewIndex()
{
AddMap<Problem>(problems => from problem in problems
select new
{
ID = problem.ID,
SolutionCount = 0,
});

AddMap<Solution>(solutions => from solution in solutions
select new
{
ID = solution.ProblemID,
SolutionCount = 1,
});


Reduce = results => from result in results
group result by result.ID
into g
select new
{
ID = g.Key,
SolutionCount = g.Sum(x => x.SolutionCount),
};

Indexes.Add(x => x.ID, FieldIndexing.Analyzed);

TransformResults = (database, results) => from result in results
let problem = database.Load<Problem>("problems/" + result.ID.ToString())
let user = database.Load<User>("users/" + problem.PostedByID.ToString())
select new
{
ID = result.ID,
PostedByID = problem.PostedByID,
PostedByName = user.DisplayName,
SolutionCount = result.SolutionCount,
};

}
}

所以一切看起来都很好,当我在 RavenDB 网站上测试索引时,我得到了好坏参半的结果。我有重复的预测。我有我期望的两个投影,但是它们的两个副本。以下是投影结果上的“ID”。
  • 问题/194
  • 问题/195
  • 194
  • 195

  • 我很困惑,但后来我回去看了“ map ”。我的代码在创建的索引中转换为不同的内容。这是最初创建时第一张 map 的样子。
    docs.Problems
    .Select(problem => new {ID = problem.__document_id, SolutionCount = 0})

    即使是 RavenDB 的新手,我也看到了这个问题。当我想使用“ID”字段时,它使用“__document_id”字段。我更改了 map ,然后将索引保存到以下内容。
    docs.Problems
    .Select(problem => new {ID = problem.ID, SolutionCount = 0})

    一旦我这样做了,我的投影就完全符合我的预期和我想要的。
  • 194
  • 195

  • 我的问题是我需要在代码中做什么才能使用“ID”而不是“__document_id”来创建索引?

    最佳答案

    我不明白这里有什么问题。我#ve 使用你的代码进行了一个测试,我得到了我所期望的:

    public class MultiMapWithTransformAndCustomId
    {
    public class Problem
    {
    public string ID { get; set; }
    public string PostedByID { get; set; }
    public string Foo { get; set; }
    }

    public class Solution
    {
    public string ID { get; set; }
    public string ProblemID { get; set; }
    public string Foo { get; set; }
    }

    public class User
    {
    public string ID { get; set; }
    public string DisplayName { get; set; }
    }

    public class ProblemListViewIndex : AbstractMultiMapIndexCreationTask<ProblemListViewIndex.ReduceResult>
    {
    public class ReduceResult
    {
    public string ID { get; set; }
    public int SolutionCount { get; set; }
    public string PostedByID { get; set; }
    public string PostedByName { get; set; }
    }

    public ProblemListViewIndex()
    {
    AddMap<Problem>(problems => from problem in problems
    select new
    {
    ID = problem.ID,
    SolutionCount = 0,
    });

    AddMap<Solution>(solutions => from solution in solutions
    select new
    {
    ID = solution.ProblemID,
    SolutionCount = 1,
    });


    Reduce = results => from result in results
    group result by result.ID
    into g
    select new
    {
    ID = g.Key,
    SolutionCount = g.Sum(x => x.SolutionCount),
    };

    Indexes.Add(x => x.ID, FieldIndexing.Analyzed);

    TransformResults = (database, results) => from result in results
    let problem = database.Load<Problem>(result.ID.ToString())
    let user = database.Load<User>(problem.PostedByID.ToString())
    select new
    {
    ID = result.ID,
    PostedByID = problem.PostedByID,
    PostedByName = user.DisplayName,
    SolutionCount = result.SolutionCount,
    };

    }
    }

    [Fact]
    public void Can_do_simple_query()
    {
    using (var documentStore = new EmbeddableDocumentStore
    {
    RunInMemory = true
    })
    {
    documentStore.Conventions.FindIdentityProperty = p => p.Name == "ID";
    documentStore.Initialize();

    using (var documentSession = documentStore.OpenSession())
    {
    documentSession.Store(new User {ID = "users/1", DisplayName = "Daniel"});
    documentSession.Store(new User {ID = "users/2", DisplayName = "Lang"});
    documentSession.Store(new Problem {ID = "problems/194", PostedByID = "users/1"});
    documentSession.Store(new Problem {ID = "problems/195", PostedByID = "users/2"});
    documentSession.Store(new Solution {ID = "solutions/1", ProblemID = "problems/194"});
    documentSession.Store(new Solution {ID = "solutions/2", ProblemID = "problems/194"});
    documentSession.Store(new Solution {ID = "solutions/3", ProblemID = "problems/195"});
    documentSession.SaveChanges();
    }

    new ProblemListViewIndex().Execute(documentStore);

    using (var documentSession = documentStore.OpenSession())
    {
    var results = documentSession.Query<ProblemListViewIndex.ReduceResult, ProblemListViewIndex>()
    .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite())
    .ToList();

    Assert.Equal(2, results.Count);

    var daniel = results.First(x => x.PostedByName == "Daniel");
    var lang = results.First(x => x.PostedByName == "Lang");

    Assert.Equal("problems/194", daniel.ID);
    Assert.Equal("problems/195", lang.ID);
    }
    }
    }
    }

    关于RavenDB ID 在代码创建的索引中变得困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9695391/

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