RavenDB:如何使用Multi Maps / Reduce索引



我有一个很简单的模型:

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的Multi Maps/Reduce索引功能可以帮助解决这个问题,而不是使用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();

这个索引没有结果。我正在使用构建472。

任何想法?

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

相关内容

  • 没有找到相关文章

最新更新