RavenDB:如何使用多映射/Reduce 索引

发布于 2024-12-05 19:35:07 字数 2554 浏览 1 评论 0原文

我有非常简单的模型:

public class PhraseMeta:
{
 public int Id { get; set; }
 public string ModuleName { get; set; }
 public string Description { get; set; }
 public DateTime ModifiedDate { get; set; }
}

public class Phrase
{
  public int Id { get; set; }
  public int PhraseMetaId { get; set; } //reference to PhraseMeta
  public string Language { get; set; }
  public string Text { get; set; }
}

Phrase 包含一些翻译,PhraseMeta 包含几个短语的元信息。 我正在尝试查找具有模块名称和语言的短语文本。 据我了解,RavenDB 的多映射/减少索引功能可以提供帮助,而不是使用WhereEntityIs。 我的索引是:

public class PhraseEntry
{
  public string MetaId { get; set; }
  public string ModuleName { get; set; }
  public string Language { get; set; }
  public string Text { get; set; } 
}

public class PhraseTranslationIndex : AbstractMultiMapIndexCreationTask<PhraseEntry>
{
  public PhraseTranslationIndex()
  {
    this.AddMap<PhraseMeta>(phraseMetas => from pm in phraseMetas
                 select new
                 {
                   MetaId = pm.Id,
                   ModuleName = pm.ModuleName,
                   Language = (string)null,
                   Text = (string)null
                 });

    this.AddMap<Phrase>(phrases => from phrase in phrases
               select new
               {
                 MetaId = phrase.PhraseMetaId,
                 ModuleName = (string)null,
                 Language = phrase.Language,
                 Text = phrase.Text
               });

    this.Reduce = results => from entry in results
                 group entry by entry.MetaId
                   into g
                   select new
                   {
                     MetaId = g.Key,
                     ModuleName = g.Select(x => x.ModuleName).Where(x => x != null).First(),
                     Language = g.Select(x => x.Language).Where(x => x != null).First(),
                     Text = g.Select(x => x.Text).Where(x => x != null).First()
                   };

    this.Index(x => x.ModuleName, FieldIndexing.Analyzed);
    this.Index(x => x.Language, FieldIndexing.Analyzed);
    this.Index(x => x.Text, FieldIndexing.Analyzed);
  }
}

这就是我尝试使用它的方式:

var entry = documentSession.Query<PhraseEntry, PhraseTranslationIndex>
                           .Where(p => p.ModuleName == "MyModule")
                           .Where(p => p.Language == "en")
                           .FirstOrDefault();

并且该索引没有结果。我正在使用 build 472。

有什么想法吗?

I have quite simple model:

public class PhraseMeta:
{
 public int Id { get; set; }
 public string ModuleName { get; set; }
 public string Description { get; set; }
 public DateTime ModifiedDate { get; set; }
}

public class Phrase
{
  public int Id { get; set; }
  public int PhraseMetaId { get; set; } //reference to PhraseMeta
  public string Language { get; set; }
  public string Text { get; set; }
}

Phrase contains some translations and PhraseMeta has meta information for several Phrases.
I am trying to find Phrase's Text having ModuleName and Language.
As I understood RavenDB's Multi Maps / Reduce indexes feature can help with it instead of using WhereEntityIs.
My index is:

public class PhraseEntry
{
  public string MetaId { get; set; }
  public string ModuleName { get; set; }
  public string Language { get; set; }
  public string Text { get; set; } 
}

public class PhraseTranslationIndex : AbstractMultiMapIndexCreationTask<PhraseEntry>
{
  public PhraseTranslationIndex()
  {
    this.AddMap<PhraseMeta>(phraseMetas => from pm in phraseMetas
                 select new
                 {
                   MetaId = pm.Id,
                   ModuleName = pm.ModuleName,
                   Language = (string)null,
                   Text = (string)null
                 });

    this.AddMap<Phrase>(phrases => from phrase in phrases
               select new
               {
                 MetaId = phrase.PhraseMetaId,
                 ModuleName = (string)null,
                 Language = phrase.Language,
                 Text = phrase.Text
               });

    this.Reduce = results => from entry in results
                 group entry by entry.MetaId
                   into g
                   select new
                   {
                     MetaId = g.Key,
                     ModuleName = g.Select(x => x.ModuleName).Where(x => x != null).First(),
                     Language = g.Select(x => x.Language).Where(x => x != null).First(),
                     Text = g.Select(x => x.Text).Where(x => x != null).First()
                   };

    this.Index(x => x.ModuleName, FieldIndexing.Analyzed);
    this.Index(x => x.Language, FieldIndexing.Analyzed);
    this.Index(x => x.Text, FieldIndexing.Analyzed);
  }
}

This is how I am trying to use it:

var entry = documentSession.Query<PhraseEntry, PhraseTranslationIndex>
                           .Where(p => p.ModuleName == "MyModule")
                           .Where(p => p.Language == "en")
                           .FirstOrDefault();

And this index has no results. I am using build 472.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此刻的回忆 2024-12-12 19:35:07

问题可能是您正在使用 First(),请尝试使用 FirstOrDefault()

The problem is probably that you are using First(), try using FirstOrDefault()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文